嵌套对象和聚合
Nested objects and aggregations
我将这个映射到我的 ES 数据库中,它使用了嵌套对象。
每个文档都是一个公司,并且有一个保存为嵌套对象的员工列表。
这是映射:
"company": {
"properties": {
"company_name": {
"type": "string"
},
"employee": {
"properties": {
"name": {
"type": "string"
},
"city": {
"type": "string"
}
},
"type": "nested"
}
}
}
}
}
我有这两家公司:
Company A
[
Smith, Dallas
Mark, New York
Smith, Houston
]
Company B
[
Smith, Dallas
Peter, New York
Mary, Houston
]
即同一个名字可以在不同的公司出现,并且在每个公司出现不止一次。
我需要 运行 的查询应该是这个:
为所有姓名为 Smith 的员工汇总城市
我需要这样的答案:
City for employee Smith:
Dallas: 2
Houston: 1
记住 employee 是一个嵌套对象列表,我不需要任何关于公司名称的信息。
试试这个
{
"size": 0,
"aggs": {
"my_aggs": {
"nested": {
"path": "employee"
},
"aggs": {
"city_for_smith": {
"filter": {
"term": {
"name": "smith"
}
},
"aggs": {
"result": {
"terms": {
"field": "city"
}
}
}
}
}
}
}
}
要同时显示每个城市的公司名称,您可以在最后一个聚合中嵌套另一个聚合。
{
...
"aggs": {
"result": {
"terms": {
"field": "city"
},
"aggs": {
"companyAggs": {
"reverse_nested": {},
"aggs": {
"in_company": {
"terms": {
"field": "company_name"
}
}
}
}
}
}
}
}
我将这个映射到我的 ES 数据库中,它使用了嵌套对象。
每个文档都是一个公司,并且有一个保存为嵌套对象的员工列表。 这是映射:
"company": {
"properties": {
"company_name": {
"type": "string"
},
"employee": {
"properties": {
"name": {
"type": "string"
},
"city": {
"type": "string"
}
},
"type": "nested"
}
}
}
}
}
我有这两家公司:
Company A
[
Smith, Dallas
Mark, New York
Smith, Houston
]
Company B
[
Smith, Dallas
Peter, New York
Mary, Houston
]
即同一个名字可以在不同的公司出现,并且在每个公司出现不止一次。
我需要 运行 的查询应该是这个:
为所有姓名为 Smith 的员工汇总城市
我需要这样的答案:
City for employee Smith:
Dallas: 2
Houston: 1
记住 employee 是一个嵌套对象列表,我不需要任何关于公司名称的信息。
试试这个
{
"size": 0,
"aggs": {
"my_aggs": {
"nested": {
"path": "employee"
},
"aggs": {
"city_for_smith": {
"filter": {
"term": {
"name": "smith"
}
},
"aggs": {
"result": {
"terms": {
"field": "city"
}
}
}
}
}
}
}
}
要同时显示每个城市的公司名称,您可以在最后一个聚合中嵌套另一个聚合。
{
...
"aggs": {
"result": {
"terms": {
"field": "city"
},
"aggs": {
"companyAggs": {
"reverse_nested": {},
"aggs": {
"in_company": {
"terms": {
"field": "company_name"
}
}
}
}
}
}
}
}