【问题标题】:What's the difference between Id's query and Term Query when finding documents by "_id"?通过“_id”查找文档时,Id的查询和Term Query有什么区别?
【发布时间】:2021-07-24 07:02:27
【问题描述】:

我想通过“_id”获取文档,我有3个选择:

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"}}}

我想知道Id的查询和Term Query有什么区别,性能方面以及我应该注意的任何其他点?

我应该选择哪一个(在 Id's 和 Term Query 之间)?

非常感谢任何帮助:)

【问题讨论】:

    标签: elasticsearch elastic-stack


    【解决方案1】:

    第一个选项不是搜索,只是通过 id 获取文档。

    如果您查看第二个和第三个查询的执行计划,您会发现它们是相同的:

    ID查询:

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

    执行计划:

        "_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" : [ ]
                }
              ]
            }
          ]
        }
    

    词条查询:

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

    执行计划:

      "_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" : [ ]
                }
              ]
            }
          ]
        }
    

    有什么不同吗?没有!

    【讨论】:

      猜你喜欢
      • 2020-01-20
      • 2014-10-16
      • 1970-01-01
      • 2015-11-08
      • 1970-01-01
      • 2013-03-21
      • 1970-01-01
      • 1970-01-01
      • 2015-01-17
      相关资源
      最近更新 更多