Skip to content

Implement fields fetch for runtime fields #61995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
17f46a6
Implement fields fetch for runtime fields
nik9000 Sep 3, 2020
9be09d7
Doc
nik9000 Sep 4, 2020
186a911
Merge branch 'master' into runtime_fields_fetch_fields_take_two
nik9000 Sep 4, 2020
9b4b344
Use clean nested context
nik9000 Sep 4, 2020
9565d7d
Date nanos!
nik9000 Sep 4, 2020
ef3b2a4
Update test now that we force the right resolution in the formatter
nik9000 Sep 4, 2020
af9da06
Delay building all fetchers
nik9000 Sep 4, 2020
56405f5
Fixup
nik9000 Sep 4, 2020
e494412
Map
nik9000 Sep 4, 2020
7099198
Merge branch 'master' into runtime_fields_fetch_fields_take_two
nik9000 Sep 8, 2020
6313a12
Pass lookup when building
nik9000 Sep 8, 2020
6a6584b
Cleanup
nik9000 Sep 8, 2020
4e5bd65
Close over lookup
nik9000 Sep 8, 2020
c668838
Remove oops
nik9000 Sep 9, 2020
103df6a
Something is up with dates
nik9000 Sep 9, 2020
c63a886
Revert "Map"
nik9000 Sep 9, 2020
d7315fe
Merge branch 'master' into runtime_fields_fetch_fields_take_two
nik9000 Sep 10, 2020
f92f8a5
Revert "Close over lookup"
nik9000 Sep 10, 2020
122663c
Fetch lookup
nik9000 Sep 10, 2020
77f130c
Rename k plz
nik9000 Sep 10, 2020
0fe6b3d
Merge branch 'master' into runtime_fields_fetch_fields_take_two
nik9000 Sep 10, 2020
3790f5e
Compile plz
nik9000 Sep 10, 2020
459808f
Merge branch 'master' into runtime_fields_fetch_fields_take_two
nik9000 Sep 15, 2020
2055c32
Compile
nik9000 Sep 15, 2020
d9e6209
Iter
nik9000 Sep 15, 2020
137ac90
oops
nik9000 Sep 15, 2020
d666f3f
Merge branch 'master' into runtime_fields_fetch_fields_take_two
nik9000 Sep 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,21 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

