Skip to content

Commit

Permalink
Removing unused fetch sub phase processor initialization during fetch…
Browse files Browse the repository at this point in the history
… phase execution

Signed-off-by: Ankit Jain <[email protected]>
  • Loading branch information
jainankitk committed Mar 1, 2024
1 parent 3125b94 commit a742967
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ public boolean includeNamedQueriesScore() {
return searchContext.includeNamedQueriesScore();
}

public boolean hasInnerHits() {
return searchContext.hasInnerHits();
}

/**
* Configuration for returning inner hits
*/
Expand All @@ -213,6 +217,10 @@ public FetchFieldsContext fetchFieldsContext() {
return searchContext.fetchFieldsContext();
}

public boolean hasScriptFields() {
return searchContext.hasScriptFields();
}

/**
* Configuration for script fields
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public InnerHitsPhase(FetchPhase fetchPhase) {

@Override
public FetchSubPhaseProcessor getProcessor(FetchContext searchContext) {
if (searchContext.innerHits() == null) {
if (!searchContext.hasInnerHits()) {
return null;
}
Map<String, InnerHitsContext.InnerHitSubContext> innerHits = searchContext.innerHits().getInnerHits();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public final class ScriptFieldsPhase implements FetchSubPhase {

@Override
public FetchSubPhaseProcessor getProcessor(FetchContext context) {
if (context.scriptFields() == null) {
if (!context.hasScriptFields()) {
return null;
}
List<ScriptFieldsContext.ScriptField> scriptFields = context.scriptFields().fields();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ public final void close() {

public abstract void highlight(SearchHighlightContext highlight);

public boolean hasInnerHits() {
return innerHitsContext != null;
}

public InnerHitsContext innerHits() {
if (innerHitsContext == null) {
innerHitsContext = new InnerHitsContext();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.search.fetch.subphase;

import org.opensearch.index.query.QueryShardContext;
import org.opensearch.search.fetch.FetchContext;
import org.opensearch.search.internal.SearchContext;
import org.opensearch.search.lookup.SearchLookup;
import org.opensearch.test.OpenSearchTestCase;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class InnerHitsPhaseTests extends OpenSearchTestCase {

/*
Returns mock search context reused across test methods
*/
private SearchContext getMockSearchContext(final boolean hasInnerHits) {
final QueryShardContext queryShardContext = mock(QueryShardContext.class);
when(queryShardContext.newFetchLookup()).thenReturn(mock(SearchLookup.class));

final SearchContext searchContext = mock(SearchContext.class);
when(searchContext.hasInnerHits()).thenReturn(hasInnerHits);
when(searchContext.getQueryShardContext()).thenReturn(queryShardContext);

return searchContext;
}

/*
Validates that InnerHitsPhase processor is not initialized when no inner hits
*/
public void testInnerHitsNull() {
assertNull(new InnerHitsPhase(null).getProcessor(new FetchContext(getMockSearchContext(false))));
}

/*
Validates that InnerHitsPhase processor is initialized when inner hits are present
*/
public void testInnerHitsNonNull() {
final SearchContext searchContext = getMockSearchContext(true);
when(searchContext.innerHits()).thenReturn(new InnerHitsContext());

assertNotNull(new InnerHitsPhase(null).getProcessor(new FetchContext(searchContext)));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.search.fetch.subphase;

import org.opensearch.index.query.QueryShardContext;
import org.opensearch.search.fetch.FetchContext;
import org.opensearch.search.internal.SearchContext;
import org.opensearch.search.lookup.SearchLookup;
import org.opensearch.test.OpenSearchTestCase;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ScriptFieldsPhaseTests extends OpenSearchTestCase {

/*
Returns mock search context reused across test methods
*/
private SearchContext getMockSearchContext(final boolean hasScriptFields) {
final QueryShardContext queryShardContext = mock(QueryShardContext.class);
when(queryShardContext.newFetchLookup()).thenReturn(mock(SearchLookup.class));

final SearchContext searchContext = mock(SearchContext.class);
when(searchContext.hasScriptFields()).thenReturn(hasScriptFields);
when(searchContext.getQueryShardContext()).thenReturn(queryShardContext);

return searchContext;
}

/*
Validates that ScriptFieldsPhase processor is not initialized when no script fields
*/
public void testScriptFieldsNull() {
assertNull(new ScriptFieldsPhase().getProcessor(new FetchContext(getMockSearchContext(false))));
}

/*
Validates that ScriptFieldsPhase processor is initialized when script fields are present
*/
public void testScriptFieldsNonNull() {
final SearchContext searchContext = getMockSearchContext(true);
when(searchContext.scriptFields()).thenReturn(new ScriptFieldsContext());

assertNotNull(new ScriptFieldsPhase().getProcessor(new FetchContext(searchContext)));
}

}

0 comments on commit a742967

Please sign in to comment.