【问题标题】:Elasticsearch not analyzed fieldElasticsearch 未分析字段
【发布时间】:2016-09-13 08:18:41
【问题描述】:

我有一个分析字段,其中包含以下内容:'quick brown foxes' 和另一个包含:'quick brown fox'。

我想找到那些明确包含“狐狸”(不是狐狸)的文档。据我所知,我必须创建一个包含已分析和未分析子字段的多字段(请参阅下面的映射)。但是我该如何查询呢?

这是一个示例(请注意,我的分析仪设置为匈牙利语,但我想这在这里无关紧要):

{
    "settings" : {
        "number_of_replicas": 0,
        "number_of_shards": 1,      
        "analysis" : {
            "analyzer" : {
                "hu" : {
                    "tokenizer" : "standard",
                    "filter" : [ "lowercase", "hu_HU" ]
                }
            },
            "filter" : {
                "hu_HU" : {
                    "type" : "hunspell",
                    "locale" : "hu_HU",
                    "language" : "hu_HU"
                }               
            }
        }
    },
    "mappings": {
        "foo": {
            "_source": { "enabled": true },
            "properties": {
                "text": {
                    "type": "string",
                    "analyzer": "hu",
                    "store": false,
                    "fields": {
                        "raw": {
                            "type": "string",
                            "index": "not_analyzed",
                            "store": false
                        }
                    }
                }
            }
        }
    }
}

我尝试过的查询:match、term、span_term、query_string。所有都在 text 和 text.raw 字段上执行。

【问题讨论】:

    标签: elasticsearch elasticsearch-2.0


    【解决方案1】:

    "index": "not_analyzed" 表示根本不会分析此字段 (https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-index.html)。所以它甚至不会被拆分成单词。我相信这不是你想要的。 取而代之的是,您需要添加新的分析器,其中仅包含标记器 whitespace (https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-whitespace-tokenizer.html):

    "analyzer" : {
          "hu" : {
              "tokenizer" : "standard",
               "filter" : [ "lowercase", "hu_HU" ]
           },
           "no_filter":{
               "tokenizer" : "whitespace"
           }
    }
    

    那么你需要为你的领域使用这个新的分析器:

    "raw": {
         "type": "string",
         "analyzer": "no_filter",
         "store": false
    }
    

    【讨论】:

    • 谢谢谢尔盖!即使在匈牙利也能像魅力一样工作。几天来我一直在寻找解决方案,这是一个不错且简单的“技巧”。再次感谢!
    猜你喜欢
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 2021-04-10
    相关资源
    最近更新 更多