按字段的字段分组

时间:2016-12-19 09:06:15

标签: elasticsearch

这里的示例文档:

{
  "_id": "doc_1",
  "play_count": 1,
  "meta": {
    "ancestors": [
      {"content_type": "Folder"},
      {"content_type": "SerieContainer",
        "_id": "super_doc_1"
      }
    ]
  }
}


{
  "_id": "doc_2",
  "play_count": 10,
  "meta": {
    "ancestors": [
      {"content_type": "Folder"},
      {"content_type": "SerieContainer",
        "_id": "super_doc_1"
      }
    ]
  }
}


{
  "_id": "doc_3",
  "play_count": 100,
  "meta": {
    "ancestors": [
      {"content_type": "Folder"},
      {"content_type": "SerieContainer",
        "_id": "super_doc_1"
      }
    ]
  }
}


{
  "_id": "doc_4",
  "play_count": 500,
  "meta": {
    "ancestors": [
      {"content_type": "Folder"},
      {"content_type": "SerieContainer",
        "_id": "super_doc_2"
      }
    ]
  }
}


{
  "_id": "doc_5",
  "play_count": 5,
  "meta": {
    "ancestors": [
      {"content_type": "Folder"},
      {"content_type": "SerieContainer",
        "_id": "super_doc_2"
      }
    ]
  }
}

是否可以按_id内容" content_type"字段等于" SerieContainer"然后得到" play_count"他们的领域?

例如:

super_doc_1: 111 (doc_1, doc_2, doc_3)
super_doc_2: 505 (doc_4, doc_5)

1 个答案:

答案 0 :(得分:0)

确保ancestors的类型为nested,以便存在关联。所以你可以得到你的mapping内容如下。我刚刚给出了array类型:

"ancestors": {
          "type": "nested",
          "include_in_parent": true, 
          "properties": {
            "content_type": {
              "index": "not_analyzed",
              "type": "string"
            },
            "_id": {
              "index": "not_analyzed",
              "type": "string"
            }
          }
        } 

然后,也许您可​​以使用aggs这样的groupby查询来{ "aggs": { "ancestors": { "nested": { "path": "item.meta.ancestors" }, "aggs": { "id": { "terms": { "field": "item.meta.ancestors.id" }, "aggs": { "content_type": { "terms": { "field": "item.meta.tags.content_type" } } } } } } } } 使用ID和内容类型。

pattern

因此,您可以使用ResultSet来匹配您的内容类型或ID字段以进行过滤。这SO可以派上用场。希望它有所帮助!