索引上的弹性搜索排序设置给出了奇怪的结果



我有一个这样的索引设置:

PUT items
{
"settings": {
"index": {
"sort.field": ["popularity", "title_keyword"],
"sort.order": ["desc", "asc"]
},
"analysis": {
"analyzer": {
"autocomplete": {
"tokenizer": "autocomplete",
"filter": [
"lowercase"
]
},
"autocomplete_search": {
"tokenizer": "lowercase"
}
},
"tokenizer": {
"autocomplete": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 15,
"token_chars": [
"letter"
]
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "autocomplete_search"
},
"title_keyword": {
"type": "keyword"
},
"popularity": {
"type": "integer"
},
"visibility": {
"type": "keyword"
}
}
}
}

使用以下数据:

POST items/_doc/1
{
"title": "The Arbor",
"popularity": 5,
"title_keyword": "The Arbor",
"visibility": "public"
}
POST items/_doc/2
{
"title": "The Canon",
"popularity": 10,
"title_keyword": "The Canon",
"visibility": "public"
}
POST items/_doc/3
{
"title": "The Brew",
"popularity": 15,
"title_keyword": "The Brew",
"visibility": "public"
}

我对数据运行此查询:

GET items/_search
{
"size": 3,
"query": {
"bool": {
"must": [
{
"match": {
"title": {
"query": "the", 
"operator": "and"
}
}
},
{
"match": {
"visibility": "public"
}
}
]
}
},
"highlight": {
"pre_tags": ["<mark>"],
"post_tags": ["</mark>"], 
"fields": {
"title": {}
}
}
}

它似乎正确匹配了单词the上的记录,但排序似乎不起作用。 我希望它按定义的受欢迎程度排序,结果将按该顺序The ArborThe BrewThe Canon,但我得到的结果如下:

{
"took" : 11,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.27381438,
"hits" : [
{
"_index" : "items",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.27381438,
"_source" : {
"title" : "The Brew",
"popularity" : 15,
"title_keyword" : "The Brew",
"visibility" : "public"
},
"highlight" : {
"title" : [
"<mark>The</mark> Brew"
]
}
},
{
"_index" : "items",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.26392496,
"_source" : {
"title" : "The Arbor",
"popularity" : 5,
"title_keyword" : "The Arbor",
"visibility" : "public"
},
"highlight" : {
"title" : [
"<mark>The</mark> Arbor"
]
}
},
{
"_index" : "items",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.26392496,
"_source" : {
"title" : "The Canon",
"popularity" : 10,
"title_keyword" : "The Canon",
"visibility" : "public"
},
"highlight" : {
"title" : [
"<mark>The</mark> Canon"
]
}
}
]
}
}

在创建索引时定义排序字段和顺序时,在设置下是否会自动对结果进行排序? 它似乎是按分数而不是受欢迎程度排序的。 如果我在查询中包含排序选项,它会给我正确的结果:

GET items/_search
{
"size": 3,
"sort": [
{
"popularity": {
"order": "desc"
}
},
{
"title_keyword": {
"order": "asc"
}
}
], 
"query": {
"bool": {
"must": [
{
"match": {
"title": {
"query": "the", 
"operator": "and"
}
}
},
{
"match": {
"visibility": "public"
}
}
]
}
},
"highlight": {
"pre_tags": ["<mark>"],
"post_tags": ["</mark>"], 
"fields": {
"title": {}
}
}
}

我读到像这样在查询中包含排序是低效的,并将其包含在设置中。 我在创建索引时没有做一些事情来使其默认按受欢迎程度排序吗? 在查询中包含排序选项是否会导致查询效率低下? 还是我实际上需要将其包含在每个查询中?

希望这是有道理的! 谢谢

索引排序定义了如何在分片中对区段进行排序,这与搜索结果的排序无关。您可以使用排序索引,如果您经常有使用相同条件排序的搜索,则索引排序会加快搜索速度。

如果搜索的排序与索引不同或根本没有排序,则索引排序不相关。

请参阅索引排序的文档,尤其是解释如何使用索引排序的部分。

最新更新