elasticsearch术语和总和聚合

时间:2015-06-01 21:33:00

标签: elasticsearch elasticsearch-aggregation

我在elasticsearch(1.5)中的文档看起来像:

rake db:setup
  • 并非所有文件都包含三个选项(男/女/未知)

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

{
    "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
            }
        }
    ]
}

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

知道我错过了什么吗?

1 个答案:

答案 0 :(得分:5)

您可能需要确保"gender"属性的类型为"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