From a16ca2dcf4b0aa88fb91c3fa03ae3b30cc5e1d86 Mon Sep 17 00:00:00 2001 From: jdnvn Date: Sun, 25 Aug 2024 15:27:01 -0400 Subject: [PATCH 1/3] add visitor logic to FunctionScoreQueryBuilder Signed-off-by: jdnvn --- .../functionscore/FunctionScoreQueryBuilder.java | 10 ++++++++++ .../functionscore/FunctionScoreQueryBuilderTests.java | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/server/src/main/java/org/opensearch/index/query/functionscore/FunctionScoreQueryBuilder.java b/server/src/main/java/org/opensearch/index/query/functionscore/FunctionScoreQueryBuilder.java index b3c797f11de6d..79ff7da1a36ba 100644 --- a/server/src/main/java/org/opensearch/index/query/functionscore/FunctionScoreQueryBuilder.java +++ b/server/src/main/java/org/opensearch/index/query/functionscore/FunctionScoreQueryBuilder.java @@ -32,6 +32,7 @@ package org.opensearch.index.query.functionscore; +import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.Query; import org.opensearch.common.Nullable; @@ -52,6 +53,7 @@ import org.opensearch.index.query.MatchAllQueryBuilder; import org.opensearch.index.query.MatchNoneQueryBuilder; import org.opensearch.index.query.QueryBuilder; +import org.opensearch.index.query.QueryBuilderVisitor; import org.opensearch.index.query.QueryRewriteContext; import org.opensearch.index.query.QueryShardContext; @@ -704,4 +706,12 @@ private static String parseFiltersAndFunctions( } return currentFieldName; } + + @Override + public void visit(QueryBuilderVisitor visitor) { + visitor.accept(this); + if (query != null) { + visitor.getChildVisitor(BooleanClause.Occur.MUST).accept(query); + } + } } diff --git a/server/src/test/java/org/opensearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java b/server/src/test/java/org/opensearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java index 4e64a1ec03688..78f4163d96d33 100644 --- a/server/src/test/java/org/opensearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java +++ b/server/src/test/java/org/opensearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java @@ -75,6 +75,7 @@ import org.hamcrest.Matcher; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -938,4 +939,14 @@ public void testMustRewrite() throws IOException { e = expectThrows(IllegalStateException.class, () -> functionQueryBuilder2.toQuery(context)); assertEquals("Rewrite first", e.getMessage()); } + + public void testVisit() { + TermQueryBuilder termQueryBuilder = new TermQueryBuilder("unmapped_field", "foo"); + FunctionScoreQueryBuilder builder = new FunctionScoreQueryBuilder(termQueryBuilder); + + List visitedQueries = new ArrayList<>(); + builder.visit(createTestVisitor(visitedQueries)); + + assertEquals(2, visitedQueries.size()); + } } From 2c3cef41a6465d81f7d4f8d695eb7177fd8826d6 Mon Sep 17 00:00:00 2001 From: jdnvn Date: Sun, 25 Aug 2024 15:44:50 -0400 Subject: [PATCH 2/3] update changelog Signed-off-by: jdnvn --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2aeb915ed6143..310177251833f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Bound the size of cache in deprecation logger ([16702](https://github.com/opensearch-project/OpenSearch/issues/16702)) - Ensure consistency of system flag on IndexMetadata after diff is applied ([#16644](https://github.com/opensearch-project/OpenSearch/pull/16644)) - Skip remote-repositories validations for node-joins when RepositoriesService is not in sync with cluster-state ([#16763](https://github.com/opensearch-project/OpenSearch/pull/16763)) +- Fix visit of inner query for FunctionScoreQueryBuilder ([#16776](https://github.com/opensearch-project/OpenSearch/pull/16776)) ### Security From 19c5597a623b0bcb587ea78a3dda222400f7c314 Mon Sep 17 00:00:00 2001 From: jdnvn Date: Sun, 1 Sep 2024 11:58:51 -0400 Subject: [PATCH 3/3] add assertion that inner query builder was visited MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Konrad Gołuchowski --- .../query/functionscore/FunctionScoreQueryBuilderTests.java | 1 + 1 file changed, 1 insertion(+) diff --git a/server/src/test/java/org/opensearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java b/server/src/test/java/org/opensearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java index 78f4163d96d33..8cf7941941bcb 100644 --- a/server/src/test/java/org/opensearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java +++ b/server/src/test/java/org/opensearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java @@ -948,5 +948,6 @@ public void testVisit() { builder.visit(createTestVisitor(visitedQueries)); assertEquals(2, visitedQueries.size()); + assertTrue(visitedQueries.contains(termQueryBuilder)); } }