0

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 ?

2 Answers 2

1

You can use ids query:

GET /your_index/your_type/_search
{
  "query": {
    "ids": {
      "values": [1, 23, 42, 45]
    }
  },
  "fields": []
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note 2017 Update: The post originally included "fields": [] but since then the name has changed and stored_fields is the new value.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.