Elasticsearch java 客户端 api 复合聚合

Elasticsearch java client api composite aggregation

我想使用 ES java 客户端实现此 json 查询:

{
    "query": {
        "bool": {
            "must_not": [
                {
                    "terms": {
                        "is_reversed": [
                            1,
                            2
                        ]
                    }
                }
            ],
        }
    },
    "aggs": {
        "my_buckets": {
            "composite": {
                "sources": [
                    {
                        "myfield_id": {
                            "terms": {
                                "field": "myfield_id",
                                "missing_bucket": true
                            }
                        }
                    }
                ],
                "size": 65535
            },
            "aggregations": {
                "sum": {
                    "sum": {
                        "field": "amount"
                    }
                }
            }
        }
    }
}

我使用新的 elasticsearch-java v8.2 api。它有一些文档和示例。

这是我的代码:

Query byReveredTransactionType = TermQuery.of(t ->
                t.field("is_reversed")
                        .value(1))._toQuery();

Map<String, CompositeAggregationSource> sources = Map.of("category_id", CompositeAggregationSource.of(b -> b
                .terms(t -> t.field("category_id")
                        .missingBucket(true))));

SearchRequest req = SearchRequest.of(r -> r
                        .query(q -> q
                                .bool(b -> b.mustNot(byReveredTransactionType))
                        )
                        .aggregations("my_buckets", AggregationBuilders.composite().sources(sources).build()._toAggregation())
                        .size(Short.MAX_VALUE * 2 - 1)
                        .build()._toAggregation()
        );

这里是代码 json 输出:

{
  "aggregations": {
    "my_buckets": {
      "composite": {
        "sources": [
          {
            "myfield_id": {
              "terms": {
                "field": "myfield_id",
                "missing_bucket": true
              }
            }
          }
        ]
      }
    }
  },
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "is_reversed": {
              "value": 1
            }
          }
        }
      ]
    }
  },
  "size": 65533
}

我在 is_reversed 部分有问题。我还实现了 compositeaggregations 部分,但无法将它们组合成 my_buckets.

谢谢!

您需要使用术语查询,下面是在 my_bucket 中添加聚合的方法:

List<FieldValue> values = new ArrayList<>();

values.add(FieldValue.of(1));
values.add(FieldValue.of(2));

Query byReveredTransactionType = TermsQuery.of(t -> t.field("is_reversed").terms(v -> v.value(values)))._toQuery();

Map<String, CompositeAggregationSource> cas = new HashMap<>();

cas.put("myfield_id",CompositeAggregationSource.of(c -> c.terms(t -> t.field("myfield_id").missingBucket(true))));

SearchRequest req = SearchRequest.of(r -> r.query(q -> q.bool(b -> b.mustNot(byReveredTransactionType))).aggregations("my_buckets", Aggregation.of(a -> a.composite(c -> c.sources(cas)).aggregations("sum",Aggregation.of(a2 -> a2.sum(s -> s.field("amount")))))));

StringWriter writer = new StringWriter();
JsonGenerator generator = JacksonJsonProvider.provider().createGenerator(writer);
req.serialize(generator, new JacksonJsonpMapper());
generator.flush();
System.out.println(writer.toString());