# Is it possible to return only hits with certain named query in matched\_queries array if that query hitted at least one time?

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-return-only-hits-with-certain-named-query-in-matched-queries-array-if-that-query-hitted-at-least-one-time/176589>\
**Category:** Elasticsearch\
**Created:** [April 12, 2019, 9:14am UTC](https://discuss.elastic.co/t/is-it-possible-to-return-only-hits-with-certain-named-query-in-matched-queries-array-if-that-query-hitted-at-least-one-time/176589 "2019-04-12T09:14:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy\_v](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@andy\_v](https://discuss.elastic.co/u/andy_v)\
**Post date:** [April 12, 2019, 9:14am UTC](https://discuss.elastic.co/t/is-it-possible-to-return-only-hits-with-certain-named-query-in-matched-queries-array-if-that-query-hitted-at-least-one-time/176589/1 "2019-04-12T09:14:08Z")

</div>

Is it possible to make a search with multiple named queries (let's say named q1\_important,q2,q3 etc and all are in _bool_ query's _should_ array) and if there is at least one hit for **exactly q1\_important** query, then return only hits that necessarily contain q1\_important in _ **matched\_queries** _ array?  
I'd like to not make additional unnecessary searches to elasticsearch but it also can't be solved in any good manner in the app's side logic without refactoring.  
Example:

```auto
  "query": {
    "bool": {
      "must": [],
      "must_not": [],
      "should": [
        {
          "multi_match": {
            "query": "some_text",
            "fields": [
              "brand.NAME",
              "brand.PROP"
            ],
            "type": "best_fields",
            "_name": "q1_important"
          }
        },
        {
          "multi_match": {
            "query": "some_other_text",
            "fields": [
              "detail_text",
              "preview_text"
            ],
            "type": "best_fields",
            "_name": "q2"
          },
        {
          "multi_match": {
            "query": "some_3rd_text",
            "fields": [
              "detail_text",
              "preview_text"
            ],
            "type": "best_fields",
            "_name": "q3"
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 10,
  "sort": [],
  "aggs": {}
}

```

So **if there is at least one hit** with q1\_important in _matched\_queries_ then I want to get **only** results with _matched\_queries_ **containing q1\_important** (like [**"q1\_important"**] or [**"q1\_important"** , "q2"] or [**"q1\_important"** , "q2", "q3"] etc. and no results like ["q2"], ["q2", "q3"])  
else  
get all other variants with q2, q3.  
Thanks in advance for any help!

---

<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:** [May 10, 2019, 9:14am UTC](https://discuss.elastic.co/t/is-it-possible-to-return-only-hits-with-certain-named-query-in-matched-queries-array-if-that-query-hitted-at-least-one-time/176589/2 "2019-05-10T09:14:11Z")

</div>

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