# Inconsistent Term and Terms query resolution in filter context

**URL:** https://discuss.elastic.co/t/inconsistent-term-and-terms-query-resolution-in-filter-context/313884
**Category:** Elasticsearch
**Created:** [September 7, 2022, 12:45pm UTC](https://discuss.elastic.co/t/inconsistent-term-and-terms-query-resolution-in-filter-context/313884 "2022-09-07T12:45:53Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![robertasg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robertasg/32/110594_2.png) [@robertasg](https://discuss.elastic.co/u/robertasg)
#### Post date: [September 7, 2022, 12:45pm UTC](https://discuss.elastic.co/t/inconsistent-term-and-terms-query-resolution-in-filter-context/313884/1 "2022-09-07T12:45:53Z")

</div>

According to Elasticsearch [docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html#terms-query-ex-request) it says that TermsQuery is the same as a TermQuery but can be operate with multiple values.

> The `terms` query is the same as the [`term` query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html), except you can search for multiple values.

However I see slightly different behaviour when inspecting the profiler.

It seems that it behaves correctly when it's the only filter, however if it's more than one filter I get different results.

**TermQuery** :

```auto
{
  "query":{
    "bool" : {
      "filter" : [
        {
          "term" : {
            "product_classifications.category_id" : "1604"
          }
        },
        {
          "range" : {
            "updated_at" : {
              "gte" : "now-28d/d"
            }
          }
        }
      ]
    }
  },
  "size": 10
}

```

Results in:

```auto
// ...
{
  "type" : "TermQuery",
  "description" : "product_classifications.category_id:1604",
  "time_in_nanos" : 966792,
  "breakdown" : { 
  // ... 
  }
},

```

**TermsQuery**

```auto
{
  "profile": true,
  "query":{
    "bool" : {
      "filter" : [
        {
          "terms" : {
            "product_classifications.category_id" : ["1604"]
          }
        },
        {
          "range" : {
            "updated_at" : {
              "gte" : "now-28d/d"
            }
          }
        }
      ]
    }
  },
  "size": 10
}

```

Results in:

```auto
{
// ...
  "type" : "ConstantScoreQuery",
  "description" : "ConstantScore(product_classifications.category_id:1604)",
  "time_in_nanos" : 1893944,
  "breakdown" : { 
    // ... 
  },
  "children" : [
    {
      "type" : "TermQuery",
      "description" : "product_classifications.category_id:1604",
      "time_in_nanos" : 940813,
      "breakdown" : {
      // ...
      }
    }
  ]
}

```

So what we can see here is that TermsQuery gets wrapped in ConstantScoreQuery while TermQuery does not.

Is this expected behavior?

Whenever I only have one filter the behavior seems consistent, both end up providing ConstantScoreQuery with a boost of 0.

The mapping for category\_id here is keyword.

---

<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: [October 5, 2022, 12:46pm UTC](https://discuss.elastic.co/t/inconsistent-term-and-terms-query-resolution-in-filter-context/313884/2 "2022-10-05T12:46:02Z")

</div>

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