diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bfda49ff4..40c4fe338b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Removed ### Fixed +- Restore the typed buckets of the `terms` aggregates (`StringTermsAggregate` etc. extended `TermsAggregateBase` since the October regeneration) by binding a generic type parameter from the narrowed union branch of an instantiation; `TermsBucketBase` regains `doc_count_error_upper_bound` ([#2302](https://github.com/opensearch-project/opensearch-java/issues/2302)) - Declare the dependencies the streaming support of `ApacheHttpClient5Transport` needs, without which building the transport failed with `NoClassDefFoundError: org/reactivestreams/Publisher` ([#2294](https://github.com/opensearch-project/opensearch-java/issues/2294)) ### Security diff --git a/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/DoubleTermsAggregate.java b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/DoubleTermsAggregate.java index cb630962bb..0462daa062 100644 --- a/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/DoubleTermsAggregate.java +++ b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/DoubleTermsAggregate.java @@ -39,7 +39,6 @@ import java.util.function.Function; import javax.annotation.Generated; import javax.annotation.Nonnull; -import org.opensearch.client.json.JsonData; import org.opensearch.client.json.JsonpDeserializable; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.ObjectBuilderDeserializer; @@ -55,7 +54,7 @@ */ @JsonpDeserializable @Generated("org.opensearch.client.codegen.CodeGenerator") -public class DoubleTermsAggregate extends TermsAggregateBase +public class DoubleTermsAggregate extends TermsAggregateBase implements AggregateVariant, ToCopyableBuilder { @@ -94,7 +93,7 @@ public static Builder builder() { /** * Builder for {@link DoubleTermsAggregate}. */ - public static class Builder extends TermsAggregateBase.AbstractBuilder + public static class Builder extends TermsAggregateBase.AbstractBuilder implements CopyableBuilder { @@ -145,7 +144,7 @@ public DoubleTermsAggregate build() { ); protected static void setupDoubleTermsAggregateDeserializer(ObjectDeserializer op) { - setupTermsAggregateBaseDeserializer(op, JsonData._DESERIALIZER); + setupTermsAggregateBaseDeserializer(op, DoubleTermsBucket._DESERIALIZER); } @Override diff --git a/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/DoubleTermsBucket.java b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/DoubleTermsBucket.java new file mode 100644 index 0000000000..a789337578 --- /dev/null +++ b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/DoubleTermsBucket.java @@ -0,0 +1,217 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +//---------------------------------------------------- +// THIS CODE IS GENERATED. MANUAL EDITS WILL BE LOST. +//---------------------------------------------------- + +package org.opensearch.client.opensearch._types.aggregations; + +import jakarta.json.stream.JsonGenerator; +import java.util.Objects; +import java.util.function.Function; +import javax.annotation.Generated; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.opensearch.client.json.JsonpDeserializable; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.ObjectBuilderDeserializer; +import org.opensearch.client.json.ObjectDeserializer; +import org.opensearch.client.util.ApiTypeHelper; +import org.opensearch.client.util.CopyableBuilder; +import org.opensearch.client.util.ObjectBuilder; +import org.opensearch.client.util.ToCopyableBuilder; + +// typedef: _types.aggregations.DoubleTermsBucket + +@JsonpDeserializable +@Generated("org.opensearch.client.codegen.CodeGenerator") +public class DoubleTermsBucket extends TermsBucketBase implements ToCopyableBuilder { + + private final double key; + + @Nullable + private final String keyAsString; + + // --------------------------------------------------------------------------------------------- + + private DoubleTermsBucket(Builder builder) { + super(builder); + this.key = ApiTypeHelper.requireNonNull(builder.key, this, "key"); + this.keyAsString = builder.keyAsString; + } + + public static DoubleTermsBucket of(Function> fn) { + return fn.apply(new Builder()).build(); + } + + /** + * Required - API name: {@code key} + */ + public final double key() { + return this.key; + } + + /** + * API name: {@code key_as_string} + */ + @Nullable + public final String keyAsString() { + return this.keyAsString; + } + + protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { + super.serializeInternal(generator, mapper); + generator.writeKey("key"); + generator.write(this.key); + + if (this.keyAsString != null) { + generator.writeKey("key_as_string"); + generator.write(this.keyAsString); + } + } + + // --------------------------------------------------------------------------------------------- + + @Override + @Nonnull + public Builder toBuilder() { + return new Builder(this); + } + + @Nonnull + public static Builder builder() { + return new Builder(); + } + + /** + * Builder for {@link DoubleTermsBucket}. + */ + public static class Builder extends TermsBucketBase.AbstractBuilder implements CopyableBuilder { + private Double key; + @Nullable + private String keyAsString; + + public Builder() {} + + private Builder(DoubleTermsBucket o) { + super(o); + this.key = o.key; + this.keyAsString = o.keyAsString; + } + + private Builder(Builder o) { + super(o); + this.key = o.key; + this.keyAsString = o.keyAsString; + } + + @Override + @Nonnull + public Builder copy() { + return new Builder(this); + } + + @Override + @Nonnull + protected Builder self() { + return this; + } + + /** + * Required - API name: {@code key} + */ + @Nonnull + public final Builder key(double value) { + this.key = value; + return this; + } + + /** + * API name: {@code key_as_string} + */ + @Nonnull + public final Builder keyAsString(@Nullable String value) { + this.keyAsString = value; + return this; + } + + /** + * Builds a {@link DoubleTermsBucket}. + * + * @throws NullPointerException if some of the required fields are null. + */ + @Override + @Nonnull + public DoubleTermsBucket build() { + _checkSingleUse(); + + return new DoubleTermsBucket(this); + } + } + + // --------------------------------------------------------------------------------------------- + + /** + * Json deserializer for {@link DoubleTermsBucket} + */ + public static final JsonpDeserializer _DESERIALIZER = ObjectBuilderDeserializer.lazy( + Builder::new, + DoubleTermsBucket::setupDoubleTermsBucketDeserializer + ); + + protected static void setupDoubleTermsBucketDeserializer(ObjectDeserializer op) { + setupTermsBucketBaseDeserializer(op); + op.add(Builder::key, JsonpDeserializer.doubleDeserializer(), "key"); + op.add(Builder::keyAsString, JsonpDeserializer.stringDeserializer(), "key_as_string"); + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + Double.hashCode(this.key); + result = 31 * result + Objects.hashCode(this.keyAsString); + return result; + } + + @Override + public boolean equals(Object o) { + if (!super.equals(o)) { + return false; + } + if (this == o) return true; + if (o == null || this.getClass() != o.getClass()) return false; + DoubleTermsBucket other = (DoubleTermsBucket) o; + return this.key == other.key && Objects.equals(this.keyAsString, other.keyAsString); + } +} diff --git a/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/LongTermsAggregate.java b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/LongTermsAggregate.java index 219af15276..84f1bdba3f 100644 --- a/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/LongTermsAggregate.java +++ b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/LongTermsAggregate.java @@ -39,7 +39,6 @@ import java.util.function.Function; import javax.annotation.Generated; import javax.annotation.Nonnull; -import org.opensearch.client.json.JsonData; import org.opensearch.client.json.JsonpDeserializable; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.ObjectBuilderDeserializer; @@ -55,7 +54,7 @@ */ @JsonpDeserializable @Generated("org.opensearch.client.codegen.CodeGenerator") -public class LongTermsAggregate extends TermsAggregateBase +public class LongTermsAggregate extends TermsAggregateBase implements AggregateVariant, ToCopyableBuilder { @@ -94,7 +93,7 @@ public static Builder builder() { /** * Builder for {@link LongTermsAggregate}. */ - public static class Builder extends TermsAggregateBase.AbstractBuilder + public static class Builder extends TermsAggregateBase.AbstractBuilder implements CopyableBuilder { @@ -145,7 +144,7 @@ public LongTermsAggregate build() { ); protected static void setupLongTermsAggregateDeserializer(ObjectDeserializer op) { - setupTermsAggregateBaseDeserializer(op, JsonData._DESERIALIZER); + setupTermsAggregateBaseDeserializer(op, LongTermsBucket._DESERIALIZER); } @Override diff --git a/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/LongTermsBucket.java b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/LongTermsBucket.java new file mode 100644 index 0000000000..b83db9853b --- /dev/null +++ b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/LongTermsBucket.java @@ -0,0 +1,227 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +//---------------------------------------------------- +// THIS CODE IS GENERATED. MANUAL EDITS WILL BE LOST. +//---------------------------------------------------- + +package org.opensearch.client.opensearch._types.aggregations; + +import jakarta.json.stream.JsonGenerator; +import java.util.Objects; +import java.util.function.Function; +import javax.annotation.Generated; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.opensearch.client.json.JsonpDeserializable; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.ObjectBuilderDeserializer; +import org.opensearch.client.json.ObjectDeserializer; +import org.opensearch.client.util.ApiTypeHelper; +import org.opensearch.client.util.CopyableBuilder; +import org.opensearch.client.util.ObjectBuilder; +import org.opensearch.client.util.ToCopyableBuilder; + +// typedef: _types.aggregations.LongTermsBucket + +@JsonpDeserializable +@Generated("org.opensearch.client.codegen.CodeGenerator") +public class LongTermsBucket extends TermsBucketBase implements ToCopyableBuilder { + + @Nonnull + private final LongTermsBucketKey key; + + @Nullable + private final String keyAsString; + + // --------------------------------------------------------------------------------------------- + + private LongTermsBucket(Builder builder) { + super(builder); + this.key = ApiTypeHelper.requireNonNull(builder.key, this, "key"); + this.keyAsString = builder.keyAsString; + } + + public static LongTermsBucket of(Function> fn) { + return fn.apply(new Builder()).build(); + } + + /** + * Required - API name: {@code key} + */ + @Nonnull + public final LongTermsBucketKey key() { + return this.key; + } + + /** + * API name: {@code key_as_string} + */ + @Nullable + public final String keyAsString() { + return this.keyAsString; + } + + protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { + super.serializeInternal(generator, mapper); + generator.writeKey("key"); + this.key.serialize(generator, mapper); + + if (this.keyAsString != null) { + generator.writeKey("key_as_string"); + generator.write(this.keyAsString); + } + } + + // --------------------------------------------------------------------------------------------- + + @Override + @Nonnull + public Builder toBuilder() { + return new Builder(this); + } + + @Nonnull + public static Builder builder() { + return new Builder(); + } + + /** + * Builder for {@link LongTermsBucket}. + */ + public static class Builder extends TermsBucketBase.AbstractBuilder implements CopyableBuilder { + private LongTermsBucketKey key; + @Nullable + private String keyAsString; + + public Builder() {} + + private Builder(LongTermsBucket o) { + super(o); + this.key = o.key; + this.keyAsString = o.keyAsString; + } + + private Builder(Builder o) { + super(o); + this.key = o.key; + this.keyAsString = o.keyAsString; + } + + @Override + @Nonnull + public Builder copy() { + return new Builder(this); + } + + @Override + @Nonnull + protected Builder self() { + return this; + } + + /** + * Required - API name: {@code key} + */ + @Nonnull + public final Builder key(LongTermsBucketKey value) { + this.key = value; + return this; + } + + /** + * Required - API name: {@code key} + */ + @Nonnull + public final Builder key(Function> fn) { + return key(fn.apply(new LongTermsBucketKey.Builder()).build()); + } + + /** + * API name: {@code key_as_string} + */ + @Nonnull + public final Builder keyAsString(@Nullable String value) { + this.keyAsString = value; + return this; + } + + /** + * Builds a {@link LongTermsBucket}. + * + * @throws NullPointerException if some of the required fields are null. + */ + @Override + @Nonnull + public LongTermsBucket build() { + _checkSingleUse(); + + return new LongTermsBucket(this); + } + } + + // --------------------------------------------------------------------------------------------- + + /** + * Json deserializer for {@link LongTermsBucket} + */ + public static final JsonpDeserializer _DESERIALIZER = ObjectBuilderDeserializer.lazy( + Builder::new, + LongTermsBucket::setupLongTermsBucketDeserializer + ); + + protected static void setupLongTermsBucketDeserializer(ObjectDeserializer op) { + setupTermsBucketBaseDeserializer(op); + op.add(Builder::key, LongTermsBucketKey._DESERIALIZER, "key"); + op.add(Builder::keyAsString, JsonpDeserializer.stringDeserializer(), "key_as_string"); + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + this.key.hashCode(); + result = 31 * result + Objects.hashCode(this.keyAsString); + return result; + } + + @Override + public boolean equals(Object o) { + if (!super.equals(o)) { + return false; + } + if (this == o) return true; + if (o == null || this.getClass() != o.getClass()) return false; + LongTermsBucket other = (LongTermsBucket) o; + return this.key.equals(other.key) && Objects.equals(this.keyAsString, other.keyAsString); + } +} diff --git a/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/LongTermsBucketKey.java b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/LongTermsBucketKey.java new file mode 100644 index 0000000000..250d73ab04 --- /dev/null +++ b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/LongTermsBucketKey.java @@ -0,0 +1,221 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +//---------------------------------------------------- +// THIS CODE IS GENERATED. MANUAL EDITS WILL BE LOST. +//---------------------------------------------------- + +package org.opensearch.client.opensearch._types.aggregations; + +import jakarta.json.stream.JsonGenerator; +import java.util.Objects; +import java.util.function.Function; +import javax.annotation.Generated; +import javax.annotation.Nonnull; +import org.opensearch.client.json.JsonpDeserializable; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.JsonpSerializable; +import org.opensearch.client.json.PlainJsonSerializable; +import org.opensearch.client.json.UnionDeserializer; +import org.opensearch.client.util.ApiTypeHelper; +import org.opensearch.client.util.ObjectBuilder; +import org.opensearch.client.util.ObjectBuilderBase; +import org.opensearch.client.util.TaggedUnion; +import org.opensearch.client.util.TaggedUnionUtils; + +// typedef: _types.aggregations.LongTermsBucketKey + +@JsonpDeserializable +@Generated("org.opensearch.client.codegen.CodeGenerator") +public class LongTermsBucketKey implements TaggedUnion, PlainJsonSerializable { + /** + * {@link LongTermsBucketKey} variant kinds. + */ + public enum Kind { + Signed, + Unsigned + } + + private final Kind _kind; + private final Object _value; + + @Override + public final Kind _kind() { + return _kind; + } + + @Override + public final Object _get() { + return _value; + } + + private LongTermsBucketKey(Kind kind, Object value) { + this._kind = kind; + this._value = value; + } + + private LongTermsBucketKey(Builder builder) { + this._kind = ApiTypeHelper.requireNonNull(builder._kind, builder, ""); + this._value = ApiTypeHelper.requireNonNull(builder._value, builder, ""); + } + + public static LongTermsBucketKey of(Function> fn) { + return fn.apply(new Builder()).build(); + } + + public String _toJsonString() { + switch (_kind) { + case Signed: + return String.valueOf(this.signed()); + case Unsigned: + return this.unsigned(); + default: + throw new IllegalStateException("Unknown kind " + _kind); + } + } + + /** + * Is this variant instance of kind {@code signed}? + */ + public boolean isSigned() { + return _kind == Kind.Signed; + } + + /** + * Get the {@code signed} variant value. + * + * @throws IllegalStateException if the current variant is not the {@code signed} kind. + */ + public Long signed() { + return TaggedUnionUtils.get(this, Kind.Signed); + } + + /** + * Is this variant instance of kind {@code unsigned}? + */ + public boolean isUnsigned() { + return _kind == Kind.Unsigned; + } + + /** + * Get the {@code unsigned} variant value. + * + * @throws IllegalStateException if the current variant is not the {@code unsigned} kind. + */ + public String unsigned() { + return TaggedUnionUtils.get(this, Kind.Unsigned); + } + + @Override + public void serialize(JsonGenerator generator, JsonpMapper mapper) { + if (_value instanceof JsonpSerializable) { + ((JsonpSerializable) _value).serialize(generator, mapper); + } else { + switch (_kind) { + case Signed: + generator.write(((Long) this._value)); + break; + case Unsigned: + generator.write(((String) this._value)); + break; + } + } + } + + @Nonnull + public Builder toBuilder() { + return new Builder(this); + } + + @Nonnull + public static Builder builder() { + return new Builder(); + } + + public static class Builder extends ObjectBuilderBase implements ObjectBuilder { + private Kind _kind; + private Object _value; + + public Builder() {} + + private Builder(LongTermsBucketKey o) { + this._kind = o._kind; + this._value = o._value; + } + + public ObjectBuilder signed(Long v) { + this._kind = Kind.Signed; + this._value = v; + return this; + } + + public ObjectBuilder unsigned(String v) { + this._kind = Kind.Unsigned; + this._value = v; + return this; + } + + @Override + public LongTermsBucketKey build() { + _checkSingleUse(); + return new LongTermsBucketKey(this); + } + } + + private static JsonpDeserializer buildLongTermsBucketKeyDeserializer() { + return new UnionDeserializer.Builder(LongTermsBucketKey::new, false).addMember( + Kind.Signed, + JsonpDeserializer.longDeserializer() + ).addMember(Kind.Unsigned, JsonpDeserializer.stringDeserializer()).build(); + } + + public static final JsonpDeserializer _DESERIALIZER = JsonpDeserializer.lazy( + LongTermsBucketKey::buildLongTermsBucketKeyDeserializer + ); + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + Objects.hashCode(this._kind); + result = 31 * result + Objects.hashCode(this._value); + return result; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || this.getClass() != o.getClass()) return false; + LongTermsBucketKey other = (LongTermsBucketKey) o; + return Objects.equals(this._kind, other._kind) && Objects.equals(this._value, other._value); + } +} diff --git a/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/StringTermsAggregate.java b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/StringTermsAggregate.java index 32ce96cef5..4607d0518f 100644 --- a/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/StringTermsAggregate.java +++ b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/StringTermsAggregate.java @@ -39,7 +39,6 @@ import java.util.function.Function; import javax.annotation.Generated; import javax.annotation.Nonnull; -import org.opensearch.client.json.JsonData; import org.opensearch.client.json.JsonpDeserializable; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.ObjectBuilderDeserializer; @@ -55,7 +54,7 @@ */ @JsonpDeserializable @Generated("org.opensearch.client.codegen.CodeGenerator") -public class StringTermsAggregate extends TermsAggregateBase +public class StringTermsAggregate extends TermsAggregateBase implements AggregateVariant, ToCopyableBuilder { @@ -94,7 +93,7 @@ public static Builder builder() { /** * Builder for {@link StringTermsAggregate}. */ - public static class Builder extends TermsAggregateBase.AbstractBuilder + public static class Builder extends TermsAggregateBase.AbstractBuilder implements CopyableBuilder { @@ -145,7 +144,7 @@ public StringTermsAggregate build() { ); protected static void setupStringTermsAggregateDeserializer(ObjectDeserializer op) { - setupTermsAggregateBaseDeserializer(op, JsonData._DESERIALIZER); + setupTermsAggregateBaseDeserializer(op, StringTermsBucket._DESERIALIZER); } @Override diff --git a/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/StringTermsBucket.java b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/StringTermsBucket.java new file mode 100644 index 0000000000..09fedd6935 --- /dev/null +++ b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/StringTermsBucket.java @@ -0,0 +1,185 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +//---------------------------------------------------- +// THIS CODE IS GENERATED. MANUAL EDITS WILL BE LOST. +//---------------------------------------------------- + +package org.opensearch.client.opensearch._types.aggregations; + +import jakarta.json.stream.JsonGenerator; +import java.util.function.Function; +import javax.annotation.Generated; +import javax.annotation.Nonnull; +import org.opensearch.client.json.JsonpDeserializable; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.ObjectBuilderDeserializer; +import org.opensearch.client.json.ObjectDeserializer; +import org.opensearch.client.util.ApiTypeHelper; +import org.opensearch.client.util.CopyableBuilder; +import org.opensearch.client.util.ObjectBuilder; +import org.opensearch.client.util.ToCopyableBuilder; + +// typedef: _types.aggregations.StringTermsBucket + +@JsonpDeserializable +@Generated("org.opensearch.client.codegen.CodeGenerator") +public class StringTermsBucket extends TermsBucketBase implements ToCopyableBuilder { + + @Nonnull + private final String key; + + // --------------------------------------------------------------------------------------------- + + private StringTermsBucket(Builder builder) { + super(builder); + this.key = ApiTypeHelper.requireNonNull(builder.key, this, "key"); + } + + public static StringTermsBucket of(Function> fn) { + return fn.apply(new Builder()).build(); + } + + /** + * Required - API name: {@code key} + */ + @Nonnull + public final String key() { + return this.key; + } + + protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { + super.serializeInternal(generator, mapper); + generator.writeKey("key"); + generator.write(this.key); + } + + // --------------------------------------------------------------------------------------------- + + @Override + @Nonnull + public Builder toBuilder() { + return new Builder(this); + } + + @Nonnull + public static Builder builder() { + return new Builder(); + } + + /** + * Builder for {@link StringTermsBucket}. + */ + public static class Builder extends TermsBucketBase.AbstractBuilder implements CopyableBuilder { + private String key; + + public Builder() {} + + private Builder(StringTermsBucket o) { + super(o); + this.key = o.key; + } + + private Builder(Builder o) { + super(o); + this.key = o.key; + } + + @Override + @Nonnull + public Builder copy() { + return new Builder(this); + } + + @Override + @Nonnull + protected Builder self() { + return this; + } + + /** + * Required - API name: {@code key} + */ + @Nonnull + public final Builder key(String value) { + this.key = value; + return this; + } + + /** + * Builds a {@link StringTermsBucket}. + * + * @throws NullPointerException if some of the required fields are null. + */ + @Override + @Nonnull + public StringTermsBucket build() { + _checkSingleUse(); + + return new StringTermsBucket(this); + } + } + + // --------------------------------------------------------------------------------------------- + + /** + * Json deserializer for {@link StringTermsBucket} + */ + public static final JsonpDeserializer _DESERIALIZER = ObjectBuilderDeserializer.lazy( + Builder::new, + StringTermsBucket::setupStringTermsBucketDeserializer + ); + + protected static void setupStringTermsBucketDeserializer(ObjectDeserializer op) { + setupTermsBucketBaseDeserializer(op); + op.add(Builder::key, JsonpDeserializer.stringDeserializer(), "key"); + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + this.key.hashCode(); + return result; + } + + @Override + public boolean equals(Object o) { + if (!super.equals(o)) { + return false; + } + if (this == o) return true; + if (o == null || this.getClass() != o.getClass()) return false; + StringTermsBucket other = (StringTermsBucket) o; + return this.key.equals(other.key); + } +} diff --git a/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/TermsBucketBase.java b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/TermsBucketBase.java new file mode 100644 index 0000000000..df052f21df --- /dev/null +++ b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/TermsBucketBase.java @@ -0,0 +1,138 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +//---------------------------------------------------- +// THIS CODE IS GENERATED. MANUAL EDITS WILL BE LOST. +//---------------------------------------------------- + +package org.opensearch.client.opensearch._types.aggregations; + +import jakarta.json.stream.JsonGenerator; +import java.util.Objects; +import javax.annotation.Generated; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.ObjectDeserializer; + +// typedef: _types.aggregations.TermsBucketBase + +@Generated("org.opensearch.client.codegen.CodeGenerator") +public abstract class TermsBucketBase extends MultiBucketBase { + + @Nullable + private final Long docCountErrorUpperBound; + + // --------------------------------------------------------------------------------------------- + + protected TermsBucketBase(AbstractBuilder builder) { + super(builder); + this.docCountErrorUpperBound = builder.docCountErrorUpperBound; + } + + /** + * Upper bound of the document count error. Only present when show_term_doc_count_error is true. + *

