ElasticSearch聚合数组字段的总和

时间:2016-05-26 10:05:17

标签: elasticsearch

在检索数组字段上的聚合总和时遇到困难。

我想基于两个字段执行聚合:casting.name和casting.category。

My Schema:
"mappings": {
    "movies": {
      "properties": {
        "title" : { "type": "string" },
        "year" : { "type": "integer" },
        "casting": {
          "type": "nested", 
          "properties": {
            "name":    { "type": "string" },
            "quantity": { "type": "long" }
          }
        }
      }
    }
  } 

我尝试使用基于casting.name字段的TermsAggregation,带有子聚合,这是另一个基于casting.quantity字段的TermsAggregation总和。

My Query representation is shown below:

GET groups/_search?search_type=count
{  
    "query" : {
        "terms" : {
            "movies.casting.name" : ["gap"],
            "minimum_should_match":1
        }
    },
    "aggregations" : { 
        "termsAggregation" : { 
            "terms" : { 
              "field" : "movies.casting.name"
            },      
         "aggregations": {
            "quantitySum": {
                "filter" : { 
                    "term" : { 
                        "groups.permissions.product" :  "ycsfa" }             
                },
                "aggregations" : {
                        "gapSum" : { 
                            "sum" : { 
                                "field" : "movies.casting.quantity" }
                        }
                    }               
            }
         }
        }
    }
}

Showing wrong results ...

结果将总结铸造中的所有名称和数量字段......

这里有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您需要使用nested QueryNested Aggregation,因为您的字段为nested

GET groups/_search?search_type=count
{
"query": {
  "nested": {
     "path": "casting",
     "query": {
        "terms": {
           "movies.casting.name": [
              "gap"
           ],
           "minimum_should_match": 1
        }
     }
   }
 } ,
"aggregations": {
  "nested": {
     "path": "casting"
  },
  "aggs": {
     "termsAggregation": {
        "terms": {
           "field": "movies.casting.name"
        },
        "aggregations": {
                 "gapSum": {
                    "sum": {
                       "field": "movies.casting.quantity"
                    }
                 }
              }
         }
      }
  }
 }
相关问题