Inconsistent Term and Terms query resolution in filter context

According to Elasticsearch docs 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, 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:

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

Results in:

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

TermsQuery

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

Results in:

{
// ...
  "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.

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