我创建了一个非常简单的测试索引,包含以下 5 个条目:
{ "tags": [ { "topics": "music festival dance techno germany"} ]}
{ "tags": [ { "topics": "music festival dance techno"} ]}
{ "tags": [ { "topics": "music festival dance"} ]}
{ "tags": [ { "topics": "music festival"} ]}
{ "tags": [ { "topics": "music"} ]}
然后我执行了以下查询:
{
"query": {
"bool": {
"should": [
{ "match": { "tags.topics": "music festival"}}
]
}
}
}
期望在结果中得到如下顺序:
1)“音乐节”
2)“音乐节舞会”
3)“音乐节电子舞曲”
4)“德国电子音乐节舞蹈”
5)“音乐”
考虑字段长度规范化。
但是我得到了以下信息:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 0.5753642,
"hits": [
{
"_index": "testindex",
"_type": "entry",
"_id": "1",
"_score": 0.5753642,
"_source": {
"tags": [
{
"topics": "music festival dance techno germany"
}
]
}
},
{
"_index": "testindex",
"_type": "entry",
"_id": "3",
"_score": 0.5753642,
"_source": {
"tags": [
{
"topics": "music festival dance"
}
]
}
},
{
"_index": "testindex",
"_type": "entry",
"_id": "4",
"_score": 0.42221835,
"_source": {
"tags": [
{
"topics": "music festival"
}
]
}
},
{
"_index": "testindex",
"_type": "entry",
"_id": "2",
"_score": 0.32088596,
"_source": {
"tags": [
{
"topics": "music festival dance techno"
}
]
}
},
{
"_index": "testindex",
"_type": "entry",
"_id": "5",
"_score": 0.2876821,
"_source": {
"tags": [
{
"topics": "music"
}
]
}
}
]
}
}
除了只匹配一个单词的最低分数外,其顺序似乎完全随机。
可能是什么原因导致的,我可以更改什么(在映射、索引或搜索期间)以获得预期的顺序?
注意:非完美匹配查询也是如此。搜索“music dance”应该仍会产生 3 个词条目作为第一个结果,因此使用或提升词条查询似乎是不可能的。
最佳答案
正如我在 this answer 中所描述的那样评分/相关性不是 Elasticsearch 中最简单的主题。
我正试图为您找出解决方案,目前我有类似的解决方案。
文件:
{ "tags": [ { "topics": ["music", "festival", "dance", "techno", "germany"]} ], "topics_count": 5 }
{ "tags": [ { "topics": ["music", "festival", "dance", "techno"]} ], "topics_count": 4 }
{ "tags": [ { "topics": ["music", "festival", "dance"] } ], "topics_count": 3 }
{ "tags": [ { "topics": ["music", "festival"]} ], "topics_count": 2 }
{ "tags": [ { "topics": ["music"]} ], "topics_count": 1 }
和查询:
{
"query": {
"bool": {
"should": [
{
"function_score": {
"query": {
"terms_set": {
"tags.topics" : {
"terms" : ["music", "festival"],
"minimum_should_match_script": {
"source": "params.num_terms"
}
}
}
},
"script_score" : {
"script" : {
"source": "_score * Math.sqrt(1.0 / doc['topics_count'].value)"
}
}
}
},
{
"function_score": {
"query": {
"terms_set": {
"tags.topics" : {
"terms" : ["music", "festival"],
"minimum_should_match_script": {
"source": "doc['topics_count'].value"
}
}
}
},
"script_score" : {
"script" : {
"source": "_score * Math.sqrt(1.0 / doc['topics_count'].value)"
}
}
}
}
]
}
}
}
它并不完美。仍然需要一些改进。在这个例子中,它适用于 ["music", "festival"]
和 ["music", "dance"]
(在 ES 6.2 上测试过),但我猜测在其他结果上它不会像您预期的那样 100% 起作用。主要是因为相关性/评分的复杂性。但您现在可以阅读更多关于我使用的东西并尝试改进它。
关于elasticsearch - 如何使 Elasticsearch 评分考虑字段长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49973358/