# Possible to sort and filter on length(matched\_queries)?

**URL:** <https://discuss.elastic.co/t/possible-to-sort-and-filter-on-length-matched-queries/56556>\
**Category:** Elasticsearch\
**Created:** [July 27, 2016, 7:12pm UTC](https://discuss.elastic.co/t/possible-to-sort-and-filter-on-length-matched-queries/56556 "2016-07-27T19:12:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjbottaro](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjbottaro/32/11055_2.png) [@cjbottaro](https://discuss.elastic.co/u/cjbottaro)\
**Post date:** [July 27, 2016, 7:12pm UTC](https://discuss.elastic.co/t/possible-to-sort-and-filter-on-length-matched-queries/56556/1 "2016-07-27T19:12:39Z")

</div>

Hello. I'm using named queries which generates a "field" on returned documents called `matched_queries` which is an array of the names of the queries that matched.

Is it possible to sort and filter on the cardinality of `matched_queries`?

Or perhaps use scripting to make a field `match_queries_length` that can then be sorted / filtered on?

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![cjbottaro](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjbottaro/32/11055_2.png) [@cjbottaro](https://discuss.elastic.co/u/cjbottaro)\
**Post date:** [July 27, 2016, 10:20pm UTC](https://discuss.elastic.co/t/possible-to-sort-and-filter-on-length-matched-queries/56556/2 "2016-07-27T22:20:55Z")

</div>

Figured it out.

If I construct my query such that nothing contributes to scoring except the named queries, then I can simply sort by document score and it will have the desired effect. 🙂

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [July 27, 2016, 10:34pm UTC](https://discuss.elastic.co/t/possible-to-sort-and-filter-on-length-matched-queries/56556/3 "2016-07-27T22:34:15Z")

</div>

Hey! Great! Any day you can maneuver your problem into "sort by score" is a good day.

---

<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:** [July 5, 2017, 10:32pm UTC](https://discuss.elastic.co/t/possible-to-sort-and-filter-on-length-matched-queries/56556/4 "2017-07-05T22:32:21Z")

</div>


