From bc786139815d43eba55f5e526dcae36fa14a95cd Mon Sep 17 00:00:00 2001 From: Grogdunn Date: Wed, 10 Apr 2024 16:54:30 +0200 Subject: [PATCH] Add "filter" to neural query (#932) * :adhesive_bandage: add filter to neural query Signed-off-by: Lorenzo Caenazzo * CHANGELOG.md Signed-off-by: Lorenzo Caenazzo * :green_heart: spotless fix Signed-off-by: Lorenzo Caenazzo * :green_heart: spotless fix, jdk8 check Signed-off-by: Lorenzo Caenazzo * :green_heart: spotless fix Signed-off-by: Lorenzo Caenazzo * Update CHANGELOG.md Co-authored-by: Andriy Redko Signed-off-by: Grogdunn --------- Signed-off-by: Lorenzo Caenazzo Signed-off-by: Grogdunn Co-authored-by: Andriy Redko --- CHANGELOG.md | 3 +- .../_types/query_dsl/NeuralQuery.java | 34 ++++++++++++++++++- .../_types/query_dsl/NeuralQueryTest.java | 6 +++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f71e41fdf..58dac96040 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ This section is for maintaining a changelog for all breaking changes for the cli - Support weight function in function score query ([#880](https://github.com/opensearch-project/opensearch-java/pull/880)) - Fix pattern replace by making flag and replacement optional as on api ([#895](https://github.com/opensearch-project/opensearch-java/pull/895)) - Client with Java 8 runtime and Apache HttpClient 5 Transport fails with java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer ([#920](https://github.com/opensearch-project/opensearch-java/pull/920)) +- Add missed field "filter" to NeuralQuery model class ### Security @@ -364,4 +365,4 @@ This section is for maintaining a changelog for all breaking changes for the cli [2.5.0]: https://github.com/opensearch-project/opensearch-java/compare/v2.4.0...v2.5.0 [2.4.0]: https://github.com/opensearch-project/opensearch-java/compare/v2.3.0...v2.4.0 [2.3.0]: https://github.com/opensearch-project/opensearch-java/compare/v2.2.0...v2.3.0 -[2.2.0]: https://github.com/opensearch-project/opensearch-java/compare/v2.1.0...v2.2.0 \ No newline at end of file +[2.2.0]: https://github.com/opensearch-project/opensearch-java/compare/v2.1.0...v2.2.0 diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/_types/query_dsl/NeuralQuery.java b/java-client/src/main/java/org/opensearch/client/opensearch/_types/query_dsl/NeuralQuery.java index 4e2520d199..2bf6d0baef 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/_types/query_dsl/NeuralQuery.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/_types/query_dsl/NeuralQuery.java @@ -27,6 +27,8 @@ public class NeuralQuery extends QueryBase implements QueryVariant { private final int k; @Nullable private final String modelId; + @Nullable + private final Query filter; private NeuralQuery(NeuralQuery.Builder builder) { super(builder); @@ -35,6 +37,7 @@ private NeuralQuery(NeuralQuery.Builder builder) { this.queryText = ApiTypeHelper.requireNonNull(builder.queryText, this, "queryText"); this.k = ApiTypeHelper.requireNonNull(builder.k, this, "k"); this.modelId = builder.modelId; + this.filter = builder.filter; } public static NeuralQuery of(Function> fn) { @@ -93,6 +96,16 @@ public final String modelId() { return this.modelId; } + /** + * Optional - A query to filter the results of the query. + * + * @return The filter query. + */ + @Nullable + public final Query filter() { + return this.filter; + } + @Override protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { generator.writeStartObject(this.field); @@ -107,11 +120,16 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { generator.write("k", this.k); + if (this.filter != null) { + generator.writeKey("filter"); + this.filter.serialize(generator, mapper); + } + generator.writeEnd(); } public Builder toBuilder() { - return new Builder().field(field).queryText(queryText).k(k).modelId(modelId); + return new Builder().field(field).queryText(queryText).k(k).modelId(modelId).filter(filter); } /** @@ -123,6 +141,8 @@ public static class Builder extends QueryBase.AbstractBuilder builder.values("Some_ID")).toQuery()) + .build(); NeuralQuery copied = origin.toBuilder().build(); assertEquals(toJson(copied), toJson(origin));