+ * API name: {@code doc_count_error_upper_bound} + *

+ */ + @Nullable + public final Long docCountErrorUpperBound() { + return this.docCountErrorUpperBound; + } + + protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { + super.serializeInternal(generator, mapper); + if (this.docCountErrorUpperBound != null) { + generator.writeKey("doc_count_error_upper_bound"); + generator.write(this.docCountErrorUpperBound); + } + } + + // --------------------------------------------------------------------------------------------- + + public abstract static class AbstractBuilder> extends MultiBucketBase.AbstractBuilder< + BuilderT> { + @Nullable + private Long docCountErrorUpperBound; + + protected AbstractBuilder() {} + + protected AbstractBuilder(TermsBucketBase o) { + super(o); + this.docCountErrorUpperBound = o.docCountErrorUpperBound; + } + + protected AbstractBuilder(AbstractBuilder o) { + super(o); + this.docCountErrorUpperBound = o.docCountErrorUpperBound; + } + + /** + * Upper bound of the document count error. Only present when show_term_doc_count_error is true. + *

+ * API name: {@code doc_count_error_upper_bound} + *

+ */ + @Nonnull + public final BuilderT docCountErrorUpperBound(@Nullable Long value) { + this.docCountErrorUpperBound = value; + return self(); + } + } + + // --------------------------------------------------------------------------------------------- + + protected static > void setupTermsBucketBaseDeserializer(ObjectDeserializer op) { + setupMultiBucketBaseDeserializer(op); + op.add(AbstractBuilder::docCountErrorUpperBound, JsonpDeserializer.longDeserializer(), "doc_count_error_upper_bound"); + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + Objects.hashCode(this.docCountErrorUpperBound); + return result; + } + + @Override + public boolean equals(Object o) { + if (!super.equals(o)) { + return false; + } + if (this == o) return true; + if (o == null || this.getClass() != o.getClass()) return false; + TermsBucketBase other = (TermsBucketBase) o; + return Objects.equals(this.docCountErrorUpperBound, other.docCountErrorUpperBound); + } +} diff --git a/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/UnmappedTermsAggregate.java b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/UnmappedTermsAggregate.java index 72d86d1ba2..baa2fc91e0 100644 --- a/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/UnmappedTermsAggregate.java +++ b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/UnmappedTermsAggregate.java @@ -39,7 +39,6 @@ import java.util.function.Function; import javax.annotation.Generated; import javax.annotation.Nonnull; -import org.opensearch.client.json.JsonData; import org.opensearch.client.json.JsonpDeserializable; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.ObjectBuilderDeserializer; @@ -55,7 +54,7 @@ */ @JsonpDeserializable @Generated("org.opensearch.client.codegen.CodeGenerator") -public class UnmappedTermsAggregate extends TermsAggregateBase +public class UnmappedTermsAggregate extends TermsAggregateBase implements AggregateVariant, ToCopyableBuilder { @@ -94,7 +93,7 @@ public static Builder builder() { /** * Builder for {@link UnmappedTermsAggregate}. */ - public static class Builder extends TermsAggregateBase.AbstractBuilder + public static class Builder extends TermsAggregateBase.AbstractBuilder implements CopyableBuilder { @@ -145,7 +144,7 @@ public UnmappedTermsAggregate build() { ); protected static void setupUnmappedTermsAggregateDeserializer(ObjectDeserializer op) { - setupTermsAggregateBaseDeserializer(op, JsonData._DESERIALIZER); + setupTermsAggregateBaseDeserializer(op, JsonpDeserializer.voidDeserializer()); } @Override diff --git a/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/UnsignedLongTermsAggregate.java b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/UnsignedLongTermsAggregate.java index 05ed300405..c006a82d5a 100644 --- a/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/UnsignedLongTermsAggregate.java +++ b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/UnsignedLongTermsAggregate.java @@ -39,7 +39,6 @@ import java.util.function.Function; import javax.annotation.Generated; import javax.annotation.Nonnull; -import org.opensearch.client.json.JsonData; import org.opensearch.client.json.JsonpDeserializable; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.ObjectBuilderDeserializer; @@ -59,7 +58,7 @@ */ @JsonpDeserializable @Generated("org.opensearch.client.codegen.CodeGenerator") -public class UnsignedLongTermsAggregate extends TermsAggregateBase +public class UnsignedLongTermsAggregate extends TermsAggregateBase implements AggregateVariant, ToCopyableBuilder { @@ -100,7 +99,7 @@ public static Builder builder() { /** * Builder for {@link UnsignedLongTermsAggregate}. */ - public static class Builder extends TermsAggregateBase.AbstractBuilder + public static class Builder extends TermsAggregateBase.AbstractBuilder implements CopyableBuilder { @@ -151,7 +150,7 @@ public UnsignedLongTermsAggregate build() { ); protected static void setupUnsignedLongTermsAggregateDeserializer(ObjectDeserializer op) { - setupTermsAggregateBaseDeserializer(op, JsonData._DESERIALIZER); + setupTermsAggregateBaseDeserializer(op, UnsignedLongTermsBucket._DESERIALIZER); } @Override diff --git a/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/UnsignedLongTermsBucket.java b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/UnsignedLongTermsBucket.java new file mode 100644 index 0000000000..ea94cef334 --- /dev/null +++ b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/UnsignedLongTermsBucket.java @@ -0,0 +1,246 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +//---------------------------------------------------- +// THIS CODE IS GENERATED. MANUAL EDITS WILL BE LOST. +//---------------------------------------------------- + +package org.opensearch.client.opensearch._types.aggregations; + +import jakarta.json.stream.JsonGenerator; +import java.util.Objects; +import java.util.function.Function; +import javax.annotation.Generated; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.opensearch.client.json.JsonpDeserializable; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.ObjectBuilderDeserializer; +import org.opensearch.client.json.ObjectDeserializer; +import org.opensearch.client.util.ApiTypeHelper; +import org.opensearch.client.util.CopyableBuilder; +import org.opensearch.client.util.ObjectBuilder; +import org.opensearch.client.util.ToCopyableBuilder; + +// typedef: _types.aggregations.UnsignedLongTermsBucket + +/** + * Terms bucket for unsigned_long field type. The server returns 64-bit unsigned integer values as Long or BigInteger types. + * Due to JSON specification limitations (IEEE 754 double precision provides only 53 bits of integer precision), the key is represented as + * type: number in the OpenAPI spec. For protocol buffer definitions, this is manually overridden to uint64 to + * preserve the full 64-bit unsigned integer range in binary encoding. + */ +@JsonpDeserializable +@Generated("org.opensearch.client.codegen.CodeGenerator") +public class UnsignedLongTermsBucket extends TermsBucketBase + implements + ToCopyableBuilder { + + @Nonnull + private final Number key; + + @Nullable + private final String keyAsString; + + // --------------------------------------------------------------------------------------------- + + private UnsignedLongTermsBucket(Builder builder) { + super(builder); + this.key = ApiTypeHelper.requireNonNull(builder.key, this, "key"); + this.keyAsString = builder.keyAsString; + } + + public static UnsignedLongTermsBucket of(Function> fn) { + return fn.apply(new Builder()).build(); + } + + /** + * Required - The unsigned 64-bit integer bucket key. While JSON represents this as a number, values larger than 2^53--1 may lose + * precision in JSON serialization. The gRPC protocol buffer definition uses uint64 to preserve full precision. Use + * key_as_string for exact string representation of large values. + *

