Skip to content

Commit 8348189

Browse files
committed
WIP: index-time scripted fields to be fetched from doc values
1 parent e8bbf27 commit 8348189

File tree

8 files changed

+43
-97
lines changed

8 files changed

+43
-97
lines changed

server/src/main/java/org/elasticsearch/index/mapper/BooleanFieldMapper.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.elasticsearch.script.field.BooleanDocValuesField;
4141
import org.elasticsearch.search.DocValueFormat;
4242
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
43+
import org.elasticsearch.search.fetch.StoredFieldsSpec;
4344
import org.elasticsearch.search.lookup.FieldValues;
4445
import org.elasticsearch.search.lookup.SearchLookup;
4546
import org.elasticsearch.xcontent.XContentBuilder;
@@ -238,7 +239,11 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)
238239
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
239240
}
240241
if (this.scriptValues != null) {
241-
return FieldValues.valueFetcher(this.scriptValues, context);
242+
return new DocValueFetcher(
243+
docValueFormat(format, null),
244+
context.getForField(this, FielddataOperation.SEARCH),
245+
StoredFieldsSpec.NO_REQUIREMENTS
246+
);
242247
}
243248
return sourceValueFetcher(context.isSourceEnabled() ? context.sourcePath(name()) : Collections.emptySet());
244249
}

server/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.elasticsearch.script.field.DateNanosDocValuesField;
5454
import org.elasticsearch.script.field.ToScriptFieldFactory;
5555
import org.elasticsearch.search.DocValueFormat;
56+
import org.elasticsearch.search.fetch.StoredFieldsSpec;
5657
import org.elasticsearch.search.lookup.FieldValues;
5758
import org.elasticsearch.search.lookup.SearchLookup;
5859
import org.elasticsearch.search.runtime.LongScriptFieldDistanceFeatureQuery;
@@ -548,7 +549,11 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)
548549
? DateFormatter.forPattern(format).withLocale(defaultFormatter.locale())
549550
: defaultFormatter;
550551
if (scriptValues != null) {
551-
return FieldValues.valueFetcher(scriptValues, v -> format((long) v, formatter), context);
552+
return new DocValueFetcher(
553+
docValueFormat(format, null),
554+
context.getForField(this, FielddataOperation.SEARCH),
555+
StoredFieldsSpec.NO_REQUIREMENTS
556+
);
552557
}
553558
return new SourceValueFetcher(name(), context, nullValue) {
554559
@Override

server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
5050
import org.elasticsearch.search.aggregations.support.TimeSeriesValuesSourceType;
5151
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
52+
import org.elasticsearch.search.fetch.StoredFieldsSpec;
5253
import org.elasticsearch.search.lookup.FieldValues;
5354
import org.elasticsearch.search.lookup.SearchLookup;
5455
import org.elasticsearch.search.runtime.GeoPointScriptFieldDistanceFeatureQuery;
@@ -419,8 +420,11 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)
419420
if (scriptValues == null) {
420421
return super.valueFetcher(context, format);
421422
}
422-
Function<List<GeoPoint>, List<Object>> formatter = getFormatter(format != null ? format : GeometryFormatterFactory.GEOJSON);
423-
return FieldValues.valueListFetcher(scriptValues, formatter, context);
423+
return new DocValueFetcher(
424+
docValueFormat(format, null),
425+
context.getForField(this, FielddataOperation.SEARCH),
426+
StoredFieldsSpec.NO_REQUIREMENTS
427+
);
424428
}
425429

426430
@Override

server/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.elasticsearch.script.field.IpDocValuesField;
4242
import org.elasticsearch.search.DocValueFormat;
4343
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
44+
import org.elasticsearch.search.fetch.StoredFieldsSpec;
4445
import org.elasticsearch.search.lookup.FieldValues;
4546
import org.elasticsearch.search.lookup.SearchLookup;
4647

