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

我正在Elasticsearch中搜索“Adana”这个词,并希望它能够排在最前面,因为它是确切的词,但事实并非如此.

相反,“Bilmemne Adana Otel”名列前茅.

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

除此之外,如果我写“阿达”,“阿达纳”也应该首先出现.

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

查询:

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" 
        }
    }
    }
}
}

最佳答案 如果您希望完全匹配,则可以在映射中使用关键字字段.

{
  "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查询中使用您的关键字的bool查询.

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

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

点赞