geeko
geeko

Reputation: 2862

ElasticSearch 5.1 Filtering by Comparing Arrays

I have a keyword array field (say f) and I want to filter documents with an exact array (e.g. filter docs with f = [1, 3, 6] exactly, same order and number of terms).

What is the best way of doing this?

Regards

Upvotes: 0

Views: 112

Answers (1)

pratikvasa
pratikvasa

Reputation: 2045

One way to achieve this is to add a script to the query which would also check the number of elements in the array.

it script would be something like

      "filters": [
      {
        "script": {
          "script": "doc['f'].values.length == 3"
        }
      },
      {
        "terms": {
          "f": [
            1,
            3,
            6
          ],
          "execution": "and"
        }
      }
    ]

Hope you get the idea.

I think an even better idea would be to store the array as a string (if there are not many changes to the structure of the graph) and matching the string directly. This would be much faster too.

Upvotes: 1

Related Questions