简体   繁体   中英

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

我试图在弹性搜索中通过 ID 查找最常见的查找用法。

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 .

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

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

return elasticsearchClient.mget(getDocsByIds);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM