Elasticsearch - 如何在顶部列出完全匹配
Elasticsearch - How to list exact match at the top
我正在 Elasticsearch 中搜索单词 "Adana" 并希望它出现在顶部,因为它是确切的单词,但它没有。
而是 "Bilmemne Adana Otel" 排在最前面。
普通查询没有任何用处,所以我尝试使用 "must" 和 "should" 进行布尔查询。但它也没有改变任何东西。
除此之外,如果我写 "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 中搜索单词 "Adana" 并希望它出现在顶部,因为它是确切的单词,但它没有。
而是 "Bilmemne Adana Otel" 排在最前面。
普通查询没有任何用处,所以我尝试使用 "must" 和 "should" 进行布尔查询。但它也没有改变任何东西。
除此之外,如果我写 "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"
}
}
}
]
}
}
}
那应该将完全匹配推到顶部。