【问题标题】:ElasticSearch index and search analyzers togetherElasticSearch 索引和搜索分析器一起使用
【发布时间】:2014-12-23 01:39:23
【问题描述】:

我刚刚查看了这个视频 - 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 都应该使用标准分析器,但我得到了不同的结果。为什么?

更新

我在索引中有以下名称集合:“john smith”、“johnathan smith”。

【问题讨论】:

    标签: search elasticsearch full-text-search


    【解决方案1】:

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

    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

    【讨论】:

    • 我在这里更新了代码(输入名称和查询示例)sense.qbox.io/gist/4c8299be570c87f1179f70bfd780a7e9f8d40919。请检查。
    • 您必须保存您的更改并给我一个新链接,我才能看到它们。进行更改并点击右上角的“保存代码”按钮,您将被重定向到新网址。把那个网址粘贴到这里,这样我就可以看到你做了什么(你给我的和我给你的一样,所以我看不到你的变化)。
    猜你喜欢
    • 2014-02-25
    • 2023-03-25
    • 2018-11-09
    • 2014-02-15
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多