Skip to content Skip to sidebar Skip to footer

Elastic - Desc Sort Is Not Working Anymore When Doing Boost Query + Must Query

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 specifi

Solution 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.

Solution 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!

Post a Comment for "Elastic - Desc Sort Is Not Working Anymore When Doing Boost Query + Must Query"