admin管理员组文章数量:1302557
Let's say I have a query that looks something like this:
"query": {
"bool": {
"must": [
{
"bool": {
"_name": "fooMatches",
"minimum_should_match":1,
"should": [
{
"terms": {
"foo":["a"]
}
},
{
"terms": {
"foo":["b"]
}
},
{
"terms": {
"foo":["c"]
}
},
{
"terms": {
"foo":["d"]
}
}
]
}
},
// multiple other clauses
]
}
}
It would give results maybe like this:
"hits": [
{
"_score": 5,
...
"matched_queries": {
"fooMatches": 3
}
}
]
Indicating that it matched 3 of the terms in foo
, but it matched some other clauses to give us an eventual score of 5
.
The results of the query are sorted by _score
by default, but I would like to sort them by fooMatches
under matched_queries
. Is that possible? What would be the correct sort
to add?
Something like:
"sort": [
{ "fooMatches._score":"desc" },
"_score"
]
And several variants I've tried on that don't work. So how would you do that?
Edit: I would like to be able to sort ties by the rest of the query, so I know I could just put everything except this bit in a filter, which is okay, but not perfect.
Let's say I have a query that looks something like this:
"query": {
"bool": {
"must": [
{
"bool": {
"_name": "fooMatches",
"minimum_should_match":1,
"should": [
{
"terms": {
"foo":["a"]
}
},
{
"terms": {
"foo":["b"]
}
},
{
"terms": {
"foo":["c"]
}
},
{
"terms": {
"foo":["d"]
}
}
]
}
},
// multiple other clauses
]
}
}
It would give results maybe like this:
"hits": [
{
"_score": 5,
...
"matched_queries": {
"fooMatches": 3
}
}
]
Indicating that it matched 3 of the terms in foo
, but it matched some other clauses to give us an eventual score of 5
.
The results of the query are sorted by _score
by default, but I would like to sort them by fooMatches
under matched_queries
. Is that possible? What would be the correct sort
to add?
Something like:
"sort": [
{ "fooMatches._score":"desc" },
"_score"
]
And several variants I've tried on that don't work. So how would you do that?
Edit: I would like to be able to sort ties by the rest of the query, so I know I could just put everything except this bit in a filter, which is okay, but not perfect.
Share edited Feb 10 at 20:42 Matt Burland asked Feb 10 at 20:24 Matt BurlandMatt Burland 45.2k18 gold badges107 silver badges179 bronze badges1 Answer
Reset to default 1Tldr;
I don't think you can access those values. But maybe you can work around this limitation via boosting ?
Solution
In order to first sort by Foo, then by Bar. I would boost Foo by an order or magnitude (10) compared to Bar.
This way, you would be:
- sorting on _score
- solving ties
{
"query": {
"bool": {
"should": [
{
"bool": {
"_name": "foo",
"boost": 10,
"should": [
{
"terms": {
"tags.keyword": [
"a"
]
}
},
{
"terms": {
"tags.keyword": [
"b"
]
}
}
]
}
},
{
"bool": {
"_name": "bar",
"boost": 1,
"should": [
{
"terms": {
"tags.keyword": [
"c"
]
}
}
]
}
}
]
}
}
}
a
,b
,c
would give 21b
,c
=> 11b
,d
=> 10c
,d
=> 1
本文标签: elasticsearchSort by matchedqueries scoreStack Overflow
版权声明:本文标题:elasticsearch - Sort by matched_queries score - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741692040a2392783.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论