How to retrieve all document ids (_id) in a specific index

11,368

Solution 1

For elasticsearch, only can specific _source fields by using fields array.

_index, _type, _id, _score must will be returned by elasticsearch.

there is no way to remove them from response.

Solution 2

I am assuming your _id is of your document in index not of index itself.

In new version of elastic search, "_source" is used for retrieving selected fields of your es document because _source fields contains everything you need in elastic search for a es record.

Example:

Let's say index name is "movies" and type is "movie" and you want to retrieve the movieName and movieTitle of all elastic search records.

curl -XGET 'http://localhost:9200/movies/movie/_search?pretty=true' -d '
{ 
    "query" : { 
        "match_all" : {} 
    },
    "_source": ["movieName","movieTitle"]
}'

OR http://localhost:9200/movies/movie/_search?pretty=true&_source=movieName,movieTitle

By default it return 10 results. For getting n number of records then put &size=n in url

Share:
11,368
Tomer
Author by

Tomer

Updated on June 13, 2022

Comments

  • Tomer
    Tomer almost 2 years

    I am trying to retrieve all documents in an index, while getting only the _id field back.

    Basically I want to retrieve all the document ids I have.

    While using:

    {
       "query": {
           "match_all": {}
       },
       "fields": []
    }
    

    The hits I get contain: "_index", "_type", "_id" , "_score", "_source" which is way more then I need.

    Edit(Answer): So my problem was that I used KOPF to run the queries, and the results were not accurate (got the _source and some more..)! When using curl I got the correct results!

    So the above query actually achieved what I needed! You can also use:

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

    or

    {
       "query": {
          "match_all": {}
       },
       "fields": ["_id"]
    }