Skip to main content
Skip table of contents

Query: Search for files based on the number of values in an array

Query Template

<field_name>

name of the field you want to search in

<operator>

Use an operator (<, >, <=,>=,==,!=)

<number>

The number or count in relation to the operator

JSON
{
  "query": {
    "bool": {
      "must": [
        {
          "exists": {
            "field": "<field_name>"
          }
        }
      ],
      "filter": {
        "script": {
          "script": {
            "source": "doc['<field_name>.keyword'].values.size() <operator> <number>",
            "lang": "painless"
          }
        }
      }
    }
  }
}

Example

Usecase: “I want to find all files in the index that have more than 2 rot_trivial values”

Query

JSON
{
  "query": {
    "bool": {
      "must": [
        {
          "exists": {
            "field": "rot_trivial"
          }
        }
      ],
      "filter": {
        "script": {
          "script": {
            "source": "doc['rot_trivial.keyword'].values.size() > 2",
            "lang": "painless"
          }
        }
      }
    }
  }
}

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.