Elasticsearch - 条件嵌套提取

时间:2016-11-22 19:16:28

标签: elasticsearch

我有索引映射:

document.write();

有数据:

{
  "dev.directory.3" : {
    "mappings" : {
      "profile" : {
        "properties" : {
          "email" : {
            "type" : "string",
            "index" : "not_analyzed"
          },
          "events" : {
            "type" : "nested",
            "properties" : {
              "id" : {
                "type" : "integer"
              },
              "name" : {
                "type" : "string",
                "index" : "not_analyzed"
              },
            }
          }
        }
      }
    }
  }
}

我想过滤掉匹配的嵌套元素,而不是返回所有"hits" : [ { "_index" : "dev.directory.3", "_type" : "profile", "_id" : "1", "_score" : 1.0, "_source" : { "email" : "test@dummy.com", "events" : [ { "id" : 111, "name" : "ABC", }, { "id" : 222, "name" : "DEF", } ], } }] 数组 - 这在ES中是否可行?

示例查询:

events

EG。如果我查询events.id = 222,则返回的结果列表中应该只有一个元素。

达到这种要求的最佳策略是什么?

1 个答案:

答案 0 :(得分:1)

您可以使用inner_hits仅获取与查询匹配的嵌套记录。

{
  "query": {
    "nested": {
      "path": "events",
      "query": {
        "bool": {
          "filter": [
            {
              "match": {
                "events.id": 222
              }
            }
          ]
        }
      },
      "inner_hits": {}
    }
  },
  "_source": false
}

我也排除了获取 嵌套匹配

的来源