ElasticSearch索引,因此查询返回包含

时间:2016-09-05 07:37:13

标签: elasticsearch indexing contains

我一直在尝试为用户创建自己的索引,其中查询以“name”值为索引。

这是我当前的索引设置:

{
   "users": {
      "settings": {
         "index": {
            "analysis": {
               "filter": {
                  "shingle_filter": {
                     "max_shingle_size": "2",
                     "min_shingle_size": "2",
                     "output_unigrams": "true",
                     "type": "shingle"
                  },
                  "edgeNGram_filter": {
                     "type": "edgeNGram",
                     "min_gram": "1",
                     "max_gram": "20"
                  }
               },
               "analyzer": {
                  "autocomplete_query_analyzer": {
                     "filter": [
                        "standard",
                        "asciifolding",
                        "lowercase"
                     ],
                     "tokenizer": "standard"
                  },
                  "autocomplete_index_analyzer": {
                     "filter": [
                        "standard",
                        "asciifolding",
                        "lowercase",
                        "shingle_filter",
                        "edgeNGram_filter"
                     ],
                     "tokenizer": "standard"
                  }
               }
            },
            "number_of_shards": "1",
            "number_of_replicas": "1"
         }
      }
   }
}

和我的映射:

{
   "users": {
      "mappings": {
         "data": {
            "properties": {
               "name": {
                  "type": "string",
                  "analyzer": "autocomplete_index_analyzer",
                  "search_analyzer": "autocomplete_query_analyzer"
               }
            }
         }
      }
   }
}

现在我的问题是搜索查询不会返回包含该术语的结果。例如,如果我有一个用户“David”,搜索查询“Da”,“Dav”,“Davi”等将返回值,但搜索“vid”或“avid”将不会返回任何值。

这是因为我在设置中缺少一些价值吗?

1 个答案:

答案 0 :(得分:2)

您需要使用nGram代替edgeNGram。所以只需更改此

即可
              "edgeNGram_filter": {
                 "type": "edgeNGram",
                 "min_gram": "1",
                 "max_gram": "20"
              }

进入这个

              "edgeNGram_filter": {
                 "type": "nGram",             <--- change here
                 "min_gram": "1",
                 "max_gram": "20"
              }

请注意,您需要擦除索引,重新创建索引并再次填充索引。