ElasticSearch Aggregation Java-对象的嵌套数组

时间:2018-09-11 23:49:15

标签: java elasticsearch

我正在尝试对嵌套对象数组编写报告查询。

数据如下:

{
    "payment": {
             "transactions": [
                             {
                                  "amount": "200",
                                  "paymentDate": "2018-09-12T00:00:00.000Z",
                                  "transactionType": "D"
                             },
                             ...,
                             {...}
                       ]
              }

}

我的查询的内容在下面,并且没有错误运行,但是,聚合的结果包括查询中指定的paymentDate之外的交易。我了解该查询返回的文档符合指定的条件,因此该汇总没有经过适当的“过滤”是有道理的-是否有办法确保我的汇总中仅包括指定的PaymentDate内的交易?

    SearchRequestBuilder searchRequestBuilder = esClient
            .getClient()
            .prepareSearch(PRODUCT_INDEX)
            .setIndices(INDEX)
            .setQuery(
                    boolQuery()
                            .must(
                                    nestedQuery(
                                            path,
                                            boolQuery()
                                                    .must(termQuery("payment.transactions.transactionType", "D"))
                                                    .must(rangeQuery("payment.transaction.paymentDate").from(begin.getTime()).to(end.getTime())),
                                            ScoreMode.Max
                                    )
                            )  
            )
            .addAggregation(
                    AggregationBuilders
                            .nested("agg", path)
                            .subAggregation(
                                    AggregationBuilders
                                            .sum("Report")
                                            .field(String.format("payment.transaction.amount", path))
                            )
            )
            .storedFields(SEARCH_FIELDS)
            .setSize(10000)
            .setScroll(new TimeValue(600000));

编辑已解决: 在按日期适当的总和过滤器顶部添加按日期分类的子聚合过滤器

...
            .addAggregation(
            AggregationBuilders
                .nested("agg", path)
                .subAggregation(
                    AggregationBuilders
                        .filter("date", boolQuery().must(rangeQuery("payment.transaction.paymentDate").from(begin.getTime()).to(end.getTime())))
                        .subAggregation(
                            AggregationBuilders
                                .sum("Report")
                                .field("payment.transaction.amount")
                        )
                )
        )

0 个答案:

没有答案