Nishikant Tayade
Nishikant Tayade

Reputation: 533

What's the difference between Id's query and Term Query when finding documents by "_id"?

I want to get document by "_id", I have 3 choices:

GET document by "_id" GET order/_doc/001

Use Id's Query, GET order/_search { "query": { "ids" : { "values" : ["001"] } } } Though Id's query takes array of Id's but I will be using it to get only one document at a time, so just passing one id in "values" : ["001"]

Use Term Query GET order/_search { "query": {"term": {"_id" : "001"}}}

I want to know what's the difference between Id's query and Term Query, performance wise and any other points that I should be aware of?

Which one I should choose (between Id's and Term Query)?

Any help is much appreciated:)

Upvotes: 1

Views: 1105

Answers (1)

Val
Val

Reputation: 217494

The first option is not a search and simply gets the document by id.

If you look at the execution plan of the second and third queries, you'll notice that they are identical:

Ids query:

GET order/_search
{
  "explain": true, 
  "query": {
    "ids": {
      "values": ["001"]
    }
  }
}

Execution plan:

    "_explanation" : {
      "value" : 1.0,
      "description" : "sum of:",
      "details" : [
        {
          "value" : 1.0,
          "description" : "ConstantScore(_id:[fe 0 1f])",
          "details" : [ ]
        },
        {
          "value" : 0.0,
          "description" : "match on required clause, product of:",
          "details" : [
            {
              "value" : 0.0,
              "description" : "# clause",
              "details" : [ ]
            },
            {
              "value" : 1.0,
              "description" : "DocValuesFieldExistsQuery [field=_primary_term]",
              "details" : [ ]
            }
          ]
        }
      ]
    }

Term query:

GET order/_search
{
  "explain": true, 
  "query": {
    "term": {
      "_id": "001"
    }
  }
}

Execution plan:

  "_explanation" : {
      "value" : 1.0,
      "description" : "sum of:",
      "details" : [
        {
          "value" : 1.0,
          "description" : "ConstantScore(_id:[fe 0 1f])",
          "details" : [ ]
        },
        {
          "value" : 0.0,
          "description" : "match on required clause, product of:",
          "details" : [
            {
              "value" : 0.0,
              "description" : "# clause",
              "details" : [ ]
            },
            {
              "value" : 1.0,
              "description" : "DocValuesFieldExistsQuery [field=_primary_term]",
              "details" : [ ]
            }
          ]
        }
      ]
    }

Any difference? None!

Upvotes: 5

Related Questions