内部列表上的Elasticsearch查询只从列表中获取匹配的对象,而不是结果文档中的整个列表

时间:2015-08-19 06:49:50

标签: elasticsearch

在以下弹性搜索文档中,需要查找特定名称的注释,例如“Mary Brown”。基本上查询内部列表并仅从列表中获取匹配对象而不是结果文档中的整个列表。可能吗。我已经为“评论”

定义了嵌套的映射
{
  "title": "Investment secrets",
  "body":  "What they don't tell you ...",
  "tags":  [ "shares", "equities" ],
  "comments": [
    {
      "name":    "Mary Brown",
      "comment": "Lies, lies, lies",
      "age":     42,
      "stars":   1,
      "date":    "2014-10-18"
    },
    {
      "name":    "John Smith",
      "comment": "You're making it up!",
      "age":     28,
      "stars":   2,
      "date":    "2014-10-16"
    }, 
     {
      "name":    "Mary Brown",
      "comment": "making it!!!",
      "age":     42,
      "stars":   3,
      "date":    "2014-10-20"
    }
  ]
}

1 个答案:

答案 0 :(得分:1)

由于您已将comments字段正确映射为nested,因此可以使用inner_hits进行此操作,如下所示:

{
  "_source": false,
  "query": {
    "nested": {
      "path": "comments",
      "inner_hits": {        <---- use inner_hits here
        "_source": [
          "comment", "date"
        ]
      },
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "comments.name": "Mary Brown"
              }
            }
          ]
        }
      }
    }
  }
}