+ * API name: {@code key} + *

+ */ + @Nonnull + public final Number key() { + return this.key; + } + + /** + * String representation of the key. Only present when a custom format parameter is specified in the aggregation request. + *

+ * API name: {@code key_as_string} + *

+ */ + @Nullable + public final String keyAsString() { + return this.keyAsString; + } + + protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { + super.serializeInternal(generator, mapper); + generator.writeKey("key"); + generator.write(this.key.doubleValue()); + + if (this.keyAsString != null) { + generator.writeKey("key_as_string"); + generator.write(this.keyAsString); + } + } + + // --------------------------------------------------------------------------------------------- + + @Override + @Nonnull + public Builder toBuilder() { + return new Builder(this); + } + + @Nonnull + public static Builder builder() { + return new Builder(); + } + + /** + * Builder for {@link UnsignedLongTermsBucket}. + */ + public static class Builder extends TermsBucketBase.AbstractBuilder + implements + CopyableBuilder { + private Number key; + @Nullable + private String keyAsString; + + public Builder() {} + + private Builder(UnsignedLongTermsBucket o) { + super(o); + this.key = o.key; + this.keyAsString = o.keyAsString; + } + + private Builder(Builder o) { + super(o); + this.key = o.key; + this.keyAsString = o.keyAsString; + } + + @Override + @Nonnull + public Builder copy() { + return new Builder(this); + } + + @Override + @Nonnull + protected Builder self() { + return this; + } + + /** + * Required - The unsigned 64-bit integer bucket key. While JSON represents this as a number, values larger than 2^53--1 may lose + * precision in JSON serialization. The gRPC protocol buffer definition uses uint64 to preserve full precision. Use + * key_as_string for exact string representation of large values. + *