/**
* Value fetcher that loads from doc values.
*/
public final class DocValueFetcher implements ValueFetcher {
private final DocValueFormat format;
private final Supplier<IndexFieldData<?>> ifdSource;
/**
* Field data implementation to load the values. Lazily initialized
* because it is expensive.
*/
private IndexFieldData<?> ifd;
private final IndexFieldData<?> ifd;
private Leaf leaf;

/**
* Build the fetcher.
* @param format the format to use
* @param ifdSource a supplier that will build the field data when we need it.
* We don't take the {@code IndexFieldData} directly here because
* it can be expensive to build and use a fair bit of memory so
* we wait until we're sure we need it.
*/
public DocValueFetcher(DocValueFormat format, Supplier<IndexFieldData<?>> ifdSource) {
public DocValueFetcher(DocValueFormat format, IndexFieldData<?> ifd) {
this.format = format;
this.ifdSource = ifdSource;
this.ifd = ifd;
}

public void setNextReader(LeafReaderContext context) {
if (ifd == null) {
ifd = ifdSource.get();
}
leaf = ifd.load(context).getLeafValueFetcher(format);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public FetchSubPhaseProcessor getProcessor(SearchContext context) throws IOExcep
if (ft == null) {
continue;
}
ValueFetcher fetcher = new DocValueFetcher(ft.docValueFormat(fieldAndFormat.format, null), () -> context.getForField(ft));
ValueFetcher fetcher = new DocValueFetcher(ft.docValueFormat(fieldAndFormat.format, null), context.getForField(ft));
fields.add(new DocValueField(fieldAndFormat.field, fetcher));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.elasticsearch.search.lookup.SearchLookup;

import java.util.List;
import java.util.function.BiFunction;

/**
* The context needed to retrieve fields.
Expand All @@ -36,21 +37,21 @@ public static FetchFieldsContext create(
) {
DocumentMapper documentMapper = mapperService.documentMapper();
if (documentMapper.sourceMapper().enabled() == false) {
throw new IllegalArgumentException("Unable to retrieve the requested [fields] since _source is " +
"disabled in the mappings for index [" + indexName + "]");
throw new IllegalArgumentException(
"Unable to retrieve the requested [fields] since _source is " + "disabled in the mappings for index [" + indexName + "]"
);
}

FieldValueRetriever fieldValueRetriever = FieldValueRetriever.create(mapperService, searchLookup, fields);
return new FetchFieldsContext(fieldValueRetriever);
return new FetchFieldsContext((m, s) -> FieldValueRetriever.create(m, s, fields));
}

private final FieldValueRetriever fieldValueRetriever;
private final BiFunction<MapperService, SearchLookup, FieldValueRetriever> fieldValueRetriever;

private FetchFieldsContext(FieldValueRetriever fieldValueRetriever) {
private FetchFieldsContext(BiFunction<MapperService, SearchLookup, FieldValueRetriever> fieldValueRetriever) {
this.fieldValueRetriever = fieldValueRetriever;
}

public FieldValueRetriever fieldValueRetriever() {
return fieldValueRetriever;
public FieldValueRetriever fieldValueRetriever(MapperService mapperService, SearchLookup searchLookup) {
return fieldValueRetriever.apply(mapperService, searchLookup);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,23 @@ public FetchSubPhaseProcessor getProcessor(SearchContext searchContext) {
if (fetchFieldsContext == null) {
return null;
}
FieldValueRetriever retriever = fetchFieldsContext.fieldValueRetriever(
searchContext.mapperService(),
searchContext.getQueryShardContext().fetchLookup()
);
return new FetchSubPhaseProcessor() {
@Override
public void setNextReader(LeafReaderContext readerContext) {
fetchFieldsContext.fieldValueRetriever().setNextReader(readerContext);
retriever.setNextReader(readerContext);
}

@Override
public void process(HitContext hitContext) throws IOException {
SearchHit hit = hitContext.hit();
SourceLookup sourceLookup = hitContext.sourceLookup();
FieldValueRetriever fieldValueRetriever = fetchFieldsContext.fieldValueRetriever();

Set<String> ignoredFields = getIgnoredFields(hit);
Map<String, DocumentField> documentFields = fieldValueRetriever.retrieve(sourceLookup, ignoredFields);
Map<String, DocumentField> documentFields = retriever.retrieve(sourceLookup, ignoredFields);
for (Map.Entry<String, DocumentField> entry : documentFields.entrySet()) {
hit.setDocumentField(entry.getKey(), entry.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ protected final List<?> fetchFromDocValues(FieldMapper mapper, DocValueFormat fo
}, iw -> {
IndexSearcher indexSearcher = newSearcher(iw);
SearchLookup lookup = new SearchLookup(mapperService, fieldDataLookup);
ValueFetcher valueFetcher = new DocValueFetcher(format, () -> lookup.doc().getForField(mapper.fieldType()));
ValueFetcher valueFetcher = new DocValueFetcher(format, lookup.doc().getForField(mapper.fieldType()));
indexSearcher.search(new MatchAllDocsQuery(), new Collector() {
@Override
public ScoreMode scoreMode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected void parseCreateField(ParseContext context) {

@Override
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup lookup, String format) {
return new DocValueFetcher(fieldType().docValueFormat(format, null), () -> lookup.doc().getForField(fieldType()));
return new DocValueFetcher(fieldType().docValueFormat(format, null), lookup.doc().getForField(fieldType()));
}

@Override
Expand Down