Elasticsearch - 如何在顶部列出完全匹配

som*_*ant 5 search full-text-search elasticsearch

我正在 Elasticsearch 中搜索“Adana”这个词,并希望它出现在顶部,因为它是确切的词,但事实并非如此。

\n\n

相反,“Bilmemne Adana Otel”位居榜首。

\n\n

普通查询没有任何好处,所以我尝试使用“必须”和“应该”进行布尔查询。但它也没有改变任何东西。

\n\n

除此之外,如果我写“Ada”,“Adana”也应该排在第一位。

\n\n

我们怎样才能让它正常工作呢?

\n\n

询问:

\n\n
curl -X GET "localhost:9200/destinations/_search" -H \'Content-Type: \napplication/json\' -d\'\n{\n "query": {\n   "match": {\n     \xe2\x80\x9cName\xe2\x80\x9d: {\n       "query": \xe2\x80\x9cAdana\xe2\x80\x9d\n      }\n    }\n  }\n}\n\'\n
Run Code Online (Sandbox Code Playgroud)\n\n

结果:

\n\n
{\n"took" : 16,\n"timed_out" : false,\n"_shards" : {\n    "total" : 5,\n    "successful" : 5,\n    "skipped" : 0,\n    "failed" : 0\n},\n"hits" : {\n    "total" : 3,\n    "max_score" : 3.255435,\n    "hits" : [\n    {\n        "_index" : "destinations",\n        "_type" : "_doc",\n        "_id" : "10",\n        "_score" : 3.255435,\n        "_source" : {\n        "name" : "Bilmemne Adana Otel"\n        }\n    },\n    {\n        "_index" : "destinations",\n        "_type" : "_doc",\n        "_id" : "4",\n        "_score" : 2.8624198,\n        "_source" : {\n        "name" : "Adana"\n        }\n    },\n    {\n        "_index" : "destinations",\n        "_type" : "_doc",\n        "_id" : "1",\n        "_score" : 2.3216834,\n        "_source" : {\n        "name" : "Adana Airport Otel - Adana"\n        }\n    }\n    ]\n}\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

指数:

\n\n
{\n"settings": {\n    "analysis": {\n    "filter": {\n        "autocomplete_filter": {\n        "type": "edge_ngram",\n        "min_gram": 2,\n        "max_gram": 15\n        }\n    },\n    "analyzer": {\n        "autocomplete": { \n        "type": "custom",\n        "tokenizer": "standard",\n        "filter": [\n            "lowercase",\n            "autocomplete_filter"\n        ]\n        }\n    }\n    }\n},\n"mappings": {\n    "_doc": {\n    "properties": {\n        "name": {\n        "type": "text",\n        "analyzer": "autocomplete", \n        "search_analyzer": "standard" \n        }\n    }\n    }\n}\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Tim*_*Tim 3

如果您希望完全匹配位于顶部,则可以keyword在映射中使用字段。

{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "edge_ngram",
          "min_gram": 3,
          "max_gram": 15
        }
      },
      "analyzer": {
        "autocomplete": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "autocomplete_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "_doc": {
      "properties": {
        "name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          },
          "analyzer": "autocomplete",
          "search_analyzer": "standard"
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后您可以进行在查询中bool使用您的查询。keywordshould

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "Adana"
          }
        }
      ],
      "should": [
        {
          "term": {
            "name.keyword": {
              "value": "Adana"
            }
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这应该将精确匹配推到顶部。