下面是我的映射:
{
"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
},
]