gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:52:35 25 4
gpt4 key购买 nike

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

相反,“Bilmemne Adana Otel”位于顶部。

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

除此之外,如果我写“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"
}
}
}
}
}

最佳答案

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

然后你可以有一个 bool 查询,它在 should 查询中使用你的 keyword

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

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

关于Elasticsearch - 如何在顶部列出完全匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52667657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com