通过嵌套文档ElasticSearch上的子/父数据进行聚合

时间:2016-03-22 20:58:23

标签: elasticsearch aggregate nested-queries nosql-aggregation

如何使用嵌套文档通过子数据和父数据查询嵌套文档和组: 我以这种方式查询获得每个品牌和年份的总和,但我想得到 按年份和品牌的总和,我尝试返回错误的结果:

#!/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"}
                        }
                 } 
            }
}
}
}
}
}

0 个答案:

没有答案