From 8b6b6b00d0e9f6b853dec09aef33ea1760cb3c29 Mon Sep 17 00:00:00 2001 From: Sylvain Wallez Date: Fri, 29 Apr 2022 15:15:00 +0200 Subject: [PATCH] Fix ScoreFunction: variants are optional (#257) --- .../_types/query_dsl/DecayFunctionBase.java | 20 ++- .../FieldValueFactorScoreFunction.java | 19 ++- .../_types/query_dsl/FunctionScore.java | 37 +++-- .../_types/query_dsl/RandomScoreFunction.java | 19 ++- .../_types/query_dsl/ScoreFunctionBase.java | 150 ------------------ .../_types/query_dsl/ScriptScoreFunction.java | 19 ++- .../elasticsearch/doc-files/api-spec.html | 27 ++-- .../elasticsearch/model/VariantsTest.java | 49 ++++++ 8 files changed, 146 insertions(+), 194 deletions(-) delete mode 100644 java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/ScoreFunctionBase.java diff --git a/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/DecayFunctionBase.java b/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/DecayFunctionBase.java index 9a267ba92..ed4ddb394 100644 --- a/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/DecayFunctionBase.java +++ b/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/DecayFunctionBase.java @@ -26,6 +26,7 @@ import co.elastic.clients.json.JsonpDeserializable; import co.elastic.clients.json.JsonpDeserializer; import co.elastic.clients.json.JsonpMapper; +import co.elastic.clients.json.JsonpSerializable; import co.elastic.clients.json.ObjectBuilderDeserializer; import co.elastic.clients.json.ObjectDeserializer; import co.elastic.clients.util.ObjectBuilder; @@ -43,14 +44,13 @@ * specification */ -public abstract class DecayFunctionBase extends ScoreFunctionBase { +public abstract class DecayFunctionBase implements JsonpSerializable { @Nullable private final MultiValueMode multiValueMode; // --------------------------------------------------------------------------------------------- protected DecayFunctionBase(AbstractBuilder builder) { - super(builder); this.multiValueMode = builder.multiValueMode; @@ -64,9 +64,17 @@ public final MultiValueMode multiValueMode() { return this.multiValueMode; } + /** + * Serialize this object to JSON. + */ + public void serialize(JsonGenerator generator, JsonpMapper mapper) { + generator.writeStartObject(); + serializeInternal(generator, mapper); + generator.writeEnd(); + } + protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { - super.serializeInternal(generator, mapper); if (this.multiValueMode != null) { generator.writeKey("multi_value_mode"); this.multiValueMode.serialize(generator, mapper); @@ -76,7 +84,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { protected abstract static class AbstractBuilder> extends - ScoreFunctionBase.AbstractBuilder { + WithJsonObjectBuilderBase { @Nullable private MultiValueMode multiValueMode; @@ -88,12 +96,14 @@ public final BuilderT multiValueMode(@Nullable MultiValueMode value) { return self(); } + protected abstract BuilderT self(); + } // --------------------------------------------------------------------------------------------- protected static > void setupDecayFunctionBaseDeserializer( ObjectDeserializer op) { - ScoreFunctionBase.setupScoreFunctionBaseDeserializer(op); + op.add(AbstractBuilder::multiValueMode, MultiValueMode._DESERIALIZER, "multi_value_mode"); } diff --git a/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/FieldValueFactorScoreFunction.java b/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/FieldValueFactorScoreFunction.java index 45d924c19..bf63c0d7c 100644 --- a/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/FieldValueFactorScoreFunction.java +++ b/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/FieldValueFactorScoreFunction.java @@ -26,10 +26,12 @@ import co.elastic.clients.json.JsonpDeserializable; import co.elastic.clients.json.JsonpDeserializer; import co.elastic.clients.json.JsonpMapper; +import co.elastic.clients.json.JsonpSerializable; import co.elastic.clients.json.ObjectBuilderDeserializer; import co.elastic.clients.json.ObjectDeserializer; import co.elastic.clients.util.ApiTypeHelper; import co.elastic.clients.util.ObjectBuilder; +import co.elastic.clients.util.WithJsonObjectBuilderBase; import jakarta.json.stream.JsonGenerator; import java.lang.Double; import java.lang.String; @@ -46,7 +48,7 @@ * specification */ @JsonpDeserializable -public class FieldValueFactorScoreFunction extends ScoreFunctionBase implements FunctionScoreVariant { +public class FieldValueFactorScoreFunction implements FunctionScoreVariant, JsonpSerializable { private final String field; @Nullable @@ -61,7 +63,6 @@ public class FieldValueFactorScoreFunction extends ScoreFunctionBase implements // --------------------------------------------------------------------------------------------- private FieldValueFactorScoreFunction(Builder builder) { - super(builder); this.field = ApiTypeHelper.requireNonNull(builder.field, this, "field"); this.factor = builder.factor; @@ -113,9 +114,17 @@ public final FieldValueFactorModifier modifier() { return this.modifier; } + /** + * Serialize this object to JSON. + */ + public void serialize(JsonGenerator generator, JsonpMapper mapper) { + generator.writeStartObject(); + serializeInternal(generator, mapper); + generator.writeEnd(); + } + protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { - super.serializeInternal(generator, mapper); generator.writeKey("field"); generator.write(this.field); @@ -142,7 +151,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { * Builder for {@link FieldValueFactorScoreFunction}. */ - public static class Builder extends ScoreFunctionBase.AbstractBuilder + public static class Builder extends WithJsonObjectBuilderBase implements ObjectBuilder { private String field; @@ -216,7 +225,7 @@ public FieldValueFactorScoreFunction build() { protected static void setupFieldValueFactorScoreFunctionDeserializer( ObjectDeserializer op) { - ScoreFunctionBase.setupScoreFunctionBaseDeserializer(op); + op.add(Builder::field, JsonpDeserializer.stringDeserializer(), "field"); op.add(Builder::factor, JsonpDeserializer.doubleDeserializer(), "factor"); op.add(Builder::missing, JsonpDeserializer.doubleDeserializer(), "missing"); diff --git a/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/FunctionScore.java b/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/FunctionScore.java index f6169e71d..7d32ce042 100644 --- a/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/FunctionScore.java +++ b/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/FunctionScore.java @@ -91,15 +91,19 @@ public String jsonValue() { } + @Nullable private final Kind _kind; + @Nullable private final Object _value; @Override + @Nullable public final Kind _kind() { return _kind; } @Override + @Nullable public final Object _get() { return _value; } @@ -110,10 +114,15 @@ public final Object _get() { @Nullable private final Double weight; - public FunctionScore(FunctionScoreVariant value) { + public FunctionScore(@Nullable FunctionScoreVariant value) { - this._kind = ApiTypeHelper.requireNonNull(value._functionScoreKind(), this, ""); - this._value = ApiTypeHelper.requireNonNull(value, this, ""); + if (value != null) { + this._kind = ApiTypeHelper.requireNonNull(value._functionScoreKind(), this, ""); + this._value = ApiTypeHelper.requireNonNull(value, this, ""); + } else { + this._kind = null; + this._value = null; + } this.filter = null; this.weight = null; @@ -122,8 +131,13 @@ public FunctionScore(FunctionScoreVariant value) { private FunctionScore(Builder builder) { - this._kind = ApiTypeHelper.requireNonNull(builder._kind, builder, ""); - this._value = ApiTypeHelper.requireNonNull(builder._value, builder, ""); + if (builder._value != null) { + this._kind = ApiTypeHelper.requireNonNull(builder._kind, builder, ""); + this._value = ApiTypeHelper.requireNonNull(builder._value, builder, ""); + } else { + this._kind = null; + this._value = null; + } this.filter = builder.filter; this.weight = builder.weight; @@ -270,16 +284,19 @@ public void serialize(JsonGenerator generator, JsonpMapper mapper) { } - generator.writeKey(_kind.jsonValue()); - if (_value instanceof JsonpSerializable) { - ((JsonpSerializable) _value).serialize(generator, mapper); + if (this._value != null) { + generator.writeKey(_kind.jsonValue()); + if (_value instanceof JsonpSerializable) { + ((JsonpSerializable) _value).serialize(generator, mapper); + } + } generator.writeEnd(); } - public static class Builder extends WithJsonObjectBuilderBase { + public static class Builder extends WithJsonObjectBuilderBase implements ObjectBuilder { private Kind _kind; private Object _value; @@ -379,7 +396,7 @@ public ContainerBuilder scriptScore( return this.scriptScore(fn.apply(new ScriptScoreFunction.Builder()).build()); } - protected FunctionScore build() { + public FunctionScore build() { _checkSingleUse(); return new FunctionScore(this); } diff --git a/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/RandomScoreFunction.java b/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/RandomScoreFunction.java index 8ddc0382b..1321004d2 100644 --- a/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/RandomScoreFunction.java +++ b/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/RandomScoreFunction.java @@ -26,9 +26,11 @@ import co.elastic.clients.json.JsonpDeserializable; import co.elastic.clients.json.JsonpDeserializer; import co.elastic.clients.json.JsonpMapper; +import co.elastic.clients.json.JsonpSerializable; import co.elastic.clients.json.ObjectBuilderDeserializer; import co.elastic.clients.json.ObjectDeserializer; import co.elastic.clients.util.ObjectBuilder; +import co.elastic.clients.util.WithJsonObjectBuilderBase; import jakarta.json.stream.JsonGenerator; import java.lang.String; import java.util.Objects; @@ -44,7 +46,7 @@ * specification */ @JsonpDeserializable -public class RandomScoreFunction extends ScoreFunctionBase implements FunctionScoreVariant { +public class RandomScoreFunction implements FunctionScoreVariant, JsonpSerializable { @Nullable private final String field; @@ -54,7 +56,6 @@ public class RandomScoreFunction extends ScoreFunctionBase implements FunctionSc // --------------------------------------------------------------------------------------------- private RandomScoreFunction(Builder builder) { - super(builder); this.field = builder.field; this.seed = builder.seed; @@ -89,9 +90,17 @@ public final String seed() { return this.seed; } + /** + * Serialize this object to JSON. + */ + public void serialize(JsonGenerator generator, JsonpMapper mapper) { + generator.writeStartObject(); + serializeInternal(generator, mapper); + generator.writeEnd(); + } + protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { - super.serializeInternal(generator, mapper); if (this.field != null) { generator.writeKey("field"); generator.write(this.field); @@ -111,7 +120,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { * Builder for {@link RandomScoreFunction}. */ - public static class Builder extends ScoreFunctionBase.AbstractBuilder + public static class Builder extends WithJsonObjectBuilderBase implements ObjectBuilder { @Nullable @@ -163,7 +172,7 @@ public RandomScoreFunction build() { .lazy(Builder::new, RandomScoreFunction::setupRandomScoreFunctionDeserializer); protected static void setupRandomScoreFunctionDeserializer(ObjectDeserializer op) { - ScoreFunctionBase.setupScoreFunctionBaseDeserializer(op); + op.add(Builder::field, JsonpDeserializer.stringDeserializer(), "field"); op.add(Builder::seed, JsonpDeserializer.stringDeserializer(), "seed"); diff --git a/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/ScoreFunctionBase.java b/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/ScoreFunctionBase.java deleted file mode 100644 index ddd2885b4..000000000 --- a/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/ScoreFunctionBase.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * 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. - */ - -//---------------------------------------------------- -// THIS CODE IS GENERATED. MANUAL EDITS WILL BE LOST. -//---------------------------------------------------- - -package co.elastic.clients.elasticsearch._types.query_dsl; - -import co.elastic.clients.json.JsonpDeserializable; -import co.elastic.clients.json.JsonpDeserializer; -import co.elastic.clients.json.JsonpMapper; -import co.elastic.clients.json.JsonpSerializable; -import co.elastic.clients.json.ObjectBuilderDeserializer; -import co.elastic.clients.json.ObjectDeserializer; -import co.elastic.clients.util.ObjectBuilder; -import co.elastic.clients.util.WithJsonObjectBuilderBase; -import jakarta.json.stream.JsonGenerator; -import java.lang.Double; -import java.util.Objects; -import java.util.function.Function; -import javax.annotation.Nullable; - -// typedef: _types.query_dsl.ScoreFunctionBase - -/** - * - * @see API - * specification - */ - -public abstract class ScoreFunctionBase implements JsonpSerializable { - @Nullable - private final Query filter; - - @Nullable - private final Double weight; - - // --------------------------------------------------------------------------------------------- - - protected ScoreFunctionBase(AbstractBuilder builder) { - - this.filter = builder.filter; - this.weight = builder.weight; - - } - - /** - * API name: {@code filter} - */ - @Nullable - public final Query filter() { - return this.filter; - } - - /** - * API name: {@code weight} - */ - @Nullable - public final Double weight() { - return this.weight; - } - - /** - * Serialize this object to JSON. - */ - public void serialize(JsonGenerator generator, JsonpMapper mapper) { - generator.writeStartObject(); - serializeInternal(generator, mapper); - generator.writeEnd(); - } - - protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { - - if (this.filter != null) { - generator.writeKey("filter"); - this.filter.serialize(generator, mapper); - - } - if (this.weight != null) { - generator.writeKey("weight"); - generator.write(this.weight); - - } - - } - - protected abstract static class AbstractBuilder> - extends - WithJsonObjectBuilderBase { - @Nullable - private Query filter; - - @Nullable - private Double weight; - - /** - * API name: {@code filter} - */ - public final BuilderT filter(@Nullable Query value) { - this.filter = value; - return self(); - } - - /** - * API name: {@code filter} - */ - public final BuilderT filter(Function> fn) { - return this.filter(fn.apply(new Query.Builder()).build()); - } - - /** - * API name: {@code weight} - */ - public final BuilderT weight(@Nullable Double value) { - this.weight = value; - return self(); - } - - protected abstract BuilderT self(); - - } - - // --------------------------------------------------------------------------------------------- - protected static > void setupScoreFunctionBaseDeserializer( - ObjectDeserializer op) { - - op.add(AbstractBuilder::filter, Query._DESERIALIZER, "filter"); - op.add(AbstractBuilder::weight, JsonpDeserializer.doubleDeserializer(), "weight"); - - } - -} diff --git a/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/ScriptScoreFunction.java b/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/ScriptScoreFunction.java index 727f3b6a6..50d662702 100644 --- a/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/ScriptScoreFunction.java +++ b/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/ScriptScoreFunction.java @@ -27,10 +27,12 @@ import co.elastic.clients.json.JsonpDeserializable; import co.elastic.clients.json.JsonpDeserializer; import co.elastic.clients.json.JsonpMapper; +import co.elastic.clients.json.JsonpSerializable; import co.elastic.clients.json.ObjectBuilderDeserializer; import co.elastic.clients.json.ObjectDeserializer; import co.elastic.clients.util.ApiTypeHelper; import co.elastic.clients.util.ObjectBuilder; +import co.elastic.clients.util.WithJsonObjectBuilderBase; import jakarta.json.stream.JsonGenerator; import java.util.Objects; import java.util.function.Function; @@ -45,13 +47,12 @@ * specification */ @JsonpDeserializable -public class ScriptScoreFunction extends ScoreFunctionBase implements FunctionScoreVariant { +public class ScriptScoreFunction implements FunctionScoreVariant, JsonpSerializable { private final Script script; // --------------------------------------------------------------------------------------------- private ScriptScoreFunction(Builder builder) { - super(builder); this.script = ApiTypeHelper.requireNonNull(builder.script, this, "script"); @@ -76,9 +77,17 @@ public final Script script() { return this.script; } + /** + * Serialize this object to JSON. + */ + public void serialize(JsonGenerator generator, JsonpMapper mapper) { + generator.writeStartObject(); + serializeInternal(generator, mapper); + generator.writeEnd(); + } + protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { - super.serializeInternal(generator, mapper); generator.writeKey("script"); this.script.serialize(generator, mapper); @@ -90,7 +99,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { * Builder for {@link ScriptScoreFunction}. */ - public static class Builder extends ScoreFunctionBase.AbstractBuilder + public static class Builder extends WithJsonObjectBuilderBase implements ObjectBuilder { private Script script; @@ -137,7 +146,7 @@ public ScriptScoreFunction build() { .lazy(Builder::new, ScriptScoreFunction::setupScriptScoreFunctionDeserializer); protected static void setupScriptScoreFunctionDeserializer(ObjectDeserializer op) { - ScoreFunctionBase.setupScoreFunctionBaseDeserializer(op); + op.add(Builder::script, Script._DESERIALIZER, "script"); } diff --git a/java-client/src/main/java/co/elastic/clients/elasticsearch/doc-files/api-spec.html b/java-client/src/main/java/co/elastic/clients/elasticsearch/doc-files/api-spec.html index b7a56b410..27b57e02f 100644 --- a/java-client/src/main/java/co/elastic/clients/elasticsearch/doc-files/api-spec.html +++ b/java-client/src/main/java/co/elastic/clients/elasticsearch/doc-files/api-spec.html @@ -747,19 +747,19 @@ '_types.query_dsl.CombinedFieldsZeroTerms': '_types/query_dsl/abstractions.ts#L209-L212', '_types.query_dsl.CommonTermsQuery': '_types/query_dsl/fulltext.ts#L33-L43', '_types.query_dsl.ConstantScoreQuery': '_types/query_dsl/compound.ts#L42-L44', -'_types.query_dsl.DecayFunction': '_types/query_dsl/compound.ts#L107-L112', -'_types.query_dsl.DecayFunctionBase': '_types/query_dsl/compound.ts#L91-L93', -'_types.query_dsl.DecayPlacement': '_types/query_dsl/compound.ts#L84-L89', +'_types.query_dsl.DecayFunction': '_types/query_dsl/compound.ts#L100-L105', +'_types.query_dsl.DecayFunctionBase': '_types/query_dsl/compound.ts#L84-L86', +'_types.query_dsl.DecayPlacement': '_types/query_dsl/compound.ts#L77-L82', '_types.query_dsl.DisMaxQuery': '_types/query_dsl/compound.ts#L46-L50', '_types.query_dsl.DistanceFeatureQuery': '_types/query_dsl/specialized.ts#L40-L44', '_types.query_dsl.ExistsQuery': '_types/query_dsl/term.ts#L36-L38', '_types.query_dsl.FieldAndFormat': '_types/query_dsl/abstractions.ts#L214-L228', '_types.query_dsl.FieldLookup': '_types/query_dsl/abstractions.ts#L166-L171', -'_types.query_dsl.FieldValueFactorModifier': '_types/query_dsl/compound.ts#L151-L162', -'_types.query_dsl.FieldValueFactorScoreFunction': '_types/query_dsl/compound.ts#L77-L82', -'_types.query_dsl.FunctionBoostMode': '_types/query_dsl/compound.ts#L142-L149', -'_types.query_dsl.FunctionScoreContainer': '_types/query_dsl/compound.ts#L114-L131', -'_types.query_dsl.FunctionScoreMode': '_types/query_dsl/compound.ts#L133-L140', +'_types.query_dsl.FieldValueFactorModifier': '_types/query_dsl/compound.ts#L145-L156', +'_types.query_dsl.FieldValueFactorScoreFunction': '_types/query_dsl/compound.ts#L70-L75', +'_types.query_dsl.FunctionBoostMode': '_types/query_dsl/compound.ts#L136-L143', +'_types.query_dsl.FunctionScoreContainer': '_types/query_dsl/compound.ts#L107-L125', +'_types.query_dsl.FunctionScoreMode': '_types/query_dsl/compound.ts#L127-L134', '_types.query_dsl.FunctionScoreQuery': '_types/query_dsl/compound.ts#L52-L59', '_types.query_dsl.FuzzyQuery': '_types/query_dsl/term.ts#L49-L60', '_types.query_dsl.GeoBoundingBoxQuery': '_types/query_dsl/geo.ts#L32-L41', @@ -793,7 +793,7 @@ '_types.query_dsl.MatchQuery': '_types/query_dsl/fulltext.ts#L133-L158', '_types.query_dsl.MoreLikeThisQuery': '_types/query_dsl/specialized.ts#L62-L89', '_types.query_dsl.MultiMatchQuery': '_types/query_dsl/fulltext.ts#L191-L217', -'_types.query_dsl.MultiValueMode': '_types/query_dsl/compound.ts#L164-L169', +'_types.query_dsl.MultiValueMode': '_types/query_dsl/compound.ts#L158-L163', '_types.query_dsl.NestedQuery': '_types/query_dsl/joining.ts#L63-L71', '_types.query_dsl.Operator': '_types/query_dsl/Operator.ts#L22-L27', '_types.query_dsl.ParentIdQuery': '_types/query_dsl/joining.ts#L73-L78', @@ -804,7 +804,7 @@ '_types.query_dsl.QueryBase': '_types/query_dsl/abstractions.ts#L177-L181', '_types.query_dsl.QueryContainer': '_types/query_dsl/abstractions.ts#L97-L164', '_types.query_dsl.QueryStringQuery': '_types/query_dsl/fulltext.ts#L233-L269', -'_types.query_dsl.RandomScoreFunction': '_types/query_dsl/compound.ts#L72-L75', +'_types.query_dsl.RandomScoreFunction': '_types/query_dsl/compound.ts#L65-L68', '_types.query_dsl.RangeQuery': '_types/query_dsl/term.ts#L81-L90', '_types.query_dsl.RangeQueryBase': '_types/query_dsl/term.ts#L77-L79', '_types.query_dsl.RangeRelation': '_types/query_dsl/term.ts#L105-L109', @@ -815,9 +815,8 @@ '_types.query_dsl.RankFeatureFunctionSigmoid': '_types/query_dsl/specialized.ts#L149-L152', '_types.query_dsl.RankFeatureQuery': '_types/query_dsl/specialized.ts#L154-L162', '_types.query_dsl.RegexpQuery': '_types/query_dsl/term.ts#L111-L123', -'_types.query_dsl.ScoreFunctionBase': '_types/query_dsl/compound.ts#L61-L64', '_types.query_dsl.ScriptQuery': '_types/query_dsl/specialized.ts#L164-L166', -'_types.query_dsl.ScriptScoreFunction': '_types/query_dsl/compound.ts#L68-L70', +'_types.query_dsl.ScriptScoreFunction': '_types/query_dsl/compound.ts#L61-L63', '_types.query_dsl.ScriptScoreQuery': '_types/query_dsl/specialized.ts#L168-L172', '_types.query_dsl.ShapeFieldQuery': '_types/query_dsl/specialized.ts#L183-L187', '_types.query_dsl.ShapeQuery': '_types/query_dsl/specialized.ts#L176-L181', @@ -2490,10 +2489,10 @@ if (hash.length > 1) { hash = hash.substring(1); } - window.location = "https://github.com/elastic/elasticsearch-specification/tree/7c3d1ce0febc9d78d7e385bf5ec503fb5e3af1e1/specification/" + (paths[hash] || ""); + window.location = "https://github.com/elastic/elasticsearch-specification/tree/4cbdef1d11586b7d6b27d3744dd15a6f0ce96afb/specification/" + (paths[hash] || ""); - Please see the Elasticsearch API specification. + Please see the Elasticsearch API specification. diff --git a/java-client/src/test/java/co/elastic/clients/elasticsearch/model/VariantsTest.java b/java-client/src/test/java/co/elastic/clients/elasticsearch/model/VariantsTest.java index aa56001cd..37499d454 100644 --- a/java-client/src/test/java/co/elastic/clients/elasticsearch/model/VariantsTest.java +++ b/java-client/src/test/java/co/elastic/clients/elasticsearch/model/VariantsTest.java @@ -21,6 +21,7 @@ import co.elastic.clients.elasticsearch._types.mapping.Property; import co.elastic.clients.elasticsearch._types.mapping.TypeMapping; +import co.elastic.clients.elasticsearch._types.query_dsl.FunctionScore; import co.elastic.clients.elasticsearch._types.query_dsl.Query; import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders; import co.elastic.clients.elasticsearch.core.SearchRequest; @@ -201,4 +202,52 @@ public void testNestedVariantsWithContainerProperties() { assertEquals("m1 value", search.aggregations().get("agg1").meta().get("m1").to(String.class)); assertEquals("m2 value", search.aggregations().get("agg1").meta().get("m2").to(String.class)); } + + @Test + public void testContainerWithOptionalVariants() { + // FunctionScore is the only occurrence of this + + Query q = QueryBuilders.term(t -> t.field("foo").value("bar")); + + // No variant + { + Query fsq = QueryBuilders.functionScore(fs -> fs + .query(q) + .functions(f -> f.weight(1.0)) + ); + + String json = "{\"function_score\":{\"functions\":[{\"weight\":1.0}]," + + "\"query\":{\"term\":{\"foo\":{\"value\":\"bar\"}}}}}"; + assertEquals(json, toJson(fsq)); + + Query fsq2 = checkJsonRoundtrip(fsq, json); + + assertNull(fsq2.functionScore().functions().get(0)._kind()); + assertEquals(1.0, fsq2.functionScore().functions().get(0).weight(), 0.001); + } + + // With a variant + { + Query fsq = QueryBuilders.functionScore(fs -> fs + .query(q) + .functions(f -> f + .weight(1.0) + .linear(l -> l + .field("foo") + .placement(p -> p.decay(2.0)) + ) + ) + ); + + String json = "{\"function_score\":{\"functions\":[{\"weight\":1.0,\"linear\":{\"foo\":{\"decay\":2.0}}}]," + + "\"query\":{\"term\":{\"foo\":{\"value\":\"bar\"}}}}}"; + assertEquals(json, toJson(fsq)); + + Query fsq2 = checkJsonRoundtrip(fsq, json); + + assertEquals(FunctionScore.Kind.Linear, fsq2.functionScore().functions().get(0)._kind()); + assertEquals(1.0, fsq2.functionScore().functions().get(0).weight(), 0.001); + assertEquals(2.0, fsq2.functionScore().functions().get(0).linear().placement().decay(), 0.001); + } + } }