-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Changes DocValueFieldsFetchSubPhase to reuse doc values iterators for multiple hits #25644
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d0e9038
Changes DocValueFieldsFetchSubPhase to reuse doc values iterators for…
colings86 e0e9e74
iter
colings86 ec37b53
Update ScriptDocValues to not reuse GeoPoint and Date objects
colings86 c8efa14
added Javadoc about script value re-use
colings86 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,15 +18,19 @@ | |
| */ | ||
| package org.elasticsearch.search.fetch.subphase; | ||
|
|
||
| import org.apache.lucene.index.LeafReaderContext; | ||
| import org.apache.lucene.index.ReaderUtil; | ||
| import org.elasticsearch.common.document.DocumentField; | ||
| import org.elasticsearch.index.fielddata.AtomicFieldData; | ||
| import org.elasticsearch.index.fielddata.ScriptDocValues; | ||
| import org.elasticsearch.index.mapper.MappedFieldType; | ||
| import org.elasticsearch.search.SearchHit; | ||
| import org.elasticsearch.search.fetch.FetchSubPhase; | ||
| import org.elasticsearch.search.internal.SearchContext; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
|
|
||
|
|
@@ -38,7 +42,8 @@ | |
| public final class DocValueFieldsFetchSubPhase implements FetchSubPhase { | ||
|
|
||
| @Override | ||
| public void hitExecute(SearchContext context, HitContext hitContext) throws IOException { | ||
| public void hitsExecute(SearchContext context, SearchHit[] hits) throws IOException { | ||
|
|
||
| if (context.collapse() != null) { | ||
| // retrieve the `doc_value` associated with the collapse field | ||
| String name = context.collapse().getFieldType().name(); | ||
|
|
@@ -48,26 +53,40 @@ public void hitExecute(SearchContext context, HitContext hitContext) throws IOEx | |
| context.docValueFieldsContext().fields().add(name); | ||
| } | ||
| } | ||
|
|
||
| if (context.docValueFieldsContext() == null) { | ||
| return; | ||
| } | ||
|
|
||
| hits = hits.clone(); // don't modify the incoming hits | ||
| Arrays.sort(hits, (a, b) -> Integer.compare(a.docId(), b.docId())); | ||
|
||
|
|
||
| for (String field : context.docValueFieldsContext().fields()) { | ||
| if (hitContext.hit().fieldsOrNull() == null) { | ||
| hitContext.hit().fields(new HashMap<>(2)); | ||
| } | ||
| DocumentField hitField = hitContext.hit().getFields().get(field); | ||
| if (hitField == null) { | ||
| hitField = new DocumentField(field, new ArrayList<>(2)); | ||
| hitContext.hit().getFields().put(field, hitField); | ||
| } | ||
| MappedFieldType fieldType = context.mapperService().fullName(field); | ||
| if (fieldType != null) { | ||
| /* Because this is called once per document we end up creating a new ScriptDocValues for every document which is important | ||
| * because the values inside ScriptDocValues might be reused for different documents (Dates do this). */ | ||
| AtomicFieldData data = context.fieldData().getForField(fieldType).load(hitContext.readerContext()); | ||
| ScriptDocValues<?> values = data.getScriptValues(); | ||
| values.setNextDocId(hitContext.docId()); | ||
| hitField.getValues().addAll(values); | ||
| LeafReaderContext subReaderContext = null; | ||
| AtomicFieldData data = null; | ||
| ScriptDocValues<?> values = null; | ||
| for (SearchHit hit : hits) { | ||
| // if the reader index has changed we need to get a new doc values reader instance | ||
| if (subReaderContext == null || hit.docId() >= subReaderContext.docBase + subReaderContext.reader().maxDoc()) { | ||
| int readerIndex = ReaderUtil.subIndex(hit.docId(), context.searcher().getIndexReader().leaves()); | ||
| subReaderContext = context.searcher().getIndexReader().leaves().get(readerIndex); | ||
| data = context.fieldData().getForField(fieldType).load(subReaderContext); | ||
| values = data.getScriptValues(); | ||
| } | ||
| int subDocId = hit.docId() - subReaderContext.docBase; | ||
| values.setNextDocId(subDocId); | ||
| if (hit.fieldsOrNull() == null) { | ||
| hit.fields(new HashMap<>(2)); | ||
| } | ||
| DocumentField hitField = hit.getFields().get(field); | ||
| if (hitField == null) { | ||
| hitField = new DocumentField(field, new ArrayList<>(2)); | ||
| hit.getFields().put(field, hitField); | ||
| } | ||
| hitField.getValues().addAll(values); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering whether we should keep things this way here and do the cloning in
get(int index)/getValue()to help GC by having even shorter lived objects, and potentially make escape analysis more likely to not ever create those objects.