# Combine rescore and sort in a single query

**URL:** <https://discuss.elastic.co/t/combine-rescore-and-sort-in-a-single-query/169004>\
**Category:** Elasticsearch\
**Created:** [February 19, 2019, 10:57am UTC](https://discuss.elastic.co/t/combine-rescore-and-sort-in-a-single-query/169004 "2019-02-19T10:57:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![justine](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@justine](https://discuss.elastic.co/u/justine)\
**Post date:** [February 19, 2019, 10:57am UTC](https://discuss.elastic.co/t/combine-rescore-and-sort-in-a-single-query/169004/1 "2019-02-19T10:57:49Z")

</div>

**Problem**  
What is a good alternative query if I need both a rescore and a sort? I know combining both is not supported (the rescore query is not executed with the Elasticsearch 5.4 version I am using, and the documentation states that it raises an exception with Elasticsearch 6.x) but I can't come up with a single query that would approach the behavior I am expecting.

**Example**  
Let's say the documents in my index represent books with fields `title`, `description` and `genre`. I would like to have a primary query to match on the `title` field, then a rescore query to match on the `description` field only for the top 500 documents, and finally a sort that involves both the `genre` field (to sort the results based on a custom ordering of the different genre values) and the rescored score.

I first tried using the following query:

```auto
{
  "query": {
    "match": {
      "title": "birds"
    }
  },
  "rescore": {
    "query": {
      "query_weight": 1.0,
      "rescore_query": {
        "match": {
          "description": "colorful"
        }
      },
      "rescore_query_weight": 1.0
    },
    "window_size": 500
  },
  "sort": [
    {
      "_script": {
        "order": "asc",
        "script": {
          "params": {
            "Fantasy": 0,
            "Thriller": 1,
            "Detective": 2
          },
          "source": "params[doc['genre.keyword'].value]"
        },
        "type": "number"
      }
    },
    {
      "_score": "desc"
    }
  ]
}

```

**Not satisfactory workaround**  
Because rescore and sort are not compatible, I ended up using three different rescore-only queries, one for each genre (Fantasy, Thriller and Detective) and concatenating the results in the expected order. But this solution has several drawbacks:

- it's more time consuming as I send several queries to Elasticsearch
- it's harder to make use of Elasticsearch capabilities for pagination of the results
- it requires code outside Elasticsearch

In my real-life use case, I don't want to bring the rescore query content to the primary query and use sort only, because my primary query returns many documents and the rescore query takes too much time to calculate scores on every returned document...

Thank you in advance for any advice on how to deal with rescoring and sorting at the same time!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 19, 2019, 10:57am UTC](https://discuss.elastic.co/t/combine-rescore-and-sort-in-a-single-query/169004/2 "2019-03-19T10:57:51Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
