[英]Elastic Search multi-value field aggregation
My indexed documents have a schema: 我的索引文档具有以下架构:
{
...
'authors': [{'first name': 'John', 'last name': 'Smith'},
{'first name': 'Mark', 'last name': 'Spencer'}]
...
}
I would like to search them and aggregate by the individual authors, so get a list with top authors which occurred in my hits. 我想搜索它们并按单个作者进行汇总,因此获得了我的命中出现的主要作者的列表。 Terms aggregation seems to be a match for my needs, but I'm not able to get it working for field with a list of values. 术语汇总似乎可以满足我的需求,但是我无法使它适用于带有值列表的字段。 Any help? 有什么帮助吗?
You will probably want to use a nested type , then you can use a nested aggregation on the author names. 您可能希望使用嵌套类型 ,然后可以对作者名称使用嵌套聚合 。
As an example, I set up a simple index like this: 例如,我设置了一个简单的索引,如下所示:
PUT /test_index
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"doc": {
"properties": {
"title": {
"type": "string"
},
"authors": {
"type": "nested",
"properties": {
"first_name": {
"type": "string"
},
"last_name": {
"type": "string"
}
}
}
}
}
}
}
Then added a couple of docs: 然后添加了一些文档:
PUT /test_index/doc/1
{
"title": "Book 1",
"authors": [
{
"first_name": "John",
"last_name": "Smith"
},
{
"first_name": "Mark",
"last_name": "Spencer"
}
]
}
PUT /test_index/doc/2
{
"title": "Book 2",
"authors": [
{
"first_name": "Ben",
"last_name": "Jones"
},
{
"first_name": "Tom",
"last_name": "Lawrence"
}
]
}
Then I can get the list of (analyzed) author last names with: 然后,我可以获得具有(分析的)作者姓氏的列表:
POST /test_index/_search?search_type=count
{
"aggs": {
"nested_authors": {
"nested": {
"path": "authors"
},
"aggs": {
"author_last_names": {
"terms": {
"field": "authors.last_name"
}
}
}
}
}
}
...
{
"took": 71,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": []
},
"aggregations": {
"nested_authors": {
"doc_count": 4,
"author_last_names": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "jones",
"doc_count": 1
},
{
"key": "lawrence",
"doc_count": 1
},
{
"key": "smith",
"doc_count": 1
},
{
"key": "spencer",
"doc_count": 1
}
]
}
}
}
}
Here is the code I used: 这是我使用的代码:
http://sense.qbox.io/gist/ca94cc11a12f8e4fed5c62c52966128b9a6f58de http://sense.qbox.io/gist/ca94cc11a12f8e4fed5c62c52966128b9a6f58de
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.