Skip to content

Commit

Permalink
add visitor logic to FunctionScoreQueryBuilder
Browse files Browse the repository at this point in the history
Signed-off-by: jdnvn <[email protected]>
  • Loading branch information
jdnvn authored and Konrad Gołuchowski committed Dec 4, 2024
1 parent b1bf72f commit 10034b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<QueryBuilder> visitedQueries = new ArrayList<>();
builder.visit(createTestVisitor(visitedQueries));

assertEquals(2, visitedQueries.size());
}
}

0 comments on commit 10034b5

Please sign in to comment.