问题
So, i'm doing sort with priority that already solved on my previous post: Elastic - Sorting value with priority
But i found the new problem, when i want to filter data with specified field, the query sort of timeInt doesn't work anymore
I've tried this query, but the query sort of timeInt doesn't work. Here is my query:
{
query: {
bool: {
must: {
match: {
'flag_type': "contract"
}
},
should: [
{
match: {
timeInt: {
query: 0,
boost: 3
}
}
},
{
match: {
timeInt: {
query: 1,
boost: 2
}
}
}
]
}
},
sort: [
{ _score: "desc"},
{
timeInt: {
order: "desc"
}
}
]
}
NOTE: If i delete the query sort: { _score: "desc" }
desc sort is working properly, but can't boost up value 0 / 1 to the top.
Expected result:
0, 1, 100, 99, 98, etc...
Current result:
If i delete { _score: "desc" }
:
100, 99, 98, 97, 96, etc...
With query above: 0, 1, 15, 99, 100, 70, 2, etc...
What's wrong with my query?
Please help me
Thank you.
回答1:
Your query means: first, sort by score. If several documents have the same score, then sort by the 'timeInt' field among them. In Elasticsearch's view the result is correct.
回答2:
Apologies for the delay in reply, here is what you can do additionally on top of having the boosting logic.
I've created a custom sorting logic. So basically what this would do is, it would first sort the results based on _score
and then based on sort logic of timeInt
field, the results would get sorted.
Since I've boosted 0
and 1
and that the values of boost as well as their values in sorting logic based on custom sort I've implemented, would always return 0
and 1
as per what you are looking for.
This should help you get what you are looking for.
POST myintindex/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"flag_type": "contract"
}
}
],
"should": [
{
"match": {
"timeInt": {
"query": "0",
"boost": 200000
}
}
},
{
"match": {
"timeInt": {
"query": "1",
"boost": 10000
}
}
}
]
}
},
"sort":[
{ "_score": {"order": "desc"}},
{
"_script":{
"type":"number",
"script":{
"lang":"painless",
"source":"""
String st = String.valueOf(doc['timeInt'].value);
if(params.scores.containsKey(st)) {
return params.scores[st];
}
return doc['timeInt'].value;
""",
"params":{
"scores":{
"0":200000,
"1":100000
}
}
},
"order":"desc"
}
}
]
}
Once again, I apologise it took me this long to respond, but I hope this helps!
来源:https://stackoverflow.com/questions/57543050/elastic-desc-sort-is-not-working-anymore-when-doing-boost-query-must-query