Skip to content

Commit da5b205

Browse files
authored
Removing unused fetch sub phase processor initialization during fetch… (#12503)
* Removing unused fetch sub phase processor initialization during fetch phase execution Signed-off-by: Ankit Jain <akjain@amazon.com> * Updating CHANGELOG.md with the bug fix Signed-off-by: Ankit Jain <akjain@amazon.com> * Fixing smoke test failures Signed-off-by: Ankit Jain <akjain@amazon.com> * Addressing review comments Signed-off-by: Ankit Jain <akjain@amazon.com> * Addressing review comments around boolean check convention Signed-off-by: Ankit Jain <akjain@amazon.com> --------- Signed-off-by: Ankit Jain <akjain@amazon.com>
1 parent abe270f commit da5b205

File tree

8 files changed

+126
-2
lines changed

8 files changed

+126
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
152152
- Add a system property to configure YamlParser codepoint limits ([#12298](https://github.com/opensearch-project/OpenSearch/pull/12298))
153153
- Prevent read beyond slice boundary in ByteArrayIndexInput ([#10481](https://github.com/opensearch-project/OpenSearch/issues/10481))
154154
- Fix the "highlight.max_analyzer_offset" request parameter with "plain" highlighter ([#10919](https://github.com/opensearch-project/OpenSearch/pull/10919))
155+
- Prevent unnecessary fetch sub phase processor initialization during fetch phase execution ([#12503](https://github.com/opensearch-project/OpenSearch/pull/12503))
155156
- Warn about deprecated and ignored index.mapper.dynamic index setting ([#11193](https://github.com/opensearch-project/OpenSearch/pull/11193))
156157
- Fix `terms` query on `float` field when `doc_values` are turned off by reverting back to `FloatPoint` from `FloatField` ([#12499](https://github.com/opensearch-project/OpenSearch/pull/12499))
157158
- Fix get task API does not refresh resource stats ([#11531](https://github.com/opensearch-project/OpenSearch/pull/11531))

server/src/main/java/org/opensearch/search/fetch/FetchContext.java

+8
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ public boolean includeNamedQueriesScore() {
192192
return searchContext.includeNamedQueriesScore();
193193
}
194194

195+
public boolean hasInnerHits() {
196+
return searchContext.hasInnerHits();
197+
}
198+
195199
/**
196200
* Configuration for returning inner hits
197201
*/
@@ -213,6 +217,10 @@ public FetchFieldsContext fetchFieldsContext() {
213217
return searchContext.fetchFieldsContext();
214218
}
215219

220+
public boolean hasScriptFields() {
221+
return searchContext.hasScriptFields();
222+
}
223+
216224
/**
217225
* Configuration for script fields
218226
*/

server/src/main/java/org/opensearch/search/fetch/subphase/InnerHitsContext.java

+5
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ public String getName() {
119119
return name;
120120
}
121121

122+
@Override
123+
public boolean hasInnerHits() {
124+
return childInnerHits != null;
125+
}
126+
122127
@Override
123128
public InnerHitsContext innerHits() {
124129
return childInnerHits;

server/src/main/java/org/opensearch/search/fetch/subphase/InnerHitsPhase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public InnerHitsPhase(FetchPhase fetchPhase) {
6464

6565
@Override
6666
public FetchSubPhaseProcessor getProcessor(FetchContext searchContext) {
67-
if (searchContext.innerHits() == null) {
67+
if (searchContext.hasInnerHits() == false) {
6868
return null;
6969
}
7070
Map<String, InnerHitsContext.InnerHitSubContext> innerHits = searchContext.innerHits().getInnerHits();

server/src/main/java/org/opensearch/search/fetch/subphase/ScriptFieldsPhase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public final class ScriptFieldsPhase implements FetchSubPhase {
5454

5555
@Override
5656
public FetchSubPhaseProcessor getProcessor(FetchContext context) {
57-
if (context.scriptFields() == null) {
57+
if (context.hasScriptFields() == false) {
5858
return null;
5959
}
6060
List<ScriptFieldsContext.ScriptField> scriptFields = context.scriptFields().fields();

server/src/main/java/org/opensearch/search/internal/SearchContext.java

+4
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ public final void close() {
188188

189189
public abstract void highlight(SearchHighlightContext highlight);
190190

191+
public boolean hasInnerHits() {
192+
return innerHitsContext != null;
193+
}
194+
191195
public InnerHitsContext innerHits() {
192196
if (innerHitsContext == null) {
193197
innerHitsContext = new InnerHitsContext();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.search.fetch.subphase;
10+
11+
import org.opensearch.index.query.QueryShardContext;
12+
import org.opensearch.search.fetch.FetchContext;
13+
import org.opensearch.search.internal.SearchContext;
14+
import org.opensearch.search.lookup.SearchLookup;
15+
import org.opensearch.test.OpenSearchTestCase;
16+
17+
import static org.mockito.Mockito.mock;
18+
import static org.mockito.Mockito.when;
19+
20+
public class InnerHitsPhaseTests extends OpenSearchTestCase {
21+
22+
/*
23+
Returns mock search context reused across test methods
24+
*/
25+
private SearchContext getMockSearchContext(final boolean hasInnerHits) {
26+
final QueryShardContext queryShardContext = mock(QueryShardContext.class);
27+
when(queryShardContext.newFetchLookup()).thenReturn(mock(SearchLookup.class));
28+
29+
final SearchContext searchContext = mock(SearchContext.class);
30+
when(searchContext.hasInnerHits()).thenReturn(hasInnerHits);
31+
when(searchContext.getQueryShardContext()).thenReturn(queryShardContext);
32+
33+
return searchContext;
34+
}
35+
36+
/*
37+
Validates that InnerHitsPhase processor is not initialized when no inner hits
38+
*/
39+
public void testInnerHitsNull() {
40+
assertNull(new InnerHitsPhase(null).getProcessor(new FetchContext(getMockSearchContext(false))));
41+
}
42+
43+
/*
44+
Validates that InnerHitsPhase processor is initialized when inner hits are present
45+
*/
46+
public void testInnerHitsNonNull() {
47+
final SearchContext searchContext = getMockSearchContext(true);
48+
when(searchContext.innerHits()).thenReturn(new InnerHitsContext());
49+
50+
assertNotNull(new InnerHitsPhase(null).getProcessor(new FetchContext(searchContext)));
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.search.fetch.subphase;
10+
11+
import org.opensearch.index.query.QueryShardContext;
12+
import org.opensearch.search.fetch.FetchContext;
13+
import org.opensearch.search.internal.SearchContext;
14+
import org.opensearch.search.lookup.SearchLookup;
15+
import org.opensearch.test.OpenSearchTestCase;
16+
17+
import static org.mockito.Mockito.mock;
18+
import static org.mockito.Mockito.when;
19+
20+
public class ScriptFieldsPhaseTests extends OpenSearchTestCase {
21+
22+
/*
23+
Returns mock search context reused across test methods
24+
*/
25+
private SearchContext getMockSearchContext(final boolean hasScriptFields) {
26+
final QueryShardContext queryShardContext = mock(QueryShardContext.class);
27+
when(queryShardContext.newFetchLookup()).thenReturn(mock(SearchLookup.class));
28+
29+
final SearchContext searchContext = mock(SearchContext.class);
30+
when(searchContext.hasScriptFields()).thenReturn(hasScriptFields);
31+
when(searchContext.getQueryShardContext()).thenReturn(queryShardContext);
32+
33+
return searchContext;
34+
}
35+
36+
/*
37+
Validates that ScriptFieldsPhase processor is not initialized when no script fields
38+
*/
39+
public void testScriptFieldsNull() {
40+
assertNull(new ScriptFieldsPhase().getProcessor(new FetchContext(getMockSearchContext(false))));
41+
}
42+
43+
/*
44+
Validates that ScriptFieldsPhase processor is initialized when script fields are present
45+
*/
46+
public void testScriptFieldsNonNull() {
47+
final SearchContext searchContext = getMockSearchContext(true);
48+
when(searchContext.scriptFields()).thenReturn(new ScriptFieldsContext());
49+
50+
assertNotNull(new ScriptFieldsPhase().getProcessor(new FetchContext(searchContext)));
51+
}
52+
53+
}

0 commit comments

Comments
 (0)