Skip to content

add parameters in MultiTermsAggregation #310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

package co.elastic.clients.elasticsearch._types.aggregations;

import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.json.JsonpDeserializable;
import co.elastic.clients.json.JsonpDeserializer;
import co.elastic.clients.json.JsonpMapper;
Expand All @@ -32,6 +33,7 @@
import co.elastic.clients.util.ObjectBuilder;
import jakarta.json.stream.JsonGenerator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import javax.annotation.Nullable;
Expand All @@ -48,12 +50,39 @@
public class MultiTermsAggregation extends BucketAggregationBase implements AggregationVariant {
private final List<MultiTermLookup> terms;

@Nullable
private final Integer size;

@Nullable
private final Integer shardSize;

@Nullable
private final Boolean showTermDocCountError;

private final List<Map<String, SortOrder>> order;

@Nullable
private final Integer minDocCount;

@Nullable
private final TermsAggregationCollectMode collectMode;

@Nullable
private final Long shardMinDocCount;

// ---------------------------------------------------------------------------------------------

private MultiTermsAggregation(Builder builder) {
super(builder);

this.terms = ApiTypeHelper.unmodifiableRequired(builder.terms, this, "terms");
this.size = builder.size;
this.shardSize = builder.shardSize;
this.showTermDocCountError = builder.showTermDocCountError;
this.order = ApiTypeHelper.unmodifiable(builder.order);
this.minDocCount = builder.minDocCount;
this.collectMode = builder.collectMode;
this.shardMinDocCount = builder.shardMinDocCount;

}

Expand All @@ -76,6 +105,44 @@ public final List<MultiTermLookup> terms() {
return this.terms;
}


@Nullable
public final Integer size() {
return this.size;
}

/**
* API name: {@code shard_size}
*/
@Nullable
public final Integer shardSize() {
return this.shardSize;
}

/**
* API name: {@code show_term_doc_count_error}
*/
@Nullable
public final Boolean showTermDocCountError() {
return this.showTermDocCountError;
}

/**
* API name: {@code min_doc_count}
*/
@Nullable
public final Integer minDocCount() {
return this.minDocCount;
}

/**
* API name: {@code collect_mode}
*/
@Nullable
public final TermsAggregationCollectMode collectMode() {
return this.collectMode;
}

protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

super.serializeInternal(generator, mapper);
Expand All @@ -90,6 +157,48 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

}

if (this.size != null) {
generator.writeKey("size");
generator.write(this.size);

}
if (this.shardSize != null) {
generator.writeKey("shard_size");
generator.write(this.shardSize);
}
if (this.showTermDocCountError != null) {
generator.writeKey("show_term_doc_count_error");
generator.write(this.showTermDocCountError);
}
if (this.minDocCount != null) {
generator.writeKey("min_doc_count");
generator.write(this.minDocCount);
}
if (this.collectMode != null) {
generator.writeKey("collect_mode");
this.collectMode.serialize(generator, mapper);
}
if (this.shardMinDocCount != null) {
generator.writeKey("shard_min_doc_count");
generator.write(this.shardMinDocCount);
}
if (ApiTypeHelper.isDefined(this.order)) {
generator.writeKey("order");
generator.writeStartArray();
for (Map<String, SortOrder> item0 : this.order) {
generator.writeStartObject();
if (item0 != null) {
for (Map.Entry<String, SortOrder> item1 : item0.entrySet()) {
generator.writeKey(item1.getKey());
item1.getValue().serialize(generator, mapper);
}
}
generator.writeEnd();

}
generator.writeEnd();

}
}

