Skip to content

Commit 4ca7bce

Browse files
committed
Add support for deriving source field from docValues for DateFieldMapper
Signed-off-by: Shreyansh Ray <rayshrey@amazon.com>
1 parent 13159c1 commit 4ca7bce

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

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

+35-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
import org.apache.lucene.document.SortedNumericDocValuesField;
3737
import org.apache.lucene.document.StoredField;
3838
import org.apache.lucene.index.IndexReader;
39+
import org.apache.lucene.index.LeafReader;
3940
import org.apache.lucene.index.PointValues;
41+
import org.apache.lucene.index.SortedNumericDocValues;
4042
import org.apache.lucene.search.BoostQuery;
4143
import org.apache.lucene.search.IndexOrDocValuesQuery;
4244
import org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery;
@@ -74,6 +76,7 @@
7476
import java.time.ZonedDateTime;
7577
import java.util.Arrays;
7678
import java.util.Collections;
79+
import java.util.HashMap;
7780
import java.util.List;
7881
import java.util.Locale;
7982
import java.util.Map;
@@ -330,7 +333,8 @@ public DateFieldMapper build(BuilderContext context) {
330333
buildFormatter(),
331334
resolution,
332335
nullValue.getValue(),
333-
meta.getValue()
336+
meta.getValue(),
337+
true
334338
);
335339
ft.setBoost(boost.getValue());
336340
Long nullTimestamp = parseNullValue(ft);
@@ -372,15 +376,29 @@ public DateFieldType(
372376
DateFormatter dateTimeFormatter,
373377
Resolution resolution,
374378
String nullValue,
375-
Map<String, String> meta
379+
Map<String, String> meta,
380+
boolean derivedSourceSupported
376381
) {
377-
super(name, isSearchable, isStored, hasDocValues, TextSearchInfo.SIMPLE_MATCH_ONLY, meta);
382+
super(name, isSearchable, isStored, hasDocValues, TextSearchInfo.SIMPLE_MATCH_ONLY, meta, derivedSourceSupported);
378383
this.dateTimeFormatter = dateTimeFormatter;
379384
this.dateMathParser = dateTimeFormatter.toDateMathParser();
380385
this.resolution = resolution;
381386
this.nullValue = nullValue;
382387
}
383388

389+
public DateFieldType(
390+
String name,
391+
boolean isSearchable,
392+
boolean isStored,
393+
boolean hasDocValues,
394+
DateFormatter dateTimeFormatter,
395+
Resolution resolution,
396+
String nullValue,
397+
Map<String, String> meta
398+
) {
399+
this(name, isSearchable, isStored, hasDocValues, dateTimeFormatter, resolution, nullValue, meta, false);
400+
}
401+
384402
public DateFieldType(String name) {
385403
this(name, true, false, true, getDefaultDateTimeFormatter(), Resolution.MILLISECONDS, null, Collections.emptyMap());
386404
}
@@ -800,4 +818,18 @@ public boolean getIgnoreMalformed() {
800818
public Long getNullValue() {
801819
return nullValue;
802820
}
821+
822+
@Override
823+
protected String[] deriveSource(LeafReader leafReader, int docId) throws IOException {
824+
SortedNumericDocValues sortedNumericDocValues = leafReader.getSortedNumericDocValues(name());
825+
if (sortedNumericDocValues.advanceExact(docId)) {
826+
int size = sortedNumericDocValues.docValueCount();
827+
String[] values = new String[size];
828+
DateFormatter dateFormatter = fieldType().dateTimeFormatter;
829+
for (int i = 0; i < size; i++)
830+
values[i] = dateFormatter.formatMillis(sortedNumericDocValues.nextValue());
831+
return values;
832+
}
833+
return null;
834+
}
803835
}

server/src/main/java/org/opensearch/index/mapper/FieldMapper.java

+12
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.lucene.document.Field;
3636
import org.apache.lucene.document.FieldType;
3737
import org.apache.lucene.index.IndexOptions;
38+
import org.apache.lucene.index.LeafReader;
3839
import org.opensearch.common.annotation.PublicApi;
3940
import org.opensearch.common.settings.Setting;
4041
import org.opensearch.common.settings.Setting.Property;
@@ -571,6 +572,17 @@ protected static String indexOptionToString(IndexOptions indexOption) {
571572

572573
protected abstract String contentType();
573574

575+
public Object getDerivedSource(LeafReader leafReader, int docId) throws IOException{
576+
if (mappedFieldType.isDerivedSourceSupported() == false)
577+
throw new UnsupportedOperationException("Derived source field is not supported for [" + name() + "] field");
578+
return deriveSource(leafReader, docId);
579+
}
580+
581+
// generic implementation, override in subclasses for specific implementation
582+
protected Object deriveSource(LeafReader leafReader, int docId) throws IOException {
583+
return null;
584+
}
585+
574586
/**
575587
* Multi field implementation used across field mappers
576588
*

server/src/main/java/org/opensearch/index/mapper/MappedFieldType.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,16 @@ public abstract class MappedFieldType {
9494
private float boost;
9595
private NamedAnalyzer indexAnalyzer;
9696
private boolean eagerGlobalOrdinals;
97+
private final boolean derivedSourceSupported;
9798

9899
public MappedFieldType(
99100
String name,
100101
boolean isIndexed,
101102
boolean isStored,
102103
boolean hasDocValues,
103104
TextSearchInfo textSearchInfo,
104-
Map<String, String> meta
105+
Map<String, String> meta,
106+
boolean derivedSourceSupported
105107
) {
106108
setBoost(1.0f);
107109
this.name = Objects.requireNonNull(name);
@@ -110,6 +112,18 @@ public MappedFieldType(
110112
this.docValues = hasDocValues;
111113
this.textSearchInfo = Objects.requireNonNull(textSearchInfo);
112114
this.meta = meta;
115+
this.derivedSourceSupported = derivedSourceSupported;
116+
}
117+
118+
public MappedFieldType(
119+
String name,
120+
boolean isIndexed,
121+
boolean isStored,
122+
boolean hasDocValues,
123+
TextSearchInfo textSearchInfo,
124+
Map<String, String> meta
125+
) {
126+
this(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta, false);
113127
}
114128

115129
/**
@@ -158,6 +172,10 @@ public boolean hasDocValues() {
158172
return docValues;
159173
}
160174

175+
public boolean isDerivedSourceSupported() {
176+
return derivedSourceSupported;
177+
}
178+
161179
public NamedAnalyzer indexAnalyzer() {
162180
return indexAnalyzer;
163181
}

0 commit comments

Comments
 (0)