【问题标题】:Elasticsearch: retrieve only document _id where field doesn't existElasticsearch:仅检索字段不存在的文档_id
【发布时间】:2021-01-30 08:45:08
【问题描述】:

我想检索字段“name”不存在的所有文档_id(没有其他字段):

我知道我可以像这样搜索字段“名称”不存在的地方:

    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "name"
                }
            }
        }
    }

而且我认为只获取文档的 _id 而不需要使用任何字段(如果我错了,请纠正我):

"fields": []

我如何将这两个部分结合起来以生成有效的查询?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您可以添加 _source 并将其设置为 false,因为 Elasticsearch 将默认返回该字段中的整个 JSON 对象

    "_source": false,
    "query":{
       ...
    }
    

    这将仅从您指定的索引中检索元数据,因此您的 hits 数组将包含每个结果的 _index_type_id_score
    例如

    {
      "took" : 11,
      "timed_out" : false,
      "_shards" : {
        "total" : 12,
        "successful" : 12,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 20,
          "relation" : "eq"
        },
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "filebeat-7.8.1-2021.01.28",
            "_type" : "_doc"
            "_id" : "SomeUniqeuId86aa",
            "_score" : 1.0 
          },
          {
            "_index" : "filebeat-7.8.1-2021.01.28",
            "_type" : "_doc"
            "_id" : "An0therrUniqueiD",
            "_score" : 1.0 
          }
        ]
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-07
      • 1970-01-01
      • 2019-07-31
      • 2018-03-24
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      相关资源
      最近更新 更多