Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"]
}
like image 803
Tomer Avatar asked Jul 08 '15 09:07

Tomer


People also ask

How do I list documents in an index Elasticsearch?

You can use cURL in a UNIX terminal or Windows command prompt, the Kibana Console UI, or any one of the various low-level clients available to make an API call to get all of the documents in an Elasticsearch index. All of these methods use a variation of the GET request to search the index.

How do I find Elasticsearch document ID?

The _id field is part of the document metadata and is the unique id for the document in the index. If an id is not provided when a document is indexed, Elasticsearch will generate an id for the document. Without the id, the best that can be done is to search for documents as in your example, and return the id.

How do I find Elasticsearch index ID?

A search query, in the kibana index, should give you the list of the index pattern Ids (see below). You could also filter "index-pattern. title" by the name of your new index pattern.

Is ID unique in Elasticsearch?

Each document has an _id that uniquely identifies it, which is indexed so that documents can be looked up either with the GET API or the ids query. The _id can either be assigned at indexing time, or a unique _id can be generated by Elasticsearch.


2 Answers

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.

like image 200
chengpohi Avatar answered Oct 20 '22 20:10

chengpohi


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

like image 45
Siyaram Malav Avatar answered Oct 20 '22 20:10

Siyaram Malav