Elasticsearch: match arrays of nested objects

632 views Asked by At

The example is extracted from Elasticsearch reference: https://www.elastic.co/guide/en/elasticsearch/reference/5.3/nested.html

My index is similar to this. The only difference is user.first and user.last are keyword type so I can use filter on them.

PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "user": {
          "type": "nested" 
        }
      }
    }
  }
}

PUT my_index/my_type/1
{
  "group" : "fans",
  "user" : [
    {
      "first" : "John",
      "last" :  "Smith"
    },
    {
      "first" : "Alice",
      "last" :  "White"
    }
  ]
}

What is the query I should use to get documents that match the above array (exactly two items, one item is John Smith, one item is Alice White) in two situations:

  1. Order doesn't matter
  2. Order matters
1

There are 1 answers

0
nxh On BEST ANSWER

After searching, the best way to achieve Order doesn't matter is: indexing a Count field.

See: Elasticsearch Equal Exactly

The reason is:

In Elasticsearch, there is no dedicated array type. Any field can contain zero or more values by default, however, all values in the array must be of the same datatype.