@@ -285,7 +286,11 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)
285286
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
286287
}
287288
if (scriptValues != null) {
288-
return FieldValues.valueFetcher(scriptValues, v -> InetAddresses.toAddrString((InetAddress) v), context);
289+
return new DocValueFetcher(
290+
docValueFormat(format, null),
291+
context.getForField(this, FielddataOperation.SEARCH),
292+
StoredFieldsSpec.NO_REQUIREMENTS
293+
);
289294
}
290295
return new SourceValueFetcher(name(), context, nullValue) {
291296
@Override

server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import org.elasticsearch.script.StringFieldScript;
6060
import org.elasticsearch.script.field.KeywordDocValuesField;
6161
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
62+
import org.elasticsearch.search.fetch.StoredFieldsSpec;
6263
import org.elasticsearch.search.lookup.FieldValues;
6364
import org.elasticsearch.search.lookup.SearchLookup;
6465
import org.elasticsearch.search.runtime.StringScriptFieldFuzzyQuery;
@@ -843,7 +844,11 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)
843844
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
844845
}
845846
if (this.scriptValues != null) {
846-
return FieldValues.valueFetcher(this.scriptValues, context);
847+
return new DocValueFetcher(
848+
docValueFormat(format, null),
849+
context.getForField(this, FielddataOperation.SEARCH),
850+
StoredFieldsSpec.NO_REQUIREMENTS
851+
);
847852
}
848853
return sourceValueFetcher(context.isSourceEnabled() ? context.sourcePath(name()) : Collections.emptySet());
849854
}

server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.elasticsearch.search.DocValueFormat;
6161
import org.elasticsearch.search.aggregations.support.TimeSeriesValuesSourceType;
6262
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
63+
import org.elasticsearch.search.fetch.StoredFieldsSpec;
6364
import org.elasticsearch.search.lookup.FieldValues;
6465
import org.elasticsearch.search.lookup.SearchLookup;
6566
import org.elasticsearch.search.lookup.SourceProvider;
@@ -1806,7 +1807,11 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)
18061807
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
18071808
}
18081809
if (this.scriptValues != null) {
1809-
return FieldValues.valueFetcher(this.scriptValues, context);
1810+
return new DocValueFetcher(
1811+
docValueFormat(format, null),
1812+
context.getForField(this, FielddataOperation.SEARCH),
1813+
StoredFieldsSpec.NO_REQUIREMENTS
1814+
);
18101815
}
18111816
return sourceValueFetcher(context.isSourceEnabled() ? context.sourcePath(name()) : Collections.emptySet());
18121817
}

server/src/main/java/org/elasticsearch/search/lookup/FieldValues.java

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,8 @@
1010
package org.elasticsearch.search.lookup;
1111

1212
import org.apache.lucene.index.LeafReaderContext;
13-
import org.elasticsearch.index.mapper.ValueFetcher;
14-
import org.elasticsearch.index.query.SearchExecutionContext;
15-
import org.elasticsearch.search.fetch.StoredFieldsSpec;
1613

17-
import java.util.ArrayList;
18-
import java.util.List;
1914
import java.util.function.Consumer;
20-
import java.util.function.Function;
2115

