-
Notifications
You must be signed in to change notification settings - Fork 24.7k
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
FetchFieldsPhase needs to use local docid, not global #90901
Merged
Merged
Changes from all commits
Commits
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 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
89 changes: 89 additions & 0 deletions
89
server/src/test/java/org/elasticsearch/search/fetch/subphase/FetchFieldsPhaseTests.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.search.fetch.subphase; | ||
|
||
import org.apache.lucene.document.Document; | ||
import org.apache.lucene.document.NumericDocValuesField; | ||
import org.apache.lucene.index.IndexReader; | ||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.store.Directory; | ||
import org.apache.lucene.tests.index.RandomIndexWriter; | ||
import org.elasticsearch.index.fielddata.IndexNumericFieldData; | ||
import org.elasticsearch.index.fielddata.plain.SortedNumericIndexFieldData; | ||
import org.elasticsearch.index.mapper.DocValueFetcher; | ||
import org.elasticsearch.index.mapper.MappedFieldType; | ||
import org.elasticsearch.index.mapper.NestedLookup; | ||
import org.elasticsearch.index.query.SearchExecutionContext; | ||
import org.elasticsearch.search.DocValueFormat; | ||
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.test.ESTestCase; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class FetchFieldsPhaseTests extends ESTestCase { | ||
|
||
public void testDocValueFetcher() throws IOException { | ||
|
||
Directory dir = newDirectory(); | ||
RandomIndexWriter iw = new RandomIndexWriter(random(), dir); | ||
int numDocs = randomIntBetween(200, 300); | ||
for (int i = 0; i < numDocs; i++) { | ||
Document doc = new Document(); | ||
doc.add(new NumericDocValuesField("field", i)); | ||
iw.addDocument(doc); | ||
if (i % 27 == 0) { | ||
iw.commit(); | ||
} | ||
} | ||
|
||
iw.commit(); | ||
IndexReader reader = iw.getReader(); | ||
iw.close(); | ||
|
||
FetchFieldsContext ffc = new FetchFieldsContext(List.of(new FieldAndFormat("field", null))); | ||
|
||
SearchExecutionContext sec = mock(SearchExecutionContext.class); | ||
MappedFieldType fieldType = mock(MappedFieldType.class); | ||
when(fieldType.valueFetcher(any(), any())).thenReturn( | ||
new DocValueFetcher(DocValueFormat.RAW, new SortedNumericIndexFieldData("field", IndexNumericFieldData.NumericType.LONG, null)) | ||
); | ||
when(sec.getFieldType(any())).thenReturn(fieldType); | ||
when(sec.getMatchingFieldNames(any())).thenReturn(Set.of("field")); | ||
when(sec.nestedLookup()).thenReturn(NestedLookup.EMPTY); | ||
FetchContext fetchContext = mock(FetchContext.class); | ||
when(fetchContext.fetchFieldsContext()).thenReturn(ffc); | ||
when(fetchContext.getSearchExecutionContext()).thenReturn(sec); | ||
|
||
FetchFieldsPhase phase = new FetchFieldsPhase(); | ||
FetchSubPhaseProcessor processor = phase.getProcessor(fetchContext); | ||
assertNotNull(processor); | ||
|
||
for (LeafReaderContext context : reader.leaves()) { | ||
processor.setNextReader(context); | ||
for (int doc = 0; doc < context.reader().maxDoc(); doc++) { | ||
SearchHit searchHit = new SearchHit(doc + context.docBase); | ||
processor.process(new FetchSubPhase.HitContext(searchHit, context, doc)); | ||
assertNotNull(searchHit.getFields().get("field")); | ||
} | ||
} | ||
|
||
reader.close(); | ||
dir.close(); | ||
|
||
} | ||
} |
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.
Would it be a lot of work to try and do without mocking? MapperServiceTestCase has a utility method to create a search execution context but it requires a mapper service instance. Wondering if it may be worth changing.
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.
We need a special MappedFieldType that returns a DocValueFetcher, and we don't have any core mappers that use DocValueFetcher. So somewhat counter-intuitively, not using mocks here ends up with much more ceremony because we need to register plugins and create special Mappers and TypeParsers, etc.
I would love to extract some interfaces from SearchExecutionContext though. Loads of places that take it only need the 'return all matching field types' functionality, and it's a very heavy object to construct for tests.