coderz
coderz

Reputation: 4989

Elasticsearch query to get array size

I have Elasticsearch documents look like:

Doc 1:
{
  "name" : "Tom",
  "values" : [
    { "value" : "value 1" },
    { "value" : "value 2" },
    { "value" : "value 3" }
  ]
}

Doc 2:
{
  "name" : "Bob",
  "values" : [
    { "value" : "value 1" },
    { "value" : "value 2" }
  ]
}

How can I query by docs which the values array size is larger than 2?

Upvotes: 1

Views: 829

Answers (1)

Gibbs
Gibbs

Reputation: 22956

You need to add this to your bool query.

"filter" : {
    "script" : {
        "script" : "doc['values'].values.size() > 2"
    }
}

filter filters the document which matches the condition.

size() returns the size of the array.

doc['values'] looks for values field.

Upvotes: 1

Related Questions