如何通过score + boost + field对弹性搜索结果进行排序? [英] How to sort elastic search results by score + boost + field?
问题描述
鉴于具有标题,作者和描述的图书索引,我希望生成的搜索结果以这种方式排序:
Given an index of books that have a title, an author, and a description, I'd like the resulting search results to be sorted this way:
- 所有与下载排序的书籍(数字值)匹配的所有图书
- 与按作者排序的作品相匹配的所有图书
- 所有按照下载排列的描述匹配的书籍
我使用下面的搜索查询,但问题是每个条目都有不同因此,通过下载排序不相关。
I use the search query below, but the problem is that each entry has a different score thus making sorting by downloads irrelevant.
例如当搜索项是排序时,标题:弹性搜索中的排序将会高于标题:'postgresql sort is awesome'(因为单词位置)。
e.g. when the search term is 'sorting' - title: 'sorting in elastic search' will score higher than title: 'postgresql sorting is awesome' (because of the word position).
query = QueryBuilders.multiMatchQuery(queryString, "title^16", "author^8", "description^4")
elasticClient.prepareSearch(Index)
.setTypes(Book)
.setQuery(query)
.addSort(SortBuilders.scoreSort())
.addSort(SortBuilders.fieldSort("downloads").order(SortOrder.DESC))
我如何构建我的查询,以便我可以获得所需的书籍排序?
我使用标准分析器,我需要对搜索查询进行分析,另外我也要处理多字搜索查询字符串。
I use standard analysers and I need to the search query to be analysed, also I will have to handle multi-word search query strings.
Thx。
推荐答案
这里需要的是一种基于三个加权字段和一个数字字段来计算分数的方法。排序将总和从两者获得的分数,因为如果其中任何一个太大,它将取代另一个。
因此,通过比赛获得的得分,更好的方法是多次下载。
所以我推荐功能分数查询 -
What you need here is a way to compute score based on three weighted field and a numeric field. Sort will sum the score obtained from both , due to which if either one of them is too large , it will supersede the other. Hence a better approach would be to multiple downloads with the score obtained by the match. So i would recommend function score query -
{
"query": {
"function_score": {
"query": {
"multi_match": {
"query": "sorting",
"fields": [
"title^16",
"author^8",
"description^4"
]
}
},
"function": [
{
"field_value_factor": {
"field": "downloads"
}
}
],
"boost_mode": "multiply"
}
}
}
这将计算基于在所有三个领域。然后将该分数与下载字段中的值相乘以获得最终分数。乘法boost_mode决定函数计算的值是如何与由查询计算的分数一起进行的。
This will compute the score based on all three fields. And then multiply that score with the value in download field to get the final score. The multiply boost_mode decides how the value computed by functions are clubbed together with the score computed by query.
这篇关于如何通过score + boost + field对弹性搜索结果进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!