如何使用嵌套文档查询嵌套文档并按子数据和父数据分组:我以这种方式查询以获取每个品牌和年份的总和,但我想按年份和品牌获取总和,所有我尝试返回错误结果:
#!/bin/bash
curl -XPOST 'localhost:9200/i_part/part2/_search?pretty' -d '
{
"query": {
"bool": {
"should": [
{ "match": { "p_category":"MFGR#11"}},
{
"filtered": {
"filter": {
"nested": {
"inner_hits": {},
"path": "lineorder",
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{"match": {"lineorder.supplier.s_region":"AMERICA"}}
]
}
}
}
}
}
}
}
}
]
}
},"aggs": {
"group_by_brand": {
"terms": {
"field": "p_brand1"
},
"aggs": {
"lineorder": {
"nested": {
"path": "lineorder"
},
"aggs": {
"only_loc": {
"filter": {
"bool": {
"must": [
{"match": {"lineorder.supplier.s_region":"AMERICA"}}
]
}
},
"aggs": {
"group_by_year": {
"terms": {
"field": "lineorder.orderdate.d_year"
},
"aggs": {
"sum_revenue": {
"sum": {
"field": "lineorder.lo_revenue"
}}}
}
}
}
}
}
}
}
}, "size":0
}'
我的映射是这样的,有一部分带有一些属性,并且在一系列线订单(嵌套)内,在每个线订单内有客户、供应商和订单日期(不是嵌套)(一对一):
curl -XPUT 'localhost:9200/i_part' -d '
{
"mappings": {
"part2": {
"properties": {
"p_name": {"type":"string", "index":"not_analyzed" },
"p_category": {"type":"string", "index":"not_analyzed" },
"p_brand1": {"type":"string", "index":"not_analyzed" },
"lineorder": {
"type": "nested",
"properties": {
"lo_quantity": {"type":"integer"},
"lo_discount": {"type":"integer"},
"lo_revenue": {"type":"integer"},
"lo_shippriority": {"type": "string", "index": "not_analyzed"},
"lo_shipmode": {"type": "string", "index": "not_analyzed"},
"customer"{
"properties":{
"c_name": {"type": "string", "index": "not_analyzed"}
}
}
"supplier"{
"properties":{
"s_name": {"type": "string", "index": "not_analyzed"}
"s_region": {"type": "string", "index": "not_analyzed"}
}
}
"orderdate"{
"properties":{
"d_date": {"type": "integer"}
"d_year": {"type": "integer"}
}
}
}
}
}
}
}
}