1

Suppose I have the following data:

{"field": [{"type": "A"}, {"type": "B"}]},
{"field": [{"type": "B"}]}

How do you construct a query in Elasticsearch to get the count of all records with a specific field type value, given field is an array?

2
  • The question is not that clear. What exact result do you want to get? The title says "array length" but the questions reads like "number of documents that have a specific value for field.value. Could you please clarify and add a more specific example? Commented Sep 2, 2017 at 12:54
  • Updated the title. Goal is to get two things really: 1) total number of records that have a specific array item value, and 2) total length of all records where that array field is non-empty Commented Sep 2, 2017 at 22:54

1 Answer 1

1

You can use the Count API, with the following query

Query:

GET /index/index_type/_count
{
    "query" : {
        "term" : { "field.type" : "A" }
    }
}

Response:

{
    "count" : <number of docs>,
    "_shards" : {
        "total" : 5,
        "successful" : 5,
        "failed" : 0
    }
}
Sign up to request clarification or add additional context in comments.

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.