Skip to content

Commit 19d012e

Browse files
Add a flag in QueryShardContext to differentiate between a normal query and an inner hit query (#16600)
Signed-off-by: Heemin Kim <[email protected]> (cherry picked from commit c9edb48) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 3ca5d0a commit 19d012e

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1010
- Switch from `buildSrc/version.properties` to Gradle version catalog (`gradle/libs.versions.toml`) to enable dependabot to perform automated upgrades on common libs ([#16284](https://github.com/opensearch-project/OpenSearch/pull/16284))
1111
- Add dynamic setting allowing size > 0 requests to be cached in the request cache ([#16483](https://github.com/opensearch-project/OpenSearch/pull/16483))
1212
- Make IndexStoreListener a pluggable interface ([#16583](https://github.com/opensearch-project/OpenSearch/pull/16583))
13+
- Add a flag in QueryShardContext to differentiate inner hit query ([#16600](https://github.com/opensearch-project/OpenSearch/pull/16600))
1314

1415
### Dependencies
1516
- Bump `com.google.apis:google-api-services-compute` from v1-rev20240407-2.0.0 to v1-rev20241021-2.0.0 ([#16502](https://github.com/opensearch-project/OpenSearch/pull/16502), [#16548](https://github.com/opensearch-project/OpenSearch/pull/16548))

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

+2
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ protected void doBuild(SearchContext parentSearchContext, InnerHitsContext inner
413413
try {
414414
queryShardContext.setParentFilter(parentFilter);
415415
queryShardContext.nestedScope().nextLevel(nestedObjectMapper);
416+
queryShardContext.setInnerHitQuery(true);
416417
try {
417418
NestedInnerHitSubContext nestedInnerHits = new NestedInnerHitSubContext(
418419
name,
@@ -427,6 +428,7 @@ protected void doBuild(SearchContext parentSearchContext, InnerHitsContext inner
427428
}
428429
} finally {
429430
queryShardContext.setParentFilter(previousParentFilter);
431+
queryShardContext.setInnerHitQuery(false);
430432
}
431433
}
432434
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public class QueryShardContext extends QueryRewriteContext {
127127
private BitSetProducer parentFilter;
128128
private DerivedFieldResolver derivedFieldResolver;
129129
private boolean keywordIndexOrDocValuesEnabled;
130+
private boolean isInnerHitQuery;
130131

131132
public QueryShardContext(
132133
int shardId,
@@ -738,4 +739,12 @@ public BitSetProducer getParentFilter() {
738739
public void setParentFilter(BitSetProducer parentFilter) {
739740
this.parentFilter = parentFilter;
740741
}
742+
743+
public boolean isInnerHitQuery() {
744+
return isInnerHitQuery;
745+
}
746+
747+
public void setInnerHitQuery(boolean isInnerHitQuery) {
748+
this.isInnerHitQuery = isInnerHitQuery;
749+
}
741750
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ public void testParentFilterFromInlineLeafInnerHitsNestedQuery() throws Exceptio
335335
if (context.getParentFilter() == null) {
336336
throw new Exception("Expect parent filter to be non-null");
337337
}
338+
if (context.isInnerHitQuery() == false) {
339+
throw new Exception("Expect it to be inner hit query");
340+
}
338341
return invoke.callRealMethod();
339342
});
340343
NestedQueryBuilder query = new NestedQueryBuilder("nested1", innerQueryBuilder, ScoreMode.None);
@@ -345,6 +348,7 @@ public void testParentFilterFromInlineLeafInnerHitsNestedQuery() throws Exceptio
345348
assertThat(innerHitBuilders.size(), Matchers.equalTo(1));
346349
assertTrue(innerHitBuilders.containsKey(leafInnerHits.getName()));
347350
assertNull(queryShardContext.getParentFilter());
351+
assertFalse(queryShardContext.isInnerHitQuery());
348352
innerHitBuilders.get(leafInnerHits.getName()).build(searchContext, innerHitsContext);
349353
assertNull(queryShardContext.getParentFilter());
350354
verify(innerQueryBuilder).toQuery(queryShardContext);

0 commit comments

Comments
 (0)