Skip to content

Commit 24a24d0

Browse files
authored
Implement fields fetch for runtime fields (backport of #61995) (#62416)
This implements the `fields` API in `_search` for runtime fields using doc values. Most of that implementation is stolen from the `docvalue_fields` fetch sub-phase, just moved into the same API that the `fields` API uses. At this point the `docvalue_fields` fetch phase looks like a special case of the `fields` API. While I was at it I moved the "which doc values sub-implementation should I use for fetching?" question from a bunch of `instanceof`s to a method on `LeafFieldData` so we can be much more flexible with what is returned and we're not forced to extend certain classes just to make the fetch phase happy. Relates to #59332
1 parent f94ae7a commit 24a24d0

File tree

109 files changed

+699
-349
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+699
-349
lines changed

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeatureFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ private Float objectToFloat(Object value) {
183183
}
184184

185185
@Override
186-
public ValueFetcher valueFetcher(MapperService mapperService, String format) {
186+
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
187187
if (format != null) {
188188
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
189189
}

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeaturesFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ protected void parseCreateField(ParseContext context) throws IOException {
162162
}
163163

164164
@Override
165-
public ValueFetcher valueFetcher(MapperService mapperService, String format) {
165+
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
166166
if (format != null) {
167167
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
168168
}

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapper.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ private static double objectToDouble(Object value) {
398398
}
399399

400400
@Override
401-
public ValueFetcher valueFetcher(MapperService mapperService, String format) {
401+
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
402402
if (format != null) {
403403
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
404404
}
@@ -545,5 +545,25 @@ public int docValueCount() {
545545
}
546546
}
547547

548+
@Override
549+
public DocValueFetcher.Leaf getLeafValueFetcher(DocValueFormat format) {
550+
SortedNumericDoubleValues values = getDoubleValues();
551+
return new DocValueFetcher.Leaf() {
552+
@Override
553+
public boolean advanceExact(int docId) throws IOException {
554+
return values.advanceExact(docId);
555+
}
556+
557+
@Override
558+
public int docValueCount() throws IOException {
559+
return values.docValueCount();
560+
}
561+
562+
@Override
563+
public Object nextValue() throws IOException {
564+
return format.format(values.nextValue());
565+
}
566+
};
567+
}
548568
}
549569
}

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapper.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.elasticsearch.index.query.QueryShardContext;
5555
import org.elasticsearch.index.similarity.SimilarityProvider;
5656
import org.elasticsearch.index.similarity.SimilarityService;
57+
import org.elasticsearch.search.lookup.SearchLookup;
5758

5859
import java.io.IOException;
5960
import java.util.ArrayList;
@@ -419,7 +420,7 @@ protected void parseCreateField(ParseContext context) {
419420
}
420421

421422
@Override
422-
public ValueFetcher valueFetcher(MapperService mapperService, String format) {
423+
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
423424
throw new UnsupportedOperationException();
424425
}
425426

@@ -465,7 +466,7 @@ protected void mergeOptions(FieldMapper other, List<String> conflicts) {
465466
}
466467

467468
@Override
468-
public ValueFetcher valueFetcher(MapperService mapperService, String format) {
469+
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
469470
throw new UnsupportedOperationException();
470471
}
471472

@@ -588,7 +589,7 @@ protected void parseCreateField(ParseContext context) throws IOException {
588589
}
589590

590591
@Override
591-
public ValueFetcher valueFetcher(MapperService mapperService, String format) {
592+
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
592593
throw new UnsupportedOperationException();
593594
}
594595

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/TokenCountFieldMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.lucene.document.FieldType;
2626
import org.elasticsearch.common.xcontent.XContentBuilder;
2727
import org.elasticsearch.index.analysis.NamedAnalyzer;
28+
import org.elasticsearch.search.lookup.SearchLookup;
2829

2930
import java.io.IOException;
3031
import java.util.Iterator;
@@ -159,7 +160,7 @@ protected void parseCreateField(ParseContext context) throws IOException {
159160
}
160161

161162
@Override
162-
public ValueFetcher valueFetcher(MapperService mapperService, String format) {
163+
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
163164
if (format != null) {
164165
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
165166
}

modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeatureFieldMapperTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void testRejectMultiValuedFields() throws MapperParsingException, IOExcep
147147
e.getCause().getMessage());
148148
}
149149

150-
public void testFetchSourceValue() {
150+
public void testFetchSourceValue() throws IOException {
151151
Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id).build();
152152
Mapper.BuilderContext context = new Mapper.BuilderContext(settings, new ContentPath());
153153
RankFeatureFieldMapper mapper = new RankFeatureFieldMapper.Builder("field").build(context);

modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapperTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public void testRejectIndexOptions() {
260260
assertWarnings("Parameter [index_options] has no effect on type [scaled_float] and will be removed in future");
261261
}
262262

263-
public void testFetchSourceValue() {
263+
public void testFetchSourceValue() throws IOException {
264264
Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id).build();
265265
Mapper.BuilderContext context = new Mapper.BuilderContext(settings, new ContentPath());
266266

modules/mapper-extras/src/yamlRestTest/resources/rest-api-spec/test/scaled_float/10_basic.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,15 @@ setup:
122122
- match: { hits.total.value: 4 }
123123
- match: { hits.hits.0._id: "3" }
124124
- match: { hits.hits.0.sort.0: -2 }
125+
126+
---
127+
"docvalue_fields":
128+
129+
- do:
130+
search:
131+
body:
132+
docvalue_fields: [ "number" ]
133+
sort:
134+
number:
135+
order: asc
136+
- match: { hits.hits.0.fields.number: [-2.1] }

modules/parent-join/src/main/java/org/elasticsearch/join/mapper/MetaJoinFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ protected void parseCreateField(ParseContext context) throws IOException {
140140
}
141141

142142
@Override
143-
public ValueFetcher valueFetcher(MapperService mapperService, String format) {
143+
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
144144
throw new UnsupportedOperationException("Cannot fetch values for metadata field [" + typeName() + "].");
145145
}
146146

modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentIdFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ protected void parseCreateField(ParseContext context) throws IOException {
190190
}
191191

192192
@Override
193-
public ValueFetcher valueFetcher(MapperService mapperService, String format) {
193+
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
194194
throw new UnsupportedOperationException("Cannot fetch values for internal field [" + typeName() + "].");
195195
}
196196

0 commit comments

Comments
 (0)