Move SearchLookup into FetchContext (#62549)

FetchSubPhase#getProcessor currently takes a SearchLookup parameter. This
however is only needed by a couple of subphases, and will almost certainly change in
future as we want to simplify how fetch phases retrieve values for individual hits.

To future-proof against further signature changes, this commit moves the SearchLookup
reference into FetchContext instead.
This commit is contained in:
Alan Woodward 2020-09-17 17:31:39 +01:00 committed by Alan Woodward
parent e3e3aef3d8
commit 5421a743a7
21 changed files with 42 additions and 45 deletions

View File

@ -34,7 +34,6 @@ import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightPhase;
import org.elasticsearch.search.fetch.subphase.highlight.Highlighter;
import org.elasticsearch.search.fetch.subphase.highlight.SearchHighlightContext;
import org.elasticsearch.search.lookup.SearchLookup;
import org.elasticsearch.search.lookup.SourceLookup;
import java.io.IOException;
@ -56,7 +55,7 @@ final class PercolatorHighlightSubFetchPhase implements FetchSubPhase {
}
@Override
public FetchSubPhaseProcessor getProcessor(FetchContext fetchContext, SearchLookup lookup) {
public FetchSubPhaseProcessor getProcessor(FetchContext fetchContext) {
if (fetchContext.highlight() == null) {
return null;
}

View File

@ -37,7 +37,6 @@ import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.search.fetch.FetchContext;
import org.elasticsearch.search.fetch.FetchSubPhase;
import org.elasticsearch.search.fetch.FetchSubPhaseProcessor;
import org.elasticsearch.search.lookup.SearchLookup;
import java.io.IOException;
import java.util.ArrayList;
@ -58,7 +57,7 @@ final class PercolatorMatchedSlotSubFetchPhase implements FetchSubPhase {
static final String FIELD_NAME_PREFIX = "_percolator_document_slot";
@Override
public FetchSubPhaseProcessor getProcessor(FetchContext fetchContext, SearchLookup lookup) throws IOException {
public FetchSubPhaseProcessor getProcessor(FetchContext fetchContext) throws IOException {
List<PercolateContext> percolateContexts = new ArrayList<>();
List<PercolateQuery> percolateQueries = locatePercolatorQuery(fetchContext.query());

View File

@ -52,9 +52,9 @@ public class PercolatorHighlightSubFetchPhaseTests extends ESTestCase {
Mockito.when(fetchContext.highlight()).thenReturn(new SearchHighlightContext(Collections.emptyList()));
Mockito.when(fetchContext.query()).thenReturn(new MatchAllDocsQuery());
assertNull(subFetchPhase.getProcessor(fetchContext, null));
assertNull(subFetchPhase.getProcessor(fetchContext));
Mockito.when(fetchContext.query()).thenReturn(percolateQuery);
assertNotNull(subFetchPhase.getProcessor(fetchContext, null));
assertNotNull(subFetchPhase.getProcessor(fetchContext));
}
public void testLocatePercolatorQuery() {

View File

@ -76,7 +76,7 @@ public class PercolatorMatchedSlotSubFetchPhaseTests extends ESTestCase {
FetchContext sc = mock(FetchContext.class);
when(sc.query()).thenReturn(percolateQuery);
FetchSubPhaseProcessor processor = phase.getProcessor(sc, null);
FetchSubPhaseProcessor processor = phase.getProcessor(sc);
assertNotNull(processor);
processor.process(hit);
@ -97,7 +97,7 @@ public class PercolatorMatchedSlotSubFetchPhaseTests extends ESTestCase {
FetchContext sc = mock(FetchContext.class);
when(sc.query()).thenReturn(percolateQuery);
FetchSubPhaseProcessor processor = phase.getProcessor(sc, null);
FetchSubPhaseProcessor processor = phase.getProcessor(sc);
assertNotNull(processor);
processor.process(hit);
@ -117,7 +117,7 @@ public class PercolatorMatchedSlotSubFetchPhaseTests extends ESTestCase {
FetchContext sc = mock(FetchContext.class);
when(sc.query()).thenReturn(percolateQuery);
FetchSubPhaseProcessor processor = phase.getProcessor(sc, null);
FetchSubPhaseProcessor processor = phase.getProcessor(sc);
assertNotNull(processor);
processor.process(hit);

View File

@ -35,7 +35,6 @@ import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.SearchPlugin;
import org.elasticsearch.search.SearchExtBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.lookup.SearchLookup;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
import org.elasticsearch.test.ESIntegTestCase.Scope;
@ -117,7 +116,7 @@ public class FetchSubPhasePluginIT extends ESIntegTestCase {
private static final String NAME = "term_vectors_fetch";
@Override
public FetchSubPhaseProcessor getProcessor(FetchContext searchContext, SearchLookup lookup) {
public FetchSubPhaseProcessor getProcessor(FetchContext searchContext) {
return new FetchSubPhaseProcessor() {
@Override
public void setNextReader(LeafReaderContext readerContext) {

View File

@ -33,6 +33,7 @@ import org.elasticsearch.search.fetch.subphase.ScriptFieldsContext;
import org.elasticsearch.search.fetch.subphase.highlight.SearchHighlightContext;
import org.elasticsearch.search.internal.ContextIndexSearcher;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.search.lookup.SearchLookup;
import org.elasticsearch.search.rescore.RescoreContext;
import java.util.Collections;
@ -44,12 +45,14 @@ import java.util.List;
public class FetchContext {
private final SearchContext searchContext;
private final SearchLookup searchLookup;
/**
* Create a FetchContext based on a SearchContext
*/
public FetchContext(SearchContext searchContext) {
this.searchContext = searchContext;
this.searchLookup = searchContext.getQueryShardContext().newFetchLookup();
}
/**
@ -80,6 +83,13 @@ public class FetchContext {
return mapperService().getIndexSettings();
}
/**
* The {@code SearchLookup} for the this context
*/
public SearchLookup searchLookup() {
return searchLookup;
}
/**
* The original query
*/

View File

@ -114,8 +114,7 @@ public class FetchPhase {
SearchHit[] hits = new SearchHit[context.docIdsToLoadSize()];
Map<String, Object> sharedCache = new HashMap<>();
SearchLookup lookup = context.getQueryShardContext().newFetchLookup();
List<FetchSubPhaseProcessor> processors = getProcessors(context.shardTarget(), lookup, fetchContext);
List<FetchSubPhaseProcessor> processors = getProcessors(context.shardTarget(), fetchContext);
int currentReaderIndex = -1;
LeafReaderContext currentReaderContext = null;
@ -136,7 +135,7 @@ public class FetchPhase {
assert currentReaderContext != null;
HitContext hit = prepareHitContext(
context,
lookup,
fetchContext.searchLookup(),
fieldsVisitor,
docId,
storedToRequestedFields,
@ -160,11 +159,11 @@ public class FetchPhase {
}
List<FetchSubPhaseProcessor> getProcessors(SearchShardTarget target, SearchLookup lookup, FetchContext context) {
List<FetchSubPhaseProcessor> getProcessors(SearchShardTarget target, FetchContext context) {
try {
List<FetchSubPhaseProcessor> processors = new ArrayList<>();
for (FetchSubPhase fsp : fetchSubPhases) {
FetchSubPhaseProcessor processor = fsp.getProcessor(context, lookup);
FetchSubPhaseProcessor processor = fsp.getProcessor(context);
if (processor != null) {
processors.add(processor);
}

View File

@ -23,7 +23,6 @@ import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.ReaderUtil;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.lookup.SearchLookup;
import org.elasticsearch.search.lookup.SourceLookup;
import java.io.IOException;
@ -102,5 +101,5 @@ public interface FetchSubPhase {
* If nothing should be executed for the provided {@code FetchContext}, then the
* implementation should return {@code null}
*/
FetchSubPhaseProcessor getProcessor(FetchContext fetchContext, SearchLookup lookup) throws IOException;
FetchSubPhaseProcessor getProcessor(FetchContext fetchContext) throws IOException;
}

View File

@ -23,7 +23,6 @@ import org.apache.lucene.search.Explanation;
import org.elasticsearch.search.fetch.FetchContext;
import org.elasticsearch.search.fetch.FetchSubPhase;
import org.elasticsearch.search.fetch.FetchSubPhaseProcessor;
import org.elasticsearch.search.lookup.SearchLookup;
import org.elasticsearch.search.rescore.RescoreContext;
import java.io.IOException;
@ -34,7 +33,7 @@ import java.io.IOException;
public final class ExplainPhase implements FetchSubPhase {
@Override
public FetchSubPhaseProcessor getProcessor(FetchContext context, SearchLookup lookup) {
public FetchSubPhaseProcessor getProcessor(FetchContext context) {
if (context.explain() == false) {
return null;
}

View File

@ -23,11 +23,10 @@ import org.elasticsearch.common.document.DocumentField;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.index.mapper.DocValueFetcher;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.ValueFetcher;
import org.elasticsearch.search.fetch.FetchContext;
import org.elasticsearch.search.fetch.FetchSubPhase;
import org.elasticsearch.search.fetch.FetchSubPhaseProcessor;
import org.elasticsearch.index.mapper.ValueFetcher;
import org.elasticsearch.search.lookup.SearchLookup;
import java.io.IOException;
import java.util.ArrayList;
@ -44,7 +43,7 @@ public final class FetchDocValuesPhase implements FetchSubPhase {
private static final DeprecationLogger DEPRECATION_LOGGER = DeprecationLogger.getLogger(FetchDocValuesPhase.class);
@Override
public FetchSubPhaseProcessor getProcessor(FetchContext context, SearchLookup lookup) {
public FetchSubPhaseProcessor getProcessor(FetchContext context) {
FetchDocValuesContext dvContext = context.docValuesContext();
if (dvContext == null) {
return null;
@ -70,7 +69,10 @@ public final class FetchDocValuesPhase implements FetchSubPhase {
continue;
}
String format = USE_DEFAULT_FORMAT.equals(fieldAndFormat.format) ? null : fieldAndFormat.format;
ValueFetcher fetcher = new DocValueFetcher(ft.docValueFormat(format, null), lookup.doc().getForField(ft));
ValueFetcher fetcher = new DocValueFetcher(
ft.docValueFormat(format, null),
context.searchLookup().doc().getForField(ft)
);
fields.add(new DocValueField(fieldAndFormat.field, fetcher));
}

View File

@ -26,7 +26,6 @@ import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.FetchContext;
import org.elasticsearch.search.fetch.FetchSubPhase;
import org.elasticsearch.search.fetch.FetchSubPhaseProcessor;
import org.elasticsearch.search.lookup.SearchLookup;
import org.elasticsearch.search.lookup.SourceLookup;
import java.io.IOException;
@ -41,7 +40,7 @@ import java.util.Set;
public final class FetchFieldsPhase implements FetchSubPhase {
@Override
public FetchSubPhaseProcessor getProcessor(FetchContext fetchContext, SearchLookup lookup) {
public FetchSubPhaseProcessor getProcessor(FetchContext fetchContext) {
FetchFieldsContext fetchFieldsContext = fetchContext.fetchFieldsContext();
if (fetchFieldsContext == null) {
return null;
@ -49,7 +48,7 @@ public final class FetchFieldsPhase implements FetchSubPhase {
FieldValueRetriever retriever = fetchFieldsContext.fieldValueRetriever(
fetchContext.getIndexName(),
fetchContext.mapperService(),
lookup
fetchContext.searchLookup()
);
return new FetchSubPhaseProcessor() {
@Override

View File

@ -28,14 +28,13 @@ import org.apache.lucene.search.Weight;
import org.elasticsearch.search.fetch.FetchContext;
import org.elasticsearch.search.fetch.FetchSubPhase;
import org.elasticsearch.search.fetch.FetchSubPhaseProcessor;
import org.elasticsearch.search.lookup.SearchLookup;
import java.io.IOException;
public class FetchScorePhase implements FetchSubPhase {
@Override
public FetchSubPhaseProcessor getProcessor(FetchContext context, SearchLookup lookup) throws IOException {
public FetchSubPhaseProcessor getProcessor(FetchContext context) throws IOException {
if (context.fetchScores() == false) {
return null;
}

View File

@ -28,7 +28,6 @@ import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.FetchContext;
import org.elasticsearch.search.fetch.FetchSubPhase;
import org.elasticsearch.search.fetch.FetchSubPhaseProcessor;
import org.elasticsearch.search.lookup.SearchLookup;
import org.elasticsearch.search.lookup.SourceLookup;
import java.io.IOException;
@ -37,7 +36,7 @@ import java.util.Map;
public final class FetchSourcePhase implements FetchSubPhase {
@Override
public FetchSubPhaseProcessor getProcessor(FetchContext fetchContext, SearchLookup lookup) {
public FetchSubPhaseProcessor getProcessor(FetchContext fetchContext) {
FetchSourceContext fetchSourceContext = fetchContext.fetchSourceContext();
if (fetchSourceContext == null || fetchSourceContext.fetchSource() == false) {
return null;

View File

@ -25,14 +25,13 @@ import org.elasticsearch.index.mapper.VersionFieldMapper;
import org.elasticsearch.search.fetch.FetchContext;
import org.elasticsearch.search.fetch.FetchSubPhase;
import org.elasticsearch.search.fetch.FetchSubPhaseProcessor;
import org.elasticsearch.search.lookup.SearchLookup;
import java.io.IOException;
public final class FetchVersionPhase implements FetchSubPhase {
@Override
public FetchSubPhaseProcessor getProcessor(FetchContext context, SearchLookup lookup) {
public FetchSubPhaseProcessor getProcessor(FetchContext context) {
if (context.version() == false) {
return null;
}

View File

@ -31,7 +31,6 @@ import org.elasticsearch.search.fetch.FetchPhase;
import org.elasticsearch.search.fetch.FetchSearchResult;
import org.elasticsearch.search.fetch.FetchSubPhase;
import org.elasticsearch.search.fetch.FetchSubPhaseProcessor;
import org.elasticsearch.search.lookup.SearchLookup;
import org.elasticsearch.search.lookup.SourceLookup;
import java.io.IOException;
@ -47,7 +46,7 @@ public final class InnerHitsPhase implements FetchSubPhase {
}
@Override
public FetchSubPhaseProcessor getProcessor(FetchContext searchContext, SearchLookup lookup) {
public FetchSubPhaseProcessor getProcessor(FetchContext searchContext) {
if (searchContext.innerHits() == null) {
return null;
}

View File

@ -28,7 +28,6 @@ import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.search.fetch.FetchContext;
import org.elasticsearch.search.fetch.FetchSubPhase;
import org.elasticsearch.search.fetch.FetchSubPhaseProcessor;
import org.elasticsearch.search.lookup.SearchLookup;
import java.io.IOException;
import java.util.ArrayList;
@ -39,7 +38,7 @@ import java.util.Map;
public final class MatchedQueriesPhase implements FetchSubPhase {
@Override
public FetchSubPhaseProcessor getProcessor(FetchContext context, SearchLookup lookup) throws IOException {
public FetchSubPhaseProcessor getProcessor(FetchContext context) throws IOException {
Map<String, Query> namedQueries = new HashMap<>();
if (context.parsedQuery() != null) {
namedQueries.putAll(context.parsedQuery().namedFilters());

View File

@ -25,7 +25,6 @@ import org.elasticsearch.script.FieldScript;
import org.elasticsearch.search.fetch.FetchContext;
import org.elasticsearch.search.fetch.FetchSubPhase;
import org.elasticsearch.search.fetch.FetchSubPhaseProcessor;
import org.elasticsearch.search.lookup.SearchLookup;
import java.io.IOException;
import java.util.ArrayList;
@ -36,7 +35,7 @@ import java.util.List;
public final class ScriptFieldsPhase implements FetchSubPhase {
@Override
public FetchSubPhaseProcessor getProcessor(FetchContext context, SearchLookup lookup) {
public FetchSubPhaseProcessor getProcessor(FetchContext context) {
if (context.scriptFields() == null) {
return null;
}

View File

@ -25,14 +25,13 @@ import org.elasticsearch.index.seqno.SequenceNumbers;
import org.elasticsearch.search.fetch.FetchContext;
import org.elasticsearch.search.fetch.FetchSubPhase;
import org.elasticsearch.search.fetch.FetchSubPhaseProcessor;
import org.elasticsearch.search.lookup.SearchLookup;
import java.io.IOException;
public final class SeqNoPrimaryTermPhase implements FetchSubPhase {
@Override
public FetchSubPhaseProcessor getProcessor(FetchContext context, SearchLookup lookup) {
public FetchSubPhaseProcessor getProcessor(FetchContext context) {
if (context.seqNoAndPrimaryTerm() == false) {
return null;
}

View File

@ -29,7 +29,6 @@ import org.elasticsearch.index.mapper.TextFieldMapper;
import org.elasticsearch.search.fetch.FetchContext;
import org.elasticsearch.search.fetch.FetchSubPhase;
import org.elasticsearch.search.fetch.FetchSubPhaseProcessor;
import org.elasticsearch.search.lookup.SearchLookup;
import java.io.IOException;
import java.util.Collection;
@ -48,7 +47,7 @@ public class HighlightPhase implements FetchSubPhase {
}
@Override
public FetchSubPhaseProcessor getProcessor(FetchContext context, SearchLookup lookup) {
public FetchSubPhaseProcessor getProcessor(FetchContext context) {
if (context.highlight() == null) {
return null;
}

View File

@ -163,7 +163,7 @@ public class FetchSourcePhaseTests extends ESTestCase {
hitContext.sourceLookup().setSource(source == null ? null : BytesReference.bytes(source));
FetchSourcePhase phase = new FetchSourcePhase();
FetchSubPhaseProcessor processor = phase.getProcessor(fetchContext, null);
FetchSubPhaseProcessor processor = phase.getProcessor(fetchContext);
if (fetchSource == false) {
assertNull(processor);
} else {

View File

@ -118,7 +118,7 @@ public class AsyncSearchSingleNodeTests extends ESSingleNodeTestCase {
public static final class SubFetchPhasePlugin extends Plugin implements SearchPlugin {
@Override
public List<FetchSubPhase> getFetchSubPhases(FetchPhaseConstructionContext context) {
return Collections.singletonList((searchContext, lookup) -> new FetchSubPhaseProcessor() {
return Collections.singletonList(searchContext -> new FetchSubPhaseProcessor() {
@Override
public void setNextReader(LeafReaderContext readerContext) {}