在 Elasticsearch 中根据匹配的最佳字段进行搜索
Search according to the best field match in Elasticsearch
在下面的文档中搜索 "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
查询,但我希望你能理解要点 :)
希望对您有所帮助!
在下面的文档中搜索 "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
查询,但我希望你能理解要点 :)
希望对您有所帮助!