【发布时间】:2019-08-27 19:38:26
【问题描述】:
下面是我的映射:
{
"mappings": {
"_doc": {
"properties": {
"text": {
"type": "text",
"fields": {
"raw": {
"type": "keyword",
"normalizer": "case_insensitive"
}
}
}
}
}
}
}
设置如下所示:
{
"settings": {
"index": {
"analysis" : {
"normalizer" : {
"case_insensitive" : {
"filter" : "lowercase"
}
},
"analyzer" : {
"en_std" : {
"type" : "standard",
"stopwords" : "_english_"
}
}
},
}
}
}
以下是我的查询:
{
"query": {
"bool" : {
"must" : {
"query_string" : {
"query" : "hawaii beach 2019",
"analyze_wildcard: true,
"fields": [
"text"
]
}
},
}
}
}
以下是存储在 Elasticsearch 中的示例数据:
[
{
"text": "blue hawaii hotel"
},
{
"text": "costa beach"
},
{
"text": "white hawaii beach"
},
{
"text": "nice hotel 2019"
},
{
"text": " some 2019 white beach hawaii photo"
},
{
"text": "hawaii vacation 2019"
},
]
如果我的搜索词是hawaii,我会得到三个结果:
[
{
"text": "blue hawaii hotel"
},
{
"text": "white hawaii beach"
},
{
"text": " some 2019 white beach hawaii beach photo"
},
]
如果我的搜索词是hawaii beach,我会得到四个结果:
[
{
"text": "blue hawaii hotel"
},
{
"text": "costa beach"
},
{
"text": "white hawaii beach"
},
{
"text": " some 2019 white beach hawaii photo"
},
]
如果我的搜索词是hawaii beach 2019,我会得到五个结果,它们是:
[
{
"text": "blue hawaii hotel"
},
{
"text": "costa beach"
},
{
"text": "white hawaii beach"
},
{
"text": "nice hotel 2019"
},
{
"text": " some 2019 white beach hawaii photo"
},
]
这是因为每条记录都包含一个搜索文本的单词。这是有道理的,但这并不是我想要的。我希望包含最多匹配词的记录出现在搜索结果的顶部,而包含较少匹配词的记录出现在搜索结果的底部。如何在 Elasticsearch 6.8 中做到这一点?如果这不能实现,也希望仅显示包含最多匹配词的记录作为搜索结果。
如果我的搜索文本是所需的搜索结果,例如hawaii beach 2019:
[
{
"text": " some 2019 white beach hawaii photo" // Contains most matching words.
},
{
"text": "white hawaii beach"
},
{
"text": "blue hawaii hotel" // Contains less matching words.
},
{
"text": "costa beach" // Contains less matching words.
},
{
"text": "nice hotel 2019" // Contains less matching words.
},
]
或
[
{
"text": " some 2019 white beach hawaii photo" // Contains most matching words
},
]
【问题讨论】:
-
我测试了您的配置和查询,并且在我的测试中一切正常。
some white beach hawaii 2019 photo是得分最高的文档,其次是white hawaii beach,依此类推。 -
每个文档的分数是多少?
-
我在 Elasticsearch 中又添加了一条示例记录:
{"text": "hawaii vacation 2019"}。您可以添加这条额外的记录并再次尝试搜索文本hawaii beach 2019吗?恐怕你会先得到hawaii vacation 2019,即使some 2019 white beach hawaii photo包含最多匹配的单词。 -
我的观点是,Elasticsearch 对搜索文本的最后一个词的评分高于包含最多匹配词的文档。
-
您的索引有多少个分片?有多少文件?再说一遍,Elasticsearch 在结果中给你的分数是多少?
标签: elasticsearch elasticsearch-6.8