【问题标题】:Elasticsearch - How to list exact match at the topElasticsearch - 如何在顶部列出完全匹配
【发布时间】:2019-03-11 01:57:39
【问题描述】:

我在 Elasticsearch 中搜索“Adana”这个词,并希望它排在最前面,因为它是准确的词,但它不是。

取而代之的是“Bilmemne Adana Otel”。

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

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

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

查询:

curl -X GET "localhost:9200/destinations/_search" -H 'Content-Type: 
application/json' -d'
{
 "query": {
   "match": {
     “Name”: {
       "query": “Adana”
      }
    }
  }
}
'

结果:

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

索引:

{
"settings": {
    "analysis": {
    "filter": {
        "autocomplete_filter": {
        "type": "edge_ngram",
        "min_gram": 2,
        "max_gram": 15
        }
    },
    "analyzer": {
        "autocomplete": { 
        "type": "custom",
        "tokenizer": "standard",
        "filter": [
            "lowercase",
            "autocomplete_filter"
        ]
        }
    }
    }
},
"mappings": {
    "_doc": {
    "properties": {
        "name": {
        "type": "text",
        "analyzer": "autocomplete", 
        "search_analyzer": "standard" 
        }
    }
    }
}
}

【问题讨论】:

    标签: elasticsearch search full-text-search


    【解决方案1】:

    如果您希望完全匹配位于顶部,可以在映射中使用 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"
            }
          }
        }
      }
    }
    

    然后您可以在 should 查询中使用 keywordbool 查询。

    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "name": "Adana"
              }
            }
          ],
          "should": [
            {
              "term": {
                "name.keyword": {
                  "value": "Adana"
                }
              }
            }
          ]
        }
      }
    }
    

    这应该将完全匹配推到顶部。

    【讨论】:

    • 这行得通,但如果我搜索“Ada”,现在的顺序是:“Adana Airport Otel - Adana”、“Bilmemne Adana Otel”和“Adana”使其最后。即使写“Ada”,我们能否做到这一点,“Adana”将再次出现在第一位。
    猜你喜欢
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    相关资源
    最近更新 更多