Efficient way to retrieve all _ids in ElasticSearch

后端 未结 11 1791
轮回少年
轮回少年 2021-01-31 01:31

What is the fastest way to get all _ids of a certain index from ElasticSearch? Is it possible by using a simple query? One of my index has around 20,000 documents.

11条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-31 02:01

    Edit: Please read @Aleck Landgraf's Answer, too

    You just want the elasticsearch-internal _id field? Or an id field from within your documents?

    For the former, try

    curl http://localhost:9200/index/type/_search?pretty=true -d '
    { 
        "query" : { 
            "match_all" : {} 
        },
        "stored_fields": []
    }
    '
    

    Note 2017 Update: The post originally included "fields": [] but since then the name has changed and stored_fields is the new value.

    The result will contain only the "metadata" of your documents

    {
      "took" : 7,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "failed" : 0
      },
      "hits" : {
        "total" : 4,
        "max_score" : 1.0,
        "hits" : [ {
          "_index" : "index",
          "_type" : "type",
          "_id" : "36",
          "_score" : 1.0
        }, {
          "_index" : "index",
          "_type" : "type",
          "_id" : "38",
          "_score" : 1.0
        }, {
          "_index" : "index",
          "_type" : "type",
          "_id" : "39",
          "_score" : 1.0
        }, {
          "_index" : "index",
          "_type" : "type",
          "_id" : "34",
          "_score" : 1.0
        } ]
      }
    }
    

    For the latter, if you want to include a field from your document, simply add it to the fields array

    curl http://localhost:9200/index/type/_search?pretty=true -d '
    { 
        "query" : { 
            "match_all" : {} 
        },
        "fields": ["document_field_to_be_returned"]
    }
    '
    

提交回复
热议问题