在下面的文档中搜索“foo bar”将为name*
的累积结果计分,但我想获取最匹配字段的分数(即name1
分数)。
{
"name1": "foo bar",
"name2": "foo",
"name3": "bar"
}
查询例如:
{
"query": {
"filtered": {
"query": {
"bool": {
"should": [{
"match": {
"name1": {
"query": "foo bar"
}
}
}, {
"match": {
"name2": {
"query": "foo bar"
}
}
}, {
"match": {
"name3": {
"query": "foo bar"
}
}
}]
}
}
}
}
}
使用Elasticsearch 2.4
最佳答案
boolean query合并所有子查询的分数。从文档:
The bool query takes a more-matches-is-better approach, so the score from each matching must or should clause will be added together to provide the final _score for each document.
对于您的情况,您想使用另一个联接查询:The dis_max query
A query that generates the union of documents produced by its subqueries, and that scores each document with the maximum score for that document as produced by any subquery, plus a tie breaking increment for any additional matching subqueries.
例:
{
"query": {
"dis_max": {
"queries": [
{
"match": {
"name1": {
"query": "foo bar"
}
}
},
{
"match": {
"name2": {
"query": "foo bar"
}
}
},
{
"match": {
"name3": {
"query": "foo bar"
}
}
}
]
}
}
}
*注意,我是在ES6上编写的,它不再具有
filtered
查询,但我希望您能理解:)希望这可以帮助!
关于elasticsearch - 根据Elasticsearch中的最佳字段匹配进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50138259/