Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find by ID & IDs in ElasticSearch and ElasticSearch Javascript client

I was trying to find the most common use of lookup that is by ID in elastic search.

like image 965
Muhammad Faizan Avatar asked Nov 03 '17 05:11

Muhammad Faizan


2 Answers

Find by ID with Elasticsearch API directly:

GET /myindex/mytype/1

Find By ID with JavaScript client for ElasticSearch:

let body = {};
client.get({
    index: 'myindex',
    type: 'mytype',
    id: 1
}, (error, response) => {
    body = response.body;
    // ...
});
// TODO: Validation of return object.
return body;

Find by multiple IDs with Elasticsearch API directly:

GET /_search
{
    "query": {
        "ids" : {
            "type" : "my_type",
            "values" : ["1", "4", "100"]
        }
    }
}

Find by multiple IDs with JavaScript client for ElasticSearch:

client.search({
    index: 'myindex',
    body: {
        query: {
            ids: {
                type: "my_type",
                values: ["1", "4", "100"]
            }
        }
    },
}, (error, response) => {
    // handle response here
});

Refer to the official Elastic documentation for the Node client.

like image 169
Muhammad Faizan Avatar answered Nov 15 '22 17:11

Muhammad Faizan


You also can use mget for this kind of query, like:

const getDocsByIds = (documentIds) => ({
    index,
    type,
    body: {
        ids: documentIds,
    },
});

return elasticsearchClient.mget(getDocsByIds);
like image 32
Jhon Mario Lotero Avatar answered Nov 15 '22 16:11

Jhon Mario Lotero