+ * API name: {@code key} + *

+ */ + @Nonnull + public final Builder key(Number value) { + this.key = value; + return this; + } + + /** + * String representation of the key. Only present when a custom format parameter is specified in the aggregation + * request. + *

+ * API name: {@code key_as_string} + *

+ */ + @Nonnull + public final Builder keyAsString(@Nullable String value) { + this.keyAsString = value; + return this; + } + + /** + * Builds a {@link UnsignedLongTermsBucket}. + * + * @throws NullPointerException if some of the required fields are null. + */ + @Override + @Nonnull + public UnsignedLongTermsBucket build() { + _checkSingleUse(); + + return new UnsignedLongTermsBucket(this); + } + } + + // --------------------------------------------------------------------------------------------- + + /** + * Json deserializer for {@link UnsignedLongTermsBucket} + */ + public static final JsonpDeserializer _DESERIALIZER = ObjectBuilderDeserializer.lazy( + Builder::new, + UnsignedLongTermsBucket::setupUnsignedLongTermsBucketDeserializer + ); + + protected static void setupUnsignedLongTermsBucketDeserializer(ObjectDeserializer op) { + setupTermsBucketBaseDeserializer(op); + op.add(Builder::key, JsonpDeserializer.numberDeserializer(), "key"); + op.add(Builder::keyAsString, JsonpDeserializer.stringDeserializer(), "key_as_string"); + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + this.key.hashCode(); + result = 31 * result + Objects.hashCode(this.keyAsString); + return result; + } + + @Override + public boolean equals(Object o) { + if (!super.equals(o)) { + return false; + } + if (this == o) return true; + if (o == null || this.getClass() != o.getClass()) return false; + UnsignedLongTermsBucket other = (UnsignedLongTermsBucket) o; + return this.key.equals(other.key) && Objects.equals(this.keyAsString, other.keyAsString); + } +} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java index 747fcf1e63..6efce2e340 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java @@ -49,7 +49,6 @@ import org.opensearch.client.opensearch._types.Time; import org.opensearch.client.opensearch._types.aggregations.Aggregate; import org.opensearch.client.opensearch._types.aggregations.HistogramAggregate; -import org.opensearch.client.opensearch._types.aggregations.MultiTermsBucket; import org.opensearch.client.opensearch._types.analysis.Analyzer; import org.opensearch.client.opensearch._types.analysis.CustomAnalyzer; import org.opensearch.client.opensearch._types.analysis.ShingleTokenFilter; @@ -478,8 +477,8 @@ public void testSubAggregation() throws IOException { SearchResponse searchResponse = javaClient().search(searchRequest, Product.class); Aggregate prices = searchResponse.aggregations().get("price")._get().toAggregate(); - assertEquals(1, prices.dterms().buckets().array().get(0).to(MultiTermsBucket.class).docCount()); - assertEquals(1, prices.dterms().buckets().array().get(1).to(MultiTermsBucket.class).docCount()); + assertEquals(1, prices.dterms().buckets().array().get(0).docCount()); + assertEquals(1, prices.dterms().buckets().array().get(1).docCount()); // We've set "size" to zero assertEquals(0, searchResponse.hits().hits().size()); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractSearchTemplateRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractSearchTemplateRequestIT.java index dd02232cf0..61e72ea1cc 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractSearchTemplateRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractSearchTemplateRequestIT.java @@ -16,7 +16,6 @@ import org.opensearch.client.json.JsonData; import org.opensearch.client.opensearch._types.BuiltinScriptLanguage; import org.opensearch.client.opensearch._types.Refresh; -import org.opensearch.client.opensearch._types.aggregations.MultiTermsBucket; import org.opensearch.client.opensearch._types.mapping.Property; import org.opensearch.client.opensearch.core.PutScriptRequest; import org.opensearch.client.opensearch.core.SearchTemplateResponse; @@ -85,8 +84,8 @@ public void testTemplateSearchAggregations() throws Exception { assertEquals(1, searchResponse.aggregations().size()); var buckets = searchResponse.aggregations().get("test-aggs").sterms().buckets().array(); assertEquals(2, buckets.size()); - assertEquals(2, buckets.get(0).to(MultiTermsBucket.class).docCount()); - assertEquals(2, buckets.get(1).to(MultiTermsBucket.class).docCount()); + assertEquals(2, buckets.get(0).docCount()); + assertEquals(2, buckets.get(1).docCount()); } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/model/TypedKeysTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/model/TypedKeysTest.java index 9d5615b7bd..ebab34bf58 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/model/TypedKeysTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/model/TypedKeysTest.java @@ -37,13 +37,12 @@ import java.util.Arrays; import java.util.Collections; import org.junit.Test; -import org.opensearch.client.json.JsonData; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapperAttributes; import org.opensearch.client.opensearch._types.aggregations.Aggregate; import org.opensearch.client.opensearch._types.aggregations.AvgAggregate; -import org.opensearch.client.opensearch._types.aggregations.StringRareTermsBucket; import org.opensearch.client.opensearch._types.aggregations.StringTermsAggregate; +import org.opensearch.client.opensearch._types.aggregations.StringTermsBucket; import org.opensearch.client.opensearch.core.SearchResponse; import org.opensearch.client.opensearch.core.search.TotalHitsRelation; import org.opensearch.client.util.MapBuilder; @@ -84,12 +83,8 @@ public void testAdditionalProperties() { .buckets( b -> b.array( Arrays.asList( - JsonData.of( - StringRareTermsBucket.builder().key("key_1").docCount(1).aggregations(MapBuilder.of("bar", avg1)).build() - ), - JsonData.of( - StringRareTermsBucket.builder().key("key_2").docCount(2).aggregations(MapBuilder.of("bar", avg2)).build() - ) + StringTermsBucket.builder().key("key_1").docCount(1).aggregations(MapBuilder.of("bar", avg1)).build(), + StringTermsBucket.builder().key("key_2").docCount(2).aggregations(MapBuilder.of("bar", avg2)).build() ) ) ) @@ -117,11 +112,11 @@ public void testAdditionalProperties() { StringTermsAggregate foo = resp.aggregations().get("foo").sterms(); assertEquals((Long) 1L, foo.sumOtherDocCount()); - assertEquals(1, foo.buckets().array().get(0).to(StringRareTermsBucket.class).docCount()); - assertEquals("key_1", foo.buckets().array().get(0).to(StringRareTermsBucket.class).key()); - assertEquals(1.0, foo.buckets().array().get(0).to(StringRareTermsBucket.class).aggregations().get("bar").avg().value(), 0.01); - assertEquals("key_2", foo.buckets().array().get(1).to(StringRareTermsBucket.class).key()); - assertEquals(2.0, foo.buckets().array().get(1).to(StringRareTermsBucket.class).aggregations().get("bar").avg().value(), 0.01); + assertEquals(1, foo.buckets().array().get(0).docCount()); + assertEquals("key_1", foo.buckets().array().get(0).key()); + assertEquals(1.0, foo.buckets().array().get(0).aggregations().get("bar").avg().value(), 0.01); + assertEquals("key_2", foo.buckets().array().get(1).key()); + assertEquals(2.0, foo.buckets().array().get(1).aggregations().get("bar").avg().value(), 0.01); } @Test diff --git a/java-codegen/src/main/java/org/opensearch/client/codegen/openapi/rewriter/GenericLabelingVisitor.java b/java-codegen/src/main/java/org/opensearch/client/codegen/openapi/rewriter/GenericLabelingVisitor.java index 06d381a65f..ef1899fd15 100644 --- a/java-codegen/src/main/java/org/opensearch/client/codegen/openapi/rewriter/GenericLabelingVisitor.java +++ b/java-codegen/src/main/java/org/opensearch/client/codegen/openapi/rewriter/GenericLabelingVisitor.java @@ -55,11 +55,38 @@ private boolean isInstantiatedGeneric(OpenApiSchema schema) { if ($extends == null || !$extends.isGeneric()) { return false; } + // `isGeneric` also propagates through plain references (e.g. MultiBucketBase.additionalProperties -> Aggregate -> + // TopHitsAggregate.hits -> HitsMetadata), so a schema can be labeled generic without declaring a + // type parameter of its own. Only a schema whose own chain declares one can be instantiated. + if (!declaresTypeParameter($extends)) { + return false; + } var visitor = new IsInstantiatedGenericVisitor(); new ZippedOpenApiWalker().walkSchema(resolve($extends), schema, visitor); return visitor.result != null && visitor.result; } + // Whether the generic schema, or a schema it extends, declares a generic type parameter in its own structure + // (properties, items, additionalProperties, union branches), without following references to other schemas. + private boolean declaresTypeParameter(OpenApiSchema generic) { + for (var schema = generic; schema != null; schema = schema.resolve().get$extends().orElse(null)) { + var resolved = schema.resolve(); + var found = new boolean[1]; + new ZippedOpenApiWalker().walkSchema(resolved, resolved, new ZippedOpenApiVisitorBase() { + @Override + public void visitSchemaEnd(@Nullable OpenApiSchema left, @Nullable OpenApiSchema right) { + if (left != null && left.isGenericTypeParameter()) { + found[0] = true; + } + } + }); + if (found[0]) { + return true; + } + } + return false; + } + private static final class IsInstantiatedGenericVisitor extends ZippedOpenApiVisitorBase { private Boolean result = null; diff --git a/java-codegen/src/main/java/org/opensearch/client/codegen/transformer/TypeMapper.java b/java-codegen/src/main/java/org/opensearch/client/codegen/transformer/TypeMapper.java index 30875c672b..a0aa8f8494 100644 --- a/java-codegen/src/main/java/org/opensearch/client/codegen/transformer/TypeMapper.java +++ b/java-codegen/src/main/java/org/opensearch/client/codegen/transformer/TypeMapper.java @@ -9,10 +9,13 @@ package org.opensearch.client.codegen.transformer; import java.util.Arrays; -import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -285,8 +288,11 @@ private TypeRef mapInstantiatedGeneric(OpenApiSchema schema) { var $extends = schema.get$extends().orElseThrow(); var type = mapType($extends); var shape = type.getTargetShape().orElseThrow(); - var typeParameterDefs = shape.getTypeParameters().getTypeParameterDefinitions(); - var typeParameters = new HashMap(); + // The generic shape's type parameters are collected as its fields are added. When it is instantiated while it is + // still being visited (a cycle: Hit -> inner_hits -> HitsMetadata -> Hit), none are known yet, and the + // parameters found by zipping the generic schema with this instantiation are used instead. + var shapeTypeParameters = shape.getTypeParameters(); + var typeParameters = new LinkedHashMap(); while ($extends != null) { $extends = $extends.resolve(); @@ -294,26 +300,67 @@ private TypeRef mapInstantiatedGeneric(OpenApiSchema schema) { new ZippedOpenApiWalker().walkSchema($extends, schema, new ZippedOpenApiVisitorBase() { @Override public void visitSchemaEnd(@Nullable OpenApiSchema left, @Nullable OpenApiSchema right) { - if (left == null || right == null || !left.isGenericTypeParameter()) { + if (left == null || right == null) { return; } - typeParameters.put(getTypeParamName(left.resolve()), mapType(right)); + if (left.isGenericTypeParameter()) { + typeParameters.put(getTypeParamName(left.resolve()), mapType(right)); + return; + } + // A generic union narrowed to one of its branches: the generic declares e.g. `Buckets` as + // `oneOf: [map of TBucket, array of TBucket]` while the instantiation declares only `array of X`. + // The zipped walk never pairs TBucket with X, so bind it from the branch whose container kind matches. + var leftOneOf = left.getOneOf(); + if (leftOneOf.isEmpty() || right.hasOneOf()) { + return; + } + for (var branch : leftOneOf.get()) { + var resolvedBranch = branch.resolve(); + bindIfTypeParameter(resolvedBranch.getItems(), right.getItems()); + bindIfTypeParameter(resolvedBranch.getAdditionalProperties(), right.getAdditionalProperties()); + } + } + + private void bindIfTypeParameter(Optional leftChild, Optional rightChild) { + if (leftChild.isEmpty() || rightChild.isEmpty()) { + return; + } + var resolvedLeft = leftChild.get().resolve(); + if (resolvedLeft.isGenericTypeParameter()) { + typeParameters.put(getTypeParamName(resolvedLeft), mapType(rightChild.get())); + } } }); $extends = $extends.get$extends().orElse(null); } + var typeParameterNames = shapeTypeParameters != null + ? Arrays.stream(shapeTypeParameters.getTypeParameterDefinitions()).map(TypeParameterDefinition::getName) + : genericParameterNames(schema.get$extends().orElseThrow()).stream(); return shape.getMaterializedType() .withTypeParameters( - Arrays.stream(typeParameterDefs) - .map(TypeParameterDefinition::getName) - .map(n -> typeParameters.getOrDefault(n, Types.Client.Json.JsonData)) + typeParameterNames.map(n -> typeParameters.getOrDefault(n, Types.Client.Json.JsonData)) .map(TypeRef::getBoxed) .toArray(TypeRef[]::new) ); } + // The generic type parameters a generic schema (and the schemas it extends) declares, in the order they appear. + private static Set genericParameterNames(OpenApiSchema generic) { + var names = new LinkedHashSet(); + for (var schema = generic; schema != null; schema = schema.resolve().get$extends().orElse(null)) { + var resolved = schema.resolve(); + new ZippedOpenApiWalker().walkSchema(resolved, resolved, new ZippedOpenApiVisitorBase() { + @Override + public void visitSchemaEnd(@Nullable OpenApiSchema left, @Nullable OpenApiSchema right) { + if (left != null && left.isGenericTypeParameter()) names.add(getTypeParamName(left.resolve())); + } + }); + } + return names; + } + private boolean shouldKeepRef(OpenApiSchema schema) { if (schema.isEnum()) { return true; diff --git a/samples/samples-java/src/main/java/org/opensearch/client/samples/Search.java b/samples/samples-java/src/main/java/org/opensearch/client/samples/Search.java index ad6bbe3c5d..eaf314ee9d 100644 --- a/samples/samples-java/src/main/java/org/opensearch/client/samples/Search.java +++ b/samples/samples-java/src/main/java/org/opensearch/client/samples/Search.java @@ -22,7 +22,6 @@ import org.opensearch.client.opensearch._types.aggregations.Aggregation; import org.opensearch.client.opensearch._types.aggregations.CompositeAggregationFields; import org.opensearch.client.opensearch._types.aggregations.CompositeAggregationSource; -import org.opensearch.client.opensearch._types.aggregations.StringRareTermsBucket; import org.opensearch.client.opensearch._types.analysis.Analyzer; import org.opensearch.client.opensearch._types.analysis.CustomAnalyzer; import org.opensearch.client.opensearch._types.analysis.ShingleTokenFilter; @@ -108,13 +107,7 @@ public static void main(String[] args) { searchResponse = client.search(searchRequest, IndexData.class); for (Map.Entry entry : searchResponse.aggregations().entrySet()) { LOGGER.info("Agg - {}", entry.getKey()); - entry.getValue() - .sterms() - .buckets() - .array() - .stream() - .map(b -> b.to(StringRareTermsBucket.class)) - .forEach(b -> LOGGER.info("{} : {}", b.key(), b.docCount())); + entry.getValue().sterms().buckets().array().forEach(b -> LOGGER.info("{} : {}", b.key(), b.docCount())); } // Custom Aggregations