Skip to content

Commit c6f1235

Browse files
committed
Pass parent filter to inner hit query (#13903)
Signed-off-by: Heemin Kim <[email protected]> (cherry picked from commit 6c9603a)
1 parent c0581a0 commit c6f1235

File tree

3 files changed

+65
-31
lines changed

3 files changed

+65
-31
lines changed

CHANGELOG.md

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -121,27 +121,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
121121
- Remove deprecated classes for Rounding ([#10956](https://github.com/opensearch-project/OpenSearch/issues/10956))
122122

123123
### Fixed
124-
- Fix failure in dissect ingest processor parsing empty brackets ([#9225](https://github.com/opensearch-project/OpenSearch/pull/9255))
125-
- Fix class_cast_exception when passing int to _version and other metadata fields in ingest simulate API ([#10101](https://github.com/opensearch-project/OpenSearch/pull/10101))
126-
- Fix passing wrong parameter when calling newConfigurationException() in DotExpanderProcessor ([#10737](https://github.com/opensearch-project/OpenSearch/pull/10737))
127-
- Delegating CachingWeightWrapper#count to internal weight object ([#10543](https://github.com/opensearch-project/OpenSearch/pull/10543))
128-
- Fix per request latency last phase not tracked ([#10934](https://github.com/opensearch-project/OpenSearch/pull/10934))
129-
- Fix SuggestSearch.testSkipDuplicates by forcing refresh when indexing its test documents ([#11068](https://github.com/opensearch-project/OpenSearch/pull/11068))
130-
- [BUG] Fix the thread context that is not properly cleared and messes up the traces ([#10873](https://github.com/opensearch-project/OpenSearch/pull/10873))
131-
- Handle canMatchSearchAfter for frozen context scenario ([#11249](https://github.com/opensearch-project/OpenSearch/pull/11249))
132-
- Fix the issue with DefaultSpanScope restoring wrong span in the TracerContextStorage upon detach ([#11316](https://github.com/opensearch-project/OpenSearch/issues/11316))
133-
- Remove shadowJar from `lang-painless` module publication ([#11369](https://github.com/opensearch-project/OpenSearch/issues/11369))
134-
- Fix remote shards balancer and remove unused variables ([#11167](https://github.com/opensearch-project/OpenSearch/pull/11167))
135-
- Fix parsing of flat object fields with dots in keys ([#11425](https://github.com/opensearch-project/OpenSearch/pull/11425))
136-
- Fix bug where replication lag grows post primary relocation ([#11238](https://github.com/opensearch-project/OpenSearch/pull/11238))
137-
- Fix noop_update_total metric in indexing stats cannot be updated by bulk API ([#11485](https://github.com/opensearch-project/OpenSearch/pull/11485),[#11917](https://github.com/opensearch-project/OpenSearch/pull/11917))
138-
- Fix for stuck update action in a bulk with `retry_on_conflict` property ([#11152](https://github.com/opensearch-project/OpenSearch/issues/11152))
139-
- Fix template setting override for replication type ([#11417](https://github.com/opensearch-project/OpenSearch/pull/11417))
140-
- Fix Automatic addition of protocol broken in #11512 ([#11609](https://github.com/opensearch-project/OpenSearch/pull/11609))
141-
- Fix issue when calling Delete PIT endpoint and no PITs exist ([#11711](https://github.com/opensearch-project/OpenSearch/pull/11711))
142-
- Fix tracing context propagation for local transport instrumentation ([#11490](https://github.com/opensearch-project/OpenSearch/pull/11490))
143-
- Fix parsing of single line comments in `lang-painless` ([#11815](https://github.com/opensearch-project/OpenSearch/issues/11815))
144-
- Fix typo in API annotation check message ([11836](https://github.com/opensearch-project/OpenSearch/pull/11836))
124+
- Fix get field mapping API returns 404 error in mixed cluster with multiple versions ([#13624](https://github.com/opensearch-project/OpenSearch/pull/13624))
125+
- Pass parent filter to inner hit query ([#13903](https://github.com/opensearch-project/OpenSearch/pull/13903))
145126

146127
### Security
147128

server/src/main/java/org/opensearch/index/query/NestedQueryBuilder.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -391,16 +391,32 @@ protected void doBuild(SearchContext parentSearchContext, InnerHitsContext inner
391391
}
392392
}
393393
String name = innerHitBuilder.getName() != null ? innerHitBuilder.getName() : nestedObjectMapper.fullPath();
394-
ObjectMapper parentObjectMapper = queryShardContext.nestedScope().nextLevel(nestedObjectMapper);
395-
NestedInnerHitSubContext nestedInnerHits = new NestedInnerHitSubContext(
396-
name,
397-
parentSearchContext,
398-
parentObjectMapper,
399-
nestedObjectMapper
400-
);
401-
setupInnerHitsContext(queryShardContext, nestedInnerHits);
402-
queryShardContext.nestedScope().previousLevel();
403-
innerHitsContext.addInnerHitDefinition(nestedInnerHits);
394+
ObjectMapper parentObjectMapper = queryShardContext.nestedScope().getObjectMapper();
395+
BitSetProducer parentFilter;
396+
if (parentObjectMapper == null) {
397+
parentFilter = queryShardContext.bitsetFilter(Queries.newNonNestedFilter());
398+
} else {
399+
parentFilter = queryShardContext.bitsetFilter(parentObjectMapper.nestedTypeFilter());
400+
}
401+
BitSetProducer previousParentFilter = queryShardContext.getParentFilter();
402+
try {
403+
queryShardContext.setParentFilter(parentFilter);
404+
queryShardContext.nestedScope().nextLevel(nestedObjectMapper);
405+
try {
406+
NestedInnerHitSubContext nestedInnerHits = new NestedInnerHitSubContext(
407+
name,
408+
parentSearchContext,
409+
parentObjectMapper,
410+
nestedObjectMapper
411+
);
412+
setupInnerHitsContext(queryShardContext, nestedInnerHits);
413+
innerHitsContext.addInnerHitDefinition(nestedInnerHits);
414+
} finally {
415+
queryShardContext.nestedScope().previousLevel();
416+
}
417+
} finally {
418+
queryShardContext.setParentFilter(previousParentFilter);
419+
}
404420
}
405421
}
406422

server/src/test/java/org/opensearch/index/query/NestedQueryBuilderTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,43 @@ public void testInlineLeafInnerHitsNestedQuery() throws Exception {
302302
assertThat(innerHitBuilders.get(leafInnerHits.getName()), Matchers.notNullValue());
303303
}
304304

305+
public void testParentFilterFromInlineLeafInnerHitsNestedQuery() throws Exception {
306+
QueryShardContext queryShardContext = createShardContext();
307+
SearchContext searchContext = mock(SearchContext.class);
308+
when(searchContext.getQueryShardContext()).thenReturn(queryShardContext);
309+
310+
MapperService mapperService = mock(MapperService.class);
311+
IndexSettings settings = new IndexSettings(newIndexMeta("index", Settings.EMPTY), Settings.EMPTY);
312+
when(mapperService.getIndexSettings()).thenReturn(settings);
313+
when(searchContext.mapperService()).thenReturn(mapperService);
314+
315+
InnerHitBuilder leafInnerHits = randomNestedInnerHits();
316+
// Set null for values not related with this test case
317+
leafInnerHits.setScriptFields(null);
318+
leafInnerHits.setHighlightBuilder(null);
319+
leafInnerHits.setSorts(null);
320+
321+
QueryBuilder innerQueryBuilder = spy(new MatchAllQueryBuilder());
322+
when(innerQueryBuilder.toQuery(queryShardContext)).thenAnswer(invoke -> {
323+
QueryShardContext context = invoke.getArgument(0);
324+
if (context.getParentFilter() == null) {
325+
throw new Exception("Expect parent filter to be non-null");
326+
}
327+
return invoke.callRealMethod();
328+
});
329+
NestedQueryBuilder query = new NestedQueryBuilder("nested1", innerQueryBuilder, ScoreMode.None);
330+
query.innerHit(leafInnerHits);
331+
final Map<String, InnerHitContextBuilder> innerHitBuilders = new HashMap<>();
332+
final InnerHitsContext innerHitsContext = new InnerHitsContext();
333+
query.extractInnerHitBuilders(innerHitBuilders);
334+
assertThat(innerHitBuilders.size(), Matchers.equalTo(1));
335+
assertTrue(innerHitBuilders.containsKey(leafInnerHits.getName()));
336+
assertNull(queryShardContext.getParentFilter());
337+
innerHitBuilders.get(leafInnerHits.getName()).build(searchContext, innerHitsContext);
338+
assertNull(queryShardContext.getParentFilter());
339+
verify(innerQueryBuilder).toQuery(queryShardContext);
340+
}
341+
305342
public void testInlineLeafInnerHitsNestedQueryViaBoolQuery() {
306343
InnerHitBuilder leafInnerHits = randomNestedInnerHits();
307344
NestedQueryBuilder nestedQueryBuilder = new NestedQueryBuilder("path", new MatchAllQueryBuilder(), ScoreMode.None).innerHit(

0 commit comments

Comments
 (0)