Skip to content

Commit

Permalink
Pass parent filter to inner query in nested query
Browse files Browse the repository at this point in the history
Pass parent filter to inner query so that inner query can utilize the information

Signed-off-by: Heemin Kim <heemin@amazon.com>
  • Loading branch information
heemin32 committed Sep 26, 2023
1 parent 8807d7a commit 99fccbf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Force merge with `only_expunge_deletes` honors max segment size ([#10036](https://github.com/opensearch-project/OpenSearch/pull/10036))
- Add instrumentation in transport service. ([#10042](https://github.com/opensearch-project/OpenSearch/pull/10042))
- [Tracing Framework] Add support for SpanKind. ([#10122](https://github.com/opensearch-project/OpenSearch/pull/10122))
- Pass parent filter to inner query in nested query ([#10246](https://github.com/opensearch-project/OpenSearch/pull/10246))

### Deprecated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,13 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
parentFilter = context.bitsetFilter(objectMapper.nestedTypeFilter());
}

BitSetProducer previousParentFilter = context.getParentFilter();
try {
context.setParentFilter(parentFilter);
context.nestedScope().nextLevel(nestedObjectMapper);
innerQuery = this.query.toQuery(context);
} finally {
context.setParentFilter(previousParentFilter);
context.nestedScope().previousLevel();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public class QueryShardContext extends QueryRewriteContext {
private boolean mapUnmappedFieldAsString;
private NestedScope nestedScope;
private final ValuesSourceRegistry valuesSourceRegistry;
private BitSetProducer parentFilter;

public QueryShardContext(
int shardId,
Expand Down Expand Up @@ -622,4 +623,12 @@ public BitsetFilterCache getBitsetFilterCache() {
public AggregationUsageService getUsageService() {
return valuesSourceRegistry.getUsageService();
}

public BitSetProducer getParentFilter() {
return parentFilter;
}

public void setParentFilter(BitSetProducer parentFilter) {
this.parentFilter = parentFilter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
import java.util.HashMap;
import java.util.Map;

import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.opensearch.index.IndexSettingsTests.newIndexMeta;
import static org.opensearch.index.query.InnerHitBuilderTests.randomNestedInnerHits;
import static org.hamcrest.CoreMatchers.containsString;
Expand Down Expand Up @@ -411,4 +413,22 @@ public void testDisallowExpensiveQueries() {
OpenSearchException e = expectThrows(OpenSearchException.class, () -> queryBuilder.toQuery(queryShardContext));
assertEquals("[joining] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", e.getMessage());
}

public void testSetParentFilterInContext() throws Exception {
QueryShardContext queryShardContext = createShardContext();
QueryBuilder innerQueryBuilder = spy(new MatchAllQueryBuilderTests().createTestQueryBuilder());
when(innerQueryBuilder.toQuery(queryShardContext)).thenAnswer(invoke -> {
QueryShardContext context = invoke.getArgument(0);
if (context.getParentFilter() == null) {
throw new Exception("Expect parent filter to be non-null");
}
return invoke.callRealMethod();
});
NestedQueryBuilder nqb = new NestedQueryBuilder("nested1", innerQueryBuilder, RandomPicks.randomFrom(random(), ScoreMode.values()));

assertNull(queryShardContext.getParentFilter());
nqb.rewrite(queryShardContext).toQuery(queryShardContext);
assertNull(queryShardContext.getParentFilter());
verify(innerQueryBuilder).toQuery(queryShardContext);
}
}

0 comments on commit 99fccbf

Please sign in to comment.