saga
saga

Reputation: 2113

Elasticsearch query to return documents with given ids

All the documents in the elasticsearch index have an id field.

I have an array of ids and I want to fetch documents with ids in this array. What is the elasticsearch query for this task?

Upvotes: 0

Views: 2590

Answers (2)

baitmbarek
baitmbarek

Reputation: 2518

Saga, the most efficient query for this is an mget :

Example :

GET myindex/_mget
{
    "docs" : [
        {
            "_id" : "fIjOTW8BkTKnAOE5HVit"
        },
        {
            "_id" : "fojOTW8BkTKnAOE5UliD"
        }
    ]
}

More information on Elasticsearch's documentation

Upvotes: 1

Ashish Modi
Ashish Modi

Reputation: 7770

let's say you have ids in array like ["36088175", "36088176"]

The query will be

{
    "query" : {
        "terms" : {
            "_id" : ["36088175", "36088176"]
        }
    }
}

see terms query for more details - https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html

Upvotes: 2

Related Questions