Reputation: 1119
I have a list of element ids (for example: [1, 23, 42, 45]) and I'm looking for the fastest way to check if in ES database elements with those ids already exist or not. In result I want to recieve response containing only ids which are present in Elasticsearch: for example [1, 42] - what means that elements with ids 1 and 42 are yet in DB.
Now I send a query for all element ids in the DB and then check if on those list are my [1, 23, 42, 45] or not. Is there any faster way ?
Upvotes: 0
Views: 2091
Reputation: 8718
Here's another way, if what you are wanting is just a list of existing ids from the list you supply (I can't tell if that's what you're asking for or not):
POST /test_index/_search?search_type=count
{
"aggs": {
"id_terms": {
"terms": {
"field": "id",
"include": [1, 23, 42, 45]
}
}
}
}
If I have documents with ids 1,2,3,42
, the response is:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0,
"hits": []
},
"aggregations": {
"id_terms": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 1,
"doc_count": 1
},
{
"key": 42,
"doc_count": 1
}
]
}
}
}
Here is the code I used to test it:
http://sense.qbox.io/gist/96d8d27078e74f7645eee85044b59899c57d0022
Upvotes: 0
Reputation: 52368
You can use ids
query:
GET /your_index/your_type/_search
{
"query": {
"ids": {
"values": [1, 23, 42, 45]
}
},
"fields": []
}
Upvotes: 1