# Sorting by named query result

**URL:** <https://discuss.elastic.co/t/sorting-by-named-query-result/288168>\
**Category:** Elasticsearch\
**Created:** [November 1, 2021, 6:36pm UTC](https://discuss.elastic.co/t/sorting-by-named-query-result/288168 "2021-11-01T18:36:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![NLSVTN](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nlsvtn/32/84889_2.png) [@NLSVTN](https://discuss.elastic.co/u/NLSVTN)\
**Post date:** [November 1, 2021, 6:36pm UTC](https://discuss.elastic.co/t/sorting-by-named-query-result/288168/1 "2021-11-01T18:36:33Z")

</div>

Is it possible to sort the results of the ES query by the value present in the named query? We have such code in Elasticsearch-dsl:

```auto
def _named_family_sample_q(family_samples_q, family_guid, quality_filters_by_family):
    sample_queries = [family_samples_q]
    quality_q = quality_filters_by_family.get(family_guid)
    if quality_q:
        sample_queries.append(quality_q)

    return Q('bool', must=sample_queries, _name=family_guid)

```

The last line is the most crucial. The query is extremely complex but ultimately we have many hits each containing a matched\_query key with the family\_guid named query name:

`family_guids = list(raw_hit.meta.matched_queries)`

We want all hits returned to be ordered by the names of the queries. If there are two names in the `matched_queries` we need to get it just sorted by first and then second together.

---

<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:** [November 29, 2021, 6:37pm UTC](https://discuss.elastic.co/t/sorting-by-named-query-result/288168/2 "2021-11-29T18:37:04Z")

</div>

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