3

我在 elasticsearch (1.5) 中有如下文件:

{
    "gender": [
        {
            "name": "unknown",
            "value": 12
        },
        {
            "name": "male",
            "value": 89
        },
        {
            "name": "female",
            "value": 84
        } 
    ]
}
  • 并非所有文件都包含三个选项(男/女/未知)

我想获得每个性别名称的所有值的总和。像那样:

{
    "buckets": [
        {
            "key": "unknown",
            "doc_count": 112,
            "gender_a": {
                "value": 462
            }
        },
        {
            "key": "male",
            "doc_count": 107,
            "gender_a": {
                "value": 438
            }
        },
        {
            "key": "female",
            "doc_count": 36,
            "gender_a": {
                "value": 186
            }
        }
    ]
}

我试过这个查询:

{
    "aggs": {
        "gender_name": {
            "terms": {
                "field": "gender.name"
            },
            "aggs": {
                "gender_sum": {
                    "sum": {
                        "field": "gender.value"
                    }
                }
            }
        }
    }
}

但是发生了一些奇怪的事情,我没有得到正确的值。

知道我缺少什么吗?

4

1 回答 1

10

您可能需要确保您的"gender"属性具有 type "nested"。有了这个,我能够让以下做我认为你问的事情。

首先我建立了一个简单的索引:

PUT /test_index
{
   "mappings": {
      "doc": {
         "properties": {
            "gender": {
               "type": "nested",
               "properties": {
                  "name": {
                     "type": "string"
                  },
                  "value": {
                     "type": "long"
                  }
               }
            }
         }
      }
   }
}

然后添加了几个文档:

PUT /test_index/doc/1
{
    "gender": [
        {
            "name": "unknown",
            "value": 12
        },
        {
            "name": "male",
            "value": 89
        },
        {
            "name": "female",
            "value": 84
        } 
    ]
}

PUT /test_index/doc/2
{
    "gender": [
        {
            "name": "male",
            "value": 8
        },
        {
            "name": "female",
            "value": 4
        } 
    ]
}

然后我能够按性别名称获得总计数,如下所示:

POST /test_index/_search?search_type=count
{
   "aggs": {
      "genders": {
         "nested": {
            "path": "gender"
         },
         "aggs": {
            "gender_terms": {
               "terms": {
                  "field": "gender.name"
               },
               "aggs": {
                  "gender_name_value_sums": {
                     "sum": {
                        "field": "gender.value"
                     }
                  }
               }
            }
         }
      }
   }
}
...
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "genders": {
         "doc_count": 5,
         "gender_terms": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
               {
                  "key": "female",
                  "doc_count": 2,
                  "gender_name_value_sums": {
                     "value": 88,
                     "value_as_string": "88.0"
                  }
               },
               {
                  "key": "male",
                  "doc_count": 2,
                  "gender_name_value_sums": {
                     "value": 97,
                     "value_as_string": "97.0"
                  }
               },
               {
                  "key": "unknown",
                  "doc_count": 1,
                  "gender_name_value_sums": {
                     "value": 12,
                     "value_as_string": "12.0"
                  }
               }
            ]
         }
      }
   }
}

这是我用来测试它的代码:

http://sense.qbox.io/gist/d4533215806b858aa2cc1565546d167fdec3c973

于 2015-06-02T00:48:16.883 回答