1

我有以下文档文章

{
    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
      }]
    }
  }
4

1 回答 1

1

我认为您可以通过嵌套聚合中的术语聚合来获得所需内容

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

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

这似乎返回了你想要的:

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "authors": {
         "doc_count": 4,
         "author_countries": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
               {
                  "key": "UK",
                  "doc_count": 2
               },
               {
                  "key": "SA",
                  "doc_count": 1
               },
               {
                  "key": "US",
                  "doc_count": 1
               }
            ]
         }
      }
   }
}

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

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

于 2015-09-25T19:11:01.790 回答