Elastic - Desc sort is not working anymore when do

2019-08-28 23:23发布

问题:

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:

Try changing the values of boost to tens or hundreds and see if that would resolve the issue for now as you always want the result of 0 and 1 to be available on the top. For e.g.

POST <your_index_name>/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "flag_type": "contract"
          }
        }
      ],
      "should": [
        {
          "match": {
            "timeInt": {
              "query": "0",
              "boost": 1000
            }
          }
        },
        {
          "match": {
            "timeInt": {
              "query": "1",
              "boost": 900
            }
          }
        }
      ]
    }
  },
  "sort": [
    { "_score": "desc"    },
    { 
      "timeInt": {
        "order": "desc"
      }
    }
  ]
}

I would suggest you to take a step back and understand boost is applied on what? What are the underlying concepts on which Elasticsearch calculates the ranking. What does _score in the response indicate?

These are the links which can help:

https://qbox.io/blog/practical-guide-elasticsearch-scoring-relevancy

https://www.elastic.co/blog/easier-relevance-tuning-elasticsearch-7-0

https://www.elastic.co/blog/practical-bm25-part-1-how-shards-affect-relevance-scoring-in-elasticsearch

Let me know if you have any queries!!