2216
/**
2317
* Represents values for a given document
@@ -32,86 +26,4 @@ public interface FieldValues<T> {
3226
* @param consumer called with each document value
3327
*/
3428
void valuesForDoc(SearchLookup lookup, LeafReaderContext ctx, int doc, Consumer<T> consumer);
35-
36-
/**
37-
* Creates a {@link ValueFetcher} that fetches values from a {@link FieldValues} instance
38-
* @param fieldValues the source of the values
39-
* @param context the search execution context
40-
* @return the value fetcher
41-
*/
42-
static ValueFetcher valueFetcher(FieldValues<?> fieldValues, SearchExecutionContext context) {
43-
return valueFetcher(fieldValues, v -> v, context);
44-
}
45-
46-
/**
47-
* Creates a {@link ValueFetcher} that fetches values from a {@link FieldValues} instance
48-
* @param fieldValues the source of the values
49-
* @param formatter a function to format the values
50-
* @param context the search execution context
51-
* @return the value fetcher
52-
*/
53-
static ValueFetcher valueFetcher(FieldValues<?> fieldValues, Function<Object, Object> formatter, SearchExecutionContext context) {
54-
return new ValueFetcher() {
55-
LeafReaderContext ctx;
56-
57-
@Override
58-
public void setNextReader(LeafReaderContext context) {
59-
this.ctx = context;
60-
}
61-
62-
@Override
63-
public List<Object> fetchValues(Source lookup, int doc, List<Object> ignoredValues) {
64-
List<Object> values = new ArrayList<>();
65-
try {
66-
fieldValues.valuesForDoc(context.lookup(), ctx, doc, v -> values.add(formatter.apply(v)));
67-
} catch (Exception e) {
68-
ignoredValues.addAll(values);
69-
}
70-
return values;
71-
}
72-
73-
@Override
74-
public StoredFieldsSpec storedFieldsSpec() {
75-
return StoredFieldsSpec.NEEDS_SOURCE; // TODO can we get more information from the script
76-
}
77-
};
78-
}
79-
80-
/**
81-
* Creates a {@link ValueFetcher} that fetches values from a {@link FieldValues} instance
82-
* @param fieldValues the source of the values
83-
* @param formatter a function to format the list values
84-
* @param context the search execution context
85-
* @return the value fetcher
86-
*/
87-
static <T> ValueFetcher valueListFetcher(
88-
FieldValues<T> fieldValues,
89-
Function<List<T>, List<Object>> formatter,
90-
SearchExecutionContext context
91-
) {
92-
return new ValueFetcher() {
93-
LeafReaderContext ctx;
94-
95-
@Override
96-
public void setNextReader(LeafReaderContext context) {
97-
this.ctx = context;
98-
}
99-
100-
@Override
101-
public List<Object> fetchValues(Source source, int doc, List<Object> ignoredValues) {
102-
List<T> values = new ArrayList<>();
103-
try {
104-
fieldValues.valuesForDoc(context.lookup(), ctx, doc, values::add);
105-
} catch (Exception e) {
106-
ignoredValues.addAll(values);
107-
}
108-
return formatter.apply(values);
109-
}
110-
111-
@Override
112-
public StoredFieldsSpec storedFieldsSpec() {
113-
return StoredFieldsSpec.NEEDS_SOURCE; // TODO can we get more info from the script?
114-
}
115-
};
116-
}
11729
}

x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.elasticsearch.index.fielddata.ScriptDocValues;
3535
import org.elasticsearch.index.mapper.AbstractShapeGeometryFieldMapper;
3636
import org.elasticsearch.index.mapper.BlockLoader;
37+
import org.elasticsearch.index.mapper.DocValueFetcher;
3738
import org.elasticsearch.index.mapper.DocumentParserContext;
3839
import org.elasticsearch.index.mapper.FieldMapper;
3940
import org.elasticsearch.index.mapper.GeoShapeIndexer;
@@ -59,6 +60,7 @@
5960
import org.elasticsearch.script.field.AbstractScriptFieldFactory;
6061
import org.elasticsearch.script.field.DocValuesScriptFieldFactory;
6162
import org.elasticsearch.script.field.Field;
63+
import org.elasticsearch.search.fetch.StoredFieldsSpec;
6264
import org.elasticsearch.search.lookup.FieldValues;
6365
import org.elasticsearch.search.lookup.SearchLookup;
6466
import org.elasticsearch.xpack.spatial.index.fielddata.GeoShapeValues;
@@ -293,8 +295,11 @@ public List<Object> parseStoredValues(List<Object> storedValues) {
293295
};
294296
}
295297
if (scriptValues != null) {
296-
Function<List<Geometry>, List<Object>> formatter = getFormatter(format != null ? format : GeometryFormatterFactory.GEOJSON);
297-
return FieldValues.valueListFetcher(scriptValues, formatter, context);
298+
return new DocValueFetcher(
299+
docValueFormat(format, null),
300+
context.getForField(this, FielddataOperation.SEARCH),
301+
StoredFieldsSpec.NO_REQUIREMENTS
302+
);
298303
}
299304
return super.valueFetcher(context, format);
300305
}

0 commit comments

Comments
 (0)