简体   繁体   中英

Elasticsearch: retrieve only document _id where field doesn't exist

I would like to retrieve all document _ids (without other fields) where field "name" doesn't exist:

I know I can search for where field "name" doesn't exist like this:

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

and I think that to get the _id of the document only without any fields i need to use (correct me if I'm wrong):

"fields": []

How do I combine these 2 parts to make a query that works?

You can just add _source and set to false as Elasticsearch will return the entire JSON object in that field by default

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

and this will retrieve just the metadata from your specified index, so your hits array will contain _index , _type , _id and _score for each result
eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM