了解Elasticsearch聚合

时间:2018-07-25 19:48:29

标签: elasticsearch

我的情况如下: 我有可以定期或一次性收入的人。我想总结一下每个人的正常收入,这些人没有被删除,并且出生在一个日期范围内。查询部分工作得很好,但是当我开始将Elastic查询的聚合部分放在一起时,我得到了错误的数字,无法理解,我该怎么做。

这是我为数据类型创建映射的方式:

curl -X PUT -i http://localhost:9200/people --data '{
"settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
      "person" : {
        "properties" : {
          "birthDate" : {
            "type" : "date",
            "format" : "strict_date_optional_time||epoch_millis"
          },
          "company" : {
            "type" : "string"
          },
          "deleted" : {
            "type" : "boolean"
          },
          "income" : {
            "type": "nested",
            "properties" : {
              "income_type" : {
                "type" : "string"
              },
              "value" : {
                "type" : "double"
              }
            }
          },
          "name" : {
            "type" : "string"
          }
        }
      }
    }
  }
}'

这是数据:

curl -X PUT -H 'Content-Type: application/json' -i http://localhost:9200

/people/person/1 --data '{
"deleted":false,
"birthDate":"1980-10-10",
"name":"John Smith",
"company": "IBM",
"income": [{"income_type":"regular","value":55.5}]
}'

curl -X PUT -H 'Content-Type: application/json' -i http://localhost:9200/people/person/2 --data '{
"deleted":true,
"birthDate":"1960-10-10",
"name":"Mary Legend",
"company": "ASUS",
"income": [{"income_type":"one-time","value":10},{"income_type":"regular","value":55}]
}'

curl -X PUT -H 'Content-Type: application/json' -i http://localhost:9200/people/person/3 --data '{
"deleted":false,
"birthDate":"2000-10-10",
"name":"F. King Elastic",
"income": [{"income_type":"one-time","value":1},{"income_type":"regular","value":5}]
}'

curl -X PUT -H 'Content-Type: application/json' -i http://localhost:9200/people/person/4 --data '{
"deleted":false,
"birthDate":"1989-10-10",
"name":"Prison Lesley",
"income": [{"income_type":"regular","value":120.7},{"income_type":"one-time","value":99.3}]
}'

curl -X PUT -H 'Content-Type: application/json' -i http://localhost:9200/people/person/5 --data '{
"deleted":false,
"birthDate":"1983-10-10",
"name":"Prison Lesley JR.",
"income": [{"income_type":"one-time","value":99.3}]
}'

curl -X PUT -H 'Content-Type: application/json' -i http://localhost:9200/people/person/6 --data '{
"deleted":true,
"birthDate":"1986-10-10",
"name":"Hono Lulu",
"income": [{"income_type":"regular","value":11.3}]
}'

这是一个查询,它过滤具有至少一个固定收入并且在给定日期之间出生的未删除人员。下面的查询仍然可以正常工作(两个人都符合条件):

curl -X POST -H 'Content-Type: application/json' -i 'http://localhost:9200/people/person/_search?pretty=true' --data '{
    "size": 100,
    "filter": {
        "bool": {
            "must": [
                {
                    "match": {
                        "deleted": false
                    }
                },
                {
                    "range": {
                        "birthDate": {
                            "gte": "1980-01-01",
                            "lte": "1990-12-31"
                        }
                    }
                },
                {
                    "nested": {
                        "path": "income",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "match": {
                                            "income.income_type": "regular"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
}'

但是当我添加聚合部分时,一切都出错了,我不明白为什么:(

curl -X POST -H 'Content-Type: application/json' -i 'http://localhost:9200/people/person/_search?pretty=true' --data '{
    "size": 100,
    "filter": {
        "bool": {
            "must": [
                {
                    "match": {
                        "deleted": false
                    }
                },
                {
                    "range": {
                        "birthDate": {
                            "gte": "1980-01-01",
                            "lte": "1990-12-31"
                        }
                    }
                },
                {
                    "nested": {
                        "path": "income",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "match": {
                                            "income.income_type": "regular"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    },
    "aggs": {
        "incomes": {
            "nested": {
                "path": "income"
            },
            "aggs": {
                "income_type": {
                    "filter": {
                        "bool": {
                            "must": [
                                {
                                    "match": {
                                        "income.income_type": "regular"
                                    }
                                },
                                {
                                    "match": {
                                        "deleted": false
                                    }
                                }
                            ]
                        }
                    },
                    "aggs": {
                        "totalIncome": {
                            "sum": {
                                "field": "income.value"
                            }
                        }
                    }
                }
            }
        }
    }
}'

结果是这样的:

...
"aggregations": {
    "incomes": {
      "doc_count": 9,
      "income_type": {
        "doc_count": 0,
        "totalIncome": {
          "value": 0.0
        }
      }
    }
  }
}

我期望doc_count为2,totalIncome应该为176.2(120.7 + 55.5)

有人有什么主意吗,我该怎么办?

1 个答案:

答案 0 :(得分:0)

一个好的开始!您的聚合中不需要deleted字段上的过滤器,因为您的查询已经过滤掉了所有已删除的文档。试试这个:

"aggs": {
    "incomes": {
        "nested": {
            "path": "income"
        },
        "aggs": {
            "income_type": {
                "filter": {
                    "match": {
                         "income.income_type": "regular"
                    }
                },
                "aggs": {
                    "totalIncome": {
                        "sum": {
                            "field": "income.value"
                        }
                    }
                }
            }
        }
    }
}