ElasticSearch 索引和搜索分析器一起使用



我刚刚看了这个视频 - https://www.youtube.com/watch?v=7FLXjgB0PQI,得到了一个关于ElasticSearch分析仪的问题。我阅读了官方文档和其他一些关于分析和分析仪的文章,我有点困惑。

例如,我有以下索引配置:

"settings" : {
    "analysis" : {      
      "filter" : {
        "autocomplete" : {
          "type" : "edge_ngram",
          "min_gram" : 1,
          "max_gram" : 20
        }
      },
      "analyzer" : {
        "autocomplete" : {
          "type" : "custom",
          "tokenizer" : "standard",
          "filter" : ["lowercase", "autocomplete"]
        }
      }
    }
  },
  "mappings" : {
    "user" : {
      "properties" : {
        "name" : {
          "type" : "multi_field",
          "fields" : {
            "name" : {
              "type" : "string",
              "analyzer" : "standard"
            },
            "autocomplete" : {
              "type" : "string",
              "index_analyzer" : "autocomplete",
              "search_analyzer" : "standard"
            }
          }
        }
      }
    }
  }

然后我分别执行以下搜索请求:

{
  "match" : {
    "name.autocomplete" : "john smi"
  }
}

而这个:

{
  "match" : {
    "name" : "john smi"
  }
}

如果我理解正确,我必须看到相同的结果,因为在这两种情况下,ES都应该使用标准分析仪,但我得到了不同的结果。为什么?

更新

我在索引中有以下名字集合:"约翰·史密斯"、"乔纳森·史密斯"。

当我尝试您在这里使用所需的"包装"时,我得到了相同的结果。所以首先我创建了一个索引:

curl -XPOST "http://localhost:9200/test_index/" -d'
{
   "settings": {
      "analysis": {
         "filter": {
            "autocomplete": {
               "type": "edge_ngram",
               "min_gram": 1,
               "max_gram": 20
            }
         },
         "analyzer": {
            "autocomplete": {
               "type": "custom",
               "tokenizer": "standard",
               "filter": [
                  "lowercase",
                  "autocomplete"
               ]
            }
         }
      }
   },
   "mappings": {
      "user": {
         "properties": {
            "name": {
               "type": "multi_field",
               "fields": {
                  "name": {
                     "type": "string",
                     "analyzer": "standard"
                  },
                  "autocomplete": {
                     "type": "string",
                     "index_analyzer": "autocomplete",
                     "search_analyzer": "standard"
                  }
               }
            }
         }
      }
   }
}'

然后添加文档:

curl -XPUT "http://localhost:9200/test_index/user/1" -d'
{
    "name": "John Smith"
}'

第一次搜索将生成文档:

curl -XPOST "http://localhost:9200/test_index/user/_search" -d'
{
   "query": {
      "match": {
         "name.autocomplete": "john smith"
      }
   }
}'
...
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.2712221,
      "hits": [
         {
            "_index": "test_index",
            "_type": "user",
            "_id": "1",
            "_score": 0.2712221,
            "_source": {
               "name": "John Smith"
            }
         }
      ]
   }
}

第二个也是如此:

curl -XPOST "http://localhost:9200/test_index/user/_search" -d'
{
   "query": {
      "match": {
         "name": "john smith"
      }
   }
}'
...
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.2712221,
      "hits": [
         {
            "_index": "test_index",
            "_type": "user",
            "_id": "1",
            "_score": 0.2712221,
            "_source": {
               "name": "John Smith"
            }
         }
      ]
   }
}

您的设置还有其他与我在这里所做的不同的地方吗?

这是我用于此问题的代码:

http://sense.qbox.io/gist/4c8299be570c87f1179f70bfd780a7e9f8d40919

最新更新