Skip to content

Commit

Permalink
Ensure interception happens only once
Browse files Browse the repository at this point in the history
  • Loading branch information
kderusso committed Dec 6, 2024
1 parent 54989ab commit b3b2507
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException {
return queryBuilder.doToQuery(context);
}

@Override
protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
// Ensure that we only perform interception once in a query's rewrite phase
QueryRewriteContext interceptedContext = queryRewriteContext.getInterceptedQueryRewriteContext();
var rewritten = queryBuilder.rewrite(interceptedContext);
if (rewritten == queryBuilder) {
return this;
}
return rewritten;
}

@Override
protected boolean doEquals(T other) {
// Handle the edge case where we need to unwrap the incoming query builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,32 @@ public QueryRewriteContext(
);
}

public QueryRewriteContext getInterceptedQueryRewriteContext() {

if (queryRewriteInterceptor == null) {
return this;
}

return new QueryRewriteContext(
parserConfiguration,
client,
nowInMillis,
mapperService,
mappingLookup,
runtimeMappings,
indexSettings,
fullyQualifiedIndex,
indexNameMatcher,
writeableRegistry,
valuesSourceRegistry,
allowExpensiveQueries,
scriptService,
resolvedIndices,
pit,
null
);
}

/**
* The registry used to build new {@link XContentParser}s. Contains registered named parsers needed to parse the query.
*
Expand Down Expand Up @@ -438,4 +464,5 @@ public String getTierPreference() {
public QueryRewriteInterceptor getQueryRewriteInterceptor() {
return queryRewriteInterceptor;
}

}

0 comments on commit b3b2507

Please sign in to comment.