// ---------------------------------------------------------------------------------------------
Expand All @@ -103,6 +212,26 @@ public static class Builder extends BucketAggregationBase.AbstractBuilder<Builde
ObjectBuilder<MultiTermsAggregation> {
private List<MultiTermLookup> terms;

@Nullable
private Integer size;

@Nullable
private Integer shardSize;

@Nullable
private Boolean showTermDocCountError;

@Nullable
private List<Map<String, SortOrder>> order;

@Nullable
private Integer minDocCount;

@Nullable
private TermsAggregationCollectMode collectMode;

@Nullable
private Long shardMinDocCount;
/**
* Required - API name: {@code terms}
* <p>
Expand Down Expand Up @@ -132,6 +261,64 @@ public final Builder terms(Function<MultiTermLookup.Builder, ObjectBuilder<Multi
return terms(fn.apply(new MultiTermLookup.Builder()).build());
}

/**
* API name: {@code size}
*/
@Nullable
public final Builder size(@Nullable Integer value) {
this.size = value;
return this;
}

/**
* API name: {@code shard_size}
*/
public final Builder shardSize(@Nullable Integer value) {
this.shardSize = value;
return this;
}

/**
* API name: {@code show_term_doc_count_error}
*/
public final Builder showTermDocCountError(@Nullable Boolean value) {
this.showTermDocCountError = value;
return this;
}

/**
* API name: {@code order}
* <p>
*/
public final Builder order(List<Map<String, SortOrder>> list) {
this.order = _listAddAll(this.order, list);
return this;
}

/**
* API name: {@code min_doc_count}
*/
public final Builder minDocCount(@Nullable Integer value) {
this.minDocCount = value;
return this;
}

/**
* API name: {@code collect_mode}
*/
public final Builder collectMode(@Nullable TermsAggregationCollectMode value) {
this.collectMode = value;
return this;
}

/**
* API name: {@code shard_min_doc_count}
*/
public final Builder shardMinDocCount(@Nullable Long value) {
this.shardMinDocCount = value;
return this;
}

@Override
protected Builder self() {
return this;
Expand Down Expand Up @@ -161,6 +348,14 @@ public MultiTermsAggregation build() {
protected static void setupMultiTermsAggregationDeserializer(ObjectDeserializer<MultiTermsAggregation.Builder> op) {
BucketAggregationBase.setupBucketAggregationBaseDeserializer(op);
op.add(Builder::terms, JsonpDeserializer.arrayDeserializer(MultiTermLookup._DESERIALIZER), "terms");
op.add(Builder::shardSize, JsonpDeserializer.integerDeserializer(), "shard_size");
op.add(Builder::showTermDocCountError, JsonpDeserializer.booleanDeserializer(), "show_term_doc_count_error");
op.add(Builder::size, JsonpDeserializer.integerDeserializer(), "size");
op.add(Builder::order, JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringMapDeserializer(SortOrder._DESERIALIZER)),
"order");
op.add(Builder::minDocCount, JsonpDeserializer.integerDeserializer(), "min_doc_count");
op.add(Builder::collectMode, TermsAggregationCollectMode._DESERIALIZER, "collect_mode");
op.add(Builder::shardMinDocCount, JsonpDeserializer.longDeserializer(), "shard_min_doc_count");

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package co.elastic.clients.documentation.usage;

import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
import co.elastic.clients.elasticsearch._types.aggregations.MultiTermLookup;
import co.elastic.clients.elasticsearch._types.aggregations.MultiTermsAggregation;
import co.elastic.clients.elasticsearch._types.aggregations.TermsAggregationCollectMode;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.model.ModelTestCase;
import java.util.List;
import java.util.Map;
import org.junit.Test;

public class MultiTermsAggregationTest extends ModelTestCase {

@Test
public void testCreateMultiTermsAggregationWithOptionalFields() {
MultiTermsAggregation multiTermsAggregation = new MultiTermsAggregation.Builder()
.terms(new MultiTermLookup.Builder().field("field-1").build(), new MultiTermLookup.Builder().field("field-2").build())
.size(10)
.shardSize(1)
.showTermDocCountError(false)
.order(List.of(Map.of("field", SortOrder.Asc)))
.minDocCount(1)
.shardMinDocCount(2L)
.collectMode(TermsAggregationCollectMode.DepthFirst)
.build();
Aggregation aggregation = new Aggregation.Builder().multiTerms(multiTermsAggregation).build();
SearchRequest request = new SearchRequest.Builder().aggregations(Map.of("multi_terms", aggregation)).build();
String expectedValue = "{\"aggregations\":{\"multi_terms\":{\"multi_terms\":{\"terms\":[{\"field\":\"field-1\"},{\"field\":\"field-2\"}],\"size\":10,\"shard_size\":1,\"show_term_doc_count_error\":false,\"min_doc_count\":1,\"collect_mode\":\"depth_first\",\"shard_min_doc_count\":2,\"order\":[{\"field\":\"asc\"}]}}}}";
assertEquals(expectedValue, toJson(request));
}

@Test
public void testcreateMultiTermsAggregationWithoutOptionFields() {
MultiTermsAggregation multiTermsAggregation = new MultiTermsAggregation.Builder()
.terms(new MultiTermLookup.Builder().field("field-1").build(), new MultiTermLookup.Builder().field("field-2").build())
.build();
Aggregation aggregation = new Aggregation.Builder().multiTerms(multiTermsAggregation).build();
SearchRequest request = new SearchRequest.Builder().aggregations(Map.of("multi_terms", aggregation)).build();
String expectedValue = "{\"aggregations\":{\"multi_terms\":{\"multi_terms\":{\"terms\":[{\"field\":\"field-1\"},{\"field\":\"field-2\"}]}}}}";
assertEquals(expectedValue, toJson(request));
}
}