From 2f84aa4c3e30e60bbf460dea6300f156b64c54ba Mon Sep 17 00:00:00 2001 From: Jim Ferenczi Date: Thu, 10 Oct 2024 08:50:08 +0100 Subject: [PATCH] Fix standard retriever rewrite Closes #114466 --- muted-tests.yml | 3 --- .../retriever/StandardRetrieverBuilder.java | 20 +++++++++---------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/muted-tests.yml b/muted-tests.yml index 18e179c0c0636..b0661e50a7312 100644 --- a/muted-tests.yml +++ b/muted-tests.yml @@ -366,9 +366,6 @@ tests: - class: org.elasticsearch.xpack.inference.InferenceRestIT method: test {p0=inference/40_semantic_text_query/Query a field that uses the default ELSER 2 endpoint} issue: https://github.com/elastic/elasticsearch/issues/114376 -- class: org.elasticsearch.search.retriever.StandardRetrieverBuilderParsingTests - method: testRewrite - issue: https://github.com/elastic/elasticsearch/issues/114466 - class: org.elasticsearch.search.retriever.RankDocsRetrieverBuilderTests method: testRewrite issue: https://github.com/elastic/elasticsearch/issues/114467 diff --git a/server/src/main/java/org/elasticsearch/search/retriever/StandardRetrieverBuilder.java b/server/src/main/java/org/elasticsearch/search/retriever/StandardRetrieverBuilder.java index 22439e5c996ac..4e875a97fdfc4 100644 --- a/server/src/main/java/org/elasticsearch/search/retriever/StandardRetrieverBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/retriever/StandardRetrieverBuilder.java @@ -113,29 +113,29 @@ private StandardRetrieverBuilder(StandardRetrieverBuilder clone) { @Override public RetrieverBuilder rewrite(QueryRewriteContext ctx) throws IOException { boolean changed = false; - List> newSortBuilders = null; + List> rewrittenSortBuilders = null; if (sortBuilders != null) { - newSortBuilders = new ArrayList<>(sortBuilders.size()); + rewrittenSortBuilders = new ArrayList<>(sortBuilders.size()); for (var sort : sortBuilders) { var newSort = sort.rewrite(ctx); - newSortBuilders.add(newSort); - changed = newSort != sort; + rewrittenSortBuilders.add(newSort); + changed |= newSort != sort; } } var rewrittenFilters = rewritePreFilters(ctx); changed |= rewrittenFilters != preFilterQueryBuilders; - QueryBuilder queryBuilderRewrite = null; + QueryBuilder rewrittenQuery = null; if (queryBuilder != null) { - queryBuilderRewrite = queryBuilder.rewrite(ctx); - changed |= queryBuilderRewrite != queryBuilder; + rewrittenQuery = queryBuilder.rewrite(ctx); + changed |= rewrittenQuery != queryBuilder; } if (changed) { var rewritten = new StandardRetrieverBuilder(this); - rewritten.sortBuilders = newSortBuilders; - rewritten.preFilterQueryBuilders = preFilterQueryBuilders; - rewritten.queryBuilder = queryBuilderRewrite; + rewritten.sortBuilders = rewrittenSortBuilders; + rewritten.preFilterQueryBuilders = rewrittenFilters; + rewritten.queryBuilder = rewrittenQuery; return rewritten; } return this;