# Match only N queries in a large boolean query for a single result

**URL:** <https://discuss.elastic.co/t/match-only-n-queries-in-a-large-boolean-query-for-a-single-result/286754>\
**Category:** Elasticsearch\
**Created:** [October 14, 2021, 4:20pm UTC](https://discuss.elastic.co/t/match-only-n-queries-in-a-large-boolean-query-for-a-single-result/286754 "2021-10-14T16:20:57Z")\
**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:** [October 14, 2021, 4:20pm UTC](https://discuss.elastic.co/t/match-only-n-queries-in-a-large-boolean-query-for-a-single-result/286754/1 "2021-10-14T16:20:57Z")

</div>

I am constructing a query that looks like that:

`Bool(should=[Bool(_name='F002690_sf3789834989482_wgs_hgsc', must=[Bool(should=[Terms(samples_num_alt_1=['S338492778_HGSC', 'S59=89483927_HGSC']), Terms(samples_num_alt_2=['S384938978_HGSC', 'S545840927_HGSC']), Terms(samples=['S9043902778_HJKC', 'S5940394027_HJKC'])])]), Bool(_name='F002706_sf67890290_wgs_hgsc', must=[Bool(should=[Terms(samples_num_alt_1=['S3904340491_HGSC', 'S3950490_HGSC']), Terms(samples_num_alt_2=['S35904991_HGSC', 'S39504950_HGSC']), Terms(samples=['S3905401_HGSC', 'S35004900_HGSC'])])]), Bool(_name='F002819_sf5994095721_wgs_hgsc', must=[Bool(should=[Terms(samples_num_alt_1=['S20495049_HGSC', 'S549905499_HGSC', 'S57190549_HGSC', 'S710905949_HGSC']), ...`

A very long one. And I need to somehow return results where there are more than N number of \_name is present (matched\_queries). I already posted a question that was answered:

> [@Filter Elasticsearch results based on the number of 'matched\_queries'](https://discuss.elastic.co/t/filter-elasticsearch-results-based-on-the-number-of-matched-queries/286741):
>
> There is such a thing in elasticsearch as named queries : When we use it, we get matched\_queries array together with each matched document in each elasticsearch hit. Now, I want to get only documents that matched \> n queries. Is it possible to do and how?

So, I tried to use 'minimum\_should\_match' parameter like so (showing only the end of this large query, see **minimum\_should\_match** at the end of it):

`... Bool(_name='F007276_sf3014344550_wgs_hgsc', must=[Bool(should=[Terms(samples_num_alt_1=['S1643442_HGSC']), Terms(samples_num_alt_2=['S1605452_HGSC']), Terms(samples=['S1545142_HGSC'])])]), Bool(_name='F007254_sf545435_wgs_hgsc', must=[Bool(should=[Terms(samples_num_alt_1=['S2600119_HGSC']), Terms(samples_num_alt_2=['S2605459_HGSC']), Terms(samples=['S2545119_HGSC'])])]), Bool(_name='F007254_sf46454547_wgs_hgsc', must=[Bool(should=[Terms(samples_num_alt_1=['S45854555_HGSC']), Terms(samples_num_alt_2=['S44545155_HGSC']), Terms(samples=['S4545155_HGSC'])])])])], minimum_should_match=10)`

and it runs fine but it does not produce the desired results of limiting the number of matched **\_name** queries. Here is how it is constructed in **elasticsearch-dsl** :

```auto
for family_samples_q in qs:
  genotypes_q |= family_samples_q

Search().query('bool', filter=[genotypes_q], minimum_should_match=10)

```

I suspect that it does not produce correct filtering because `minimum_should_match` is applied to the outer scope and not to inner one (not to `genotypes_q`, but to `filter` itself) but I am not sure how to change that if thats the case.

Basically what I need is to return only results that contain _**N (or more) number of \_name queries matched**_. Single results, so I am not interested in the whole query matching N number results, but each single result matching N number of them.

---

<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 11, 2021, 4:21pm UTC](https://discuss.elastic.co/t/match-only-n-queries-in-a-large-boolean-query-for-a-single-result/286754/2 "2021-11-11T16:21:43Z")

</div>

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