0

i am new in ElasticSearch i want count document based on id but i want to pass array in id like "myId":[1,2,3,4,5]

for every id i want count number

Current input

GET /probedb_v1/probe/_count
{
 "query": {
    "match_phrase": {
       "myId": 1
     }
  }
}

Current output

{ "count": 6929,
 "_shards":{ "total": 1,
  "successful": 1,
  "failed": 0
}
}

What is input for my Required Output

{ "count": [6929,5222,65241,5241,6521],
 "_shards":{ "total": 1,
  "successful": 1,
  "failed": 0
}
}

also need code for elasticsearch java-api

3
  • The _id is unique per index and type combination. So counting _id doesn't make sense. Commented May 18, 2016 at 11:57
  • yes _id unique but i want on my columns(term) Commented May 18, 2016 at 12:03
  • you will have to use aggregates. even that will not give the output in your desired format. Commented May 18, 2016 at 12:18

1 Answer 1

1

You can do it like this:

GET /probedb_v1/probe/_search
{
  "size": 0,
  "query": {
    "terms": {
      "myId": [123, 44]
    }
  }, 
  "aggs": {
    "NAME": {
      "terms": {
        "field": "myId",
        "size": 50
      }
    }
  }
}

This will give you this output:

   "aggregations": {
      "NAME": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": 123,
               "doc_count": 3
            },
            {
               "key": 44,
               "doc_count": 2
            }
         ]
      }
   }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but it works but not as expected. I need separate outputs(count) for each input. I received this output. ** "hits": {"total": 2,} **
Your English is not that good and I don't understand what you mean. The output as "count": [6929,5222,65241,5241,6521] is not possible in Elasticsearch. The best approach is with aggregations and the output I provided in my answer.

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.