# Matched Queries not always functioning

**URL:** <https://discuss.elastic.co/t/matched-queries-not-always-functioning/97241>\
**Category:** Elasticsearch\
**Created:** [August 16, 2017, 10:58am UTC](https://discuss.elastic.co/t/matched-queries-not-always-functioning/97241 "2017-08-16T10:58:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddorian43](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ddorian43/32/36093_2.png) [@ddorian43](https://discuss.elastic.co/u/ddorian43)\
**Post date:** [August 16, 2017, 10:58am UTC](https://discuss.elastic.co/t/matched-queries-not-always-functioning/97241/1 "2017-08-16T10:58:56Z")

</div>

Using ES 5.2.0.

When I do:

```auto
{'query': {'bool': {'filter': {'bool': {'should': [{'bool': {'filter': {'bool': {'should': [{'term': {'title_raw': {'value': 'stylebubble', '_name': 'stylebubble'}}}], 'must': ({'type': {'value': 'editorial_source'}},)}}}}]}}}}}

```

I get the right return with matched-queries:

```auto
{u'hits': {u'hits': [{u'_score': 0.0, u'_type': u'editorial_source', u'_id': u'3a34655e-84d4-11e6-a5d1-3b1a0960c5bb', u'matched_queries': [u'stylebubble'], u'_index': u'triangulr'}], u'total': 1, u'max_score': 0.0}, u'_shards': {u'successful': 1, u'failed': 0, u'total': 1}, u'took': 11, u'timed_out': False}

```

But if I do multiple matched-queries ( i'm searching for 5 different types), the first one no longer works (but all the other work, while I'm pasting only 2 here):

```auto
{'query': {'bool': {'filter': {'bool': {'should': [{'bool': {'filter': {'bool': {'should': [{'term': {'title_raw': {'value': 'stylebubble', '_name': 'stylebubble'}}}], 'must': ({'type': {'value': 'editorial_source'}},)}}}}, {'bool': {'filter': {'bool': {'should': [{'term': {'name_raw': {'value': 'stylebubble', '_name': 'stylebubble'}}}, {'term': {'slug': {'value': 'stylebubble', '_name': 'stylebubble'}}}, {'term': {'aliases': {'value': 'stylebubble', '_name': 'stylebubble'}}}], 'must': ({'type': {'value': 'brand'}}, {'range': {'visible_product_count': {'gte': 5}}})}}}}]}}}}}

```

Response:

```auto
u'hits': {u'hits': [{u'_score': 0.0, u'_type': u'editorial_source', u'_id': u'3a34655e-84d4-11e6-a5d1-3b1a0960c5bb', u'_index': u'triangulr'}], u'total': 1, u'max_score': 0.0}, u'_shards': {u'successful': 1, u'failed': 0, u'total': 1}, u'took': 6, u'timed_out': False}

```

What am I doing wrong or is it just a bug ?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 16, 2017, 1:12pm UTC](https://discuss.elastic.co/t/matched-queries-not-always-functioning/97241/2 "2017-08-16T13:12:04Z")

</div>

can you please provide a full reproduction including documents, index mappings and if possible HTTP or kibana console calls? This way other readers would at least have the possibility to reproduce your issue.

---

<div class="post-metadata">

**Author:** ![ddorian43](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ddorian43/32/36093_2.png) [@ddorian43](https://discuss.elastic.co/u/ddorian43)\
**Post date:** [August 28, 2017, 5:58pm UTC](https://discuss.elastic.co/t/matched-queries-not-always-functioning/97241/3 "2017-08-28T17:58:40Z")

</div>

Can you try this python script ? You will see that the 2nd search doesn't have the `matched_queries` attribute.

Assuming you have `ES 5.2` listening on default `9200` port:::

```auto
    from elasticsearch import Elasticsearch
    es = Elasticsearch()
    the_index = {
        'settings': {'number_of_shards': 1, 'number_of_replicas': 0, 'refresh_interval': 0},
        'mappings': {
            'test': {
                'properties': {
                    'field1': {'type': 'keyword'},
                    'field2': {'type': 'keyword'},
                    'field3': {'type': 'keyword'},
                }
            }
        }
    }
    if es.indices.exists('the_test'):
        es.indices.delete('the_test')
        es.indices.create('the_test', body=the_index)
    es.index('the_test', 'test', {'field1': ['bum'], 'field2': 'bum', 'field3': '#bum'}, refresh=True)
    print(es.search('the_test', body={'query':{'bool': {'filter': {'bool':{'must':[{'term': {'field1': {'value': 'bum', '_name': 'bum'}}}]}}}}}, _source=False))
    print(es.search('the_test', body={'query':{'bool': {'filter': {'bool':{'should':[
        {'term': {'field1': {'value': 'bum', '_name': 'bum'}}},
        {'term': {'field2': {'value': 'bum', '_name': 'bum'}}},
        {'term': {'field3': {'value': 'bum', '_name': 'bum'}}},
    ]}}}}}, _source=False))

```

Seems like a bug, no ?

Thanks

---

<div class="post-metadata">

**Author:** ![ddorian43](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ddorian43/32/36093_2.png) [@ddorian43](https://discuss.elastic.co/u/ddorian43)\
**Post date:** [August 29, 2017, 10:23am UTC](https://discuss.elastic.co/t/matched-queries-not-always-functioning/97241/4 "2017-08-29T10:23:21Z")

</div>

Explanation + fix : [https://github.com/elastic/elasticsearch/issues/26417](https://github.com/elastic/elasticsearch/issues/26417)

---

<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:** [September 26, 2017, 10:23am UTC](https://discuss.elastic.co/t/matched-queries-not-always-functioning/97241/5 "2017-09-26T10:23:29Z")

</div>

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