基于子集合属性的术语聚合

时间:2015-09-25 15:21:53

标签: elasticsearch nest elasticsearch-net

我有以下文件

{
    title: "Some title",
    authors: [
      { LastName: "Smith", Country: "US"},
      { LastName: "Smith", Country: "UK"},
    ]
}

我想根据作者集合的属性Country为搜索添加术语聚合器。搜索应返回所有不同国家/地区的文章列表和聚合桶。似乎反向嵌套聚合是可行的方法,但我无法使它工作。

搜索聚合输出应该是这样的:

"aggregations": {
  "countries": {
      "buckets": [{
        "key": "US",
        "doc_count": 1
      }, {
        "key": "UK",
        "doc_count": 1
      }]
    }
  }

1 个答案:

答案 0 :(得分:0)

我认为您可以在terms aggregation内使用nested aggregation获得所需内容。

我设置了一个这样的简单索引:

submethod BUILD (:$tail, :@legs, *%extraargs) {
    $.tail = $tail;
    @.legs = @legs;
}

然后添加了几个文件:

PUT /test_index
{
   "mappings": {
      "doc": {
         "properties": {
            "authors": {
               "type": "nested",
               "properties": {
                  "Country": {
                     "type": "string",
                     "index": "not_analyzed"
                  },
                  "LastName": {
                     "type": "string",
                     "index": "not_analyzed"
                  }
               }
            },
            "title": {
               "type": "string"
            }
         }
      }
   }
}

然后运行此查询:

PUT /test_index/doc/1
{
    "title": "Some title",
    "authors": [
      { "LastName": "Smith", "Country": "US"},
      { "LastName": "Smith", "Country": "UK"}
    ]
}

PUT /test_index/doc/2
{
    "title": "another title",
    "authors": [
      { "LastName": "Jones", "Country": "SA"},
      { "LastName": "Jones", "Country": "UK"}
    ]
}

似乎返回了你想要的东西:

POST /test_index/_search?search_type=count
{
   "aggs": {
      "authors": {
         "nested": {
            "path": "authors"
         },
         "aggs": {
            "author_countries": {
               "terms": {
                  "field": "authors.Country"
               }
            }
         }
      }
   }
}

以下是我用于测试的一些代码:

http://sense.qbox.io/gist/ccf7bd9d05f646507b3316e985dd6a50e905aed3