# Query not returning correct result

**URL:** <https://discuss.elastic.co/t/query-not-returning-correct-result/25068>\
**Category:** Elasticsearch\
**Created:** [July 7, 2015, 5:05pm UTC](https://discuss.elastic.co/t/query-not-returning-correct-result/25068 "2015-07-07T17:05:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zelfapp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zelfapp/32/108888_2.png) [@Zelfapp](https://discuss.elastic.co/u/Zelfapp)\
**Post date:** [July 7, 2015, 5:05pm UTC](https://discuss.elastic.co/t/query-not-returning-correct-result/25068/1 "2015-07-07T17:05:53Z")

</div>

My query below is returning an incorrect result. You can see that the "params" in the result do not perfectly match the terms params in the query. How do I craft my query so that the params must match perfectly?

**The query:**

```
GET _search
{
   "query": {
      "filtered": {
         "filter": {
            "bool": {
               "must": [
                  {
                     "term": {
                        "class": "ezr_ribbon_army"
                     }
                  },
                  {
                     "term": {
                        "award_sku": "401 ARACMR"
                     }
                  },
                  {
                    "terms": {
                      "params": [
                        "bronze-star|1",
                        "branch|army",
                        "type|standard_ribbons",
                        "spacing|flush",
                        "alignment|center",
                        "assembly|assembled",
                        "width|3"
                      ]
                    }
                  },
                  {
                    "term": {
                      "params_count": 7
                    }
                  }
               ]
            }
         }
      }
   }
}

```

**The results:**

```
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "awards.v1",
            "_type": "awards",
            "_id": "AU5pVpCi-IpoJe5I_BhB",
            "_score": 1,
            "_source": {
               "images": [
                  "120445907559bfe269242d0.48849945.png"
               ],
               "award_sku": "401 ARACMR",
               "class": "ezr_ribbon_army",
               "params": [
                  "bronze-star|1",
                  "silver-star|2",
                  "spacing|flush",
                  "alignment|center",
                  "assembly|assembled",
                  "branch|army",
                  "type|standard_ribbons"
               ],
               "device_skus": [
                  "937NP",
                  "914NP"
               ],
               "params_count": 7
            }
         }
      ]
   }
}

```

**The mapping:**

```
{
   "awards.v1": {
      "mappings": {
         "awards": {
            "_all": {
               "enabled": false
            },
            "properties": {
               "award_sku": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "class": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "device_skus": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "images": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "params": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "params_count": {
                  "type": "integer"
               },
               "skus": {
                  "type": "string",
                  "index": "not_analyzed"
               }
            }
         }
      }
   }
}
```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 7, 2015, 5:19pm UTC](https://discuss.elastic.co/t/query-not-returning-correct-result/25068/2 "2015-07-07T17:19:30Z")

</div>

> the "params" in the result do not perfectly match the terms params in the query.

Were you expecting the `terms` filter to be an AND? It's an OR - see [Elastic: Search Results | Elastic](https://www.elastic.co/search?q=terms+filter)

_"Filters documents that have fields that match any of the provided terms"_

---

<div class="post-metadata">

**Author:** ![Zelfapp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zelfapp/32/108888_2.png) [@Zelfapp](https://discuss.elastic.co/u/Zelfapp)\
**Post date:** [July 7, 2015, 6:08pm UTC](https://discuss.elastic.co/t/query-not-returning-correct-result/25068/3 "2015-07-07T18:08:26Z")

</div>

> [@Mark\_Harwood](#):
>
> Were you expecting the `terms` filter to be an AND? It's an OR - see [Elastic: Search Results | Elastic](https://www.elastic.co/search?q=terms+filter)
> 
> _"Filters documents that have fields that match any of the provided terms"_

Thanks for opening my eyes to that. Clearly misread. So to change the default OR behavior of terms if I'm reading the docs correctly ([https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-filter.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-filter.html)) this is the correct way to tell terms to use AND. It seems to be working now. Thank you for the heads up.

```
"terms": {
    "params": [
        "bronze-star|1",
        "branch|army",
        "type|standard_ribbons",
        "spacing|flush",
        "alignment|center",
        "assembly|assembled",
        "width|3"
    ],
    "execution" : "and",
    "_cache": true
}

```

---

<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 6, 2017, 12:03am UTC](https://discuss.elastic.co/t/query-not-returning-correct-result/25068/4 "2017-07-06T00:03:04Z")

</div>


