这是一份工作terms
聚合(文档 http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation).
你可以拥有与众不同的departments
像这样的值:
POST company/employee/_search
{
"size":0,
"aggs": {
"by_departments": {
"terms": {
"field": "departments.name",
"size": 0 //see note 1
}
}
}
}
在您的示例中,输出:
{
...
"aggregations": {
"by_departments": {
"buckets": [
{
"key": "management", //see note 2
"doc_count": 2
},
{
"key": "accounts",
"doc_count": 1
},
{
"key": "it",
"doc_count": 1
}
]
}
}
}
两个附加说明:
- setting
size
设置为 0 会将最大存储桶数设置为 Integer.MAX_VALUE。如果太多就不要使用departments
独特的价值观。
- 你可以看到钥匙是
terms
分析结果departments
价值观。请务必使用您的terms
映射为的字段上的聚合not_analyzed
.
例如,使用我们的默认映射(departments.name
is an analyzed
字符串),添加该员工:
{
"name": "Bill Gates",
"departments": [
{
"name": "IT"
},
{
"name": "Human Resource"
}
]
}
会导致这样的结果:
{
...
"aggregations": {
"by_departments": {
"buckets": [
{
"key": "it",
"doc_count": 2
},
{
"key": "management",
"doc_count": 2
},
{
"key": "accounts",
"doc_count": 1
},
{
"key": "human",
"doc_count": 1
},
{
"key": "resource",
"doc_count": 1
}
]
}
}
}
有了正确的映射:
POST company
{
"mappings": {
"employee": {
"properties": {
"name": {
"type": "string"
},
"departments": {
"type": "object",
"properties": {
"name": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
相同的请求最终输出:
{
...
"aggregations": {
"by_departments": {
"buckets": [
{
"key": "IT",
"doc_count": 2
},
{
"key": "Management",
"doc_count": 2
},
{
"key": "Accounts",
"doc_count": 1
},
{
"key": "Human Resource",
"doc_count": 1
}
]
}
}
}
希望这可以帮助!