Elasticsearch:匹配嵌套对象的数组

Elasticsearch: match arrays of nested objects

示例摘自Elasticsearch参考:https://www.elastic.co/guide/en/elasticsearch/reference/5.3/nested.html

我的索引跟这个差不多。唯一的区别是 user.first 和 user.last 是 keyword 类型,所以我可以对它们使用 filter

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"
    }
  ]
}

在两种情况下,我应该使用什么查询来获取与上述数组匹配的文档(恰好两项,一项是 John Smith,一项是 Alice White):

  1. 顺序无关紧要
  2. 订单很重要

经过搜索,实现Order don't matter的最好方法是:索引一个Count字段。

参见:Elasticsearch Equal Exactly

原因是:

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.