Skip to content

Commit bf31ee6

Browse files
Doc values sparse index on _tsid fields (elastic#122699)
This patch extends the work done in elastic#122161 and elastic#121751 to also use the doc values sparse index for the _tsid fields in time-series mode indices.
1 parent 2c15b68 commit bf31ee6

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

server/src/main/java/org/elasticsearch/index/IndexMode.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.index.mapper.KeywordFieldMapper;
2929
import org.elasticsearch.index.mapper.MapperService;
3030
import org.elasticsearch.index.mapper.MappingLookup;
31+
import org.elasticsearch.index.mapper.MappingParserContext;
3132
import org.elasticsearch.index.mapper.MetadataFieldMapper;
3233
import org.elasticsearch.index.mapper.ProvidedIdFieldMapper;
3334
import org.elasticsearch.index.mapper.RoutingFieldMapper;
@@ -90,7 +91,7 @@ public TimestampBounds getTimestampBound(IndexMetadata indexMetadata) {
9091
}
9192

9293
@Override
93-
public MetadataFieldMapper timeSeriesIdFieldMapper() {
94+
public MetadataFieldMapper timeSeriesIdFieldMapper(MappingParserContext c) {
9495
// non time-series indices must not have a TimeSeriesIdFieldMapper
9596
return null;
9697
}
@@ -187,8 +188,8 @@ private static String routingRequiredBad() {
187188
}
188189

189190
@Override
190-
public MetadataFieldMapper timeSeriesIdFieldMapper() {
191-
return TimeSeriesIdFieldMapper.INSTANCE;
191+
public MetadataFieldMapper timeSeriesIdFieldMapper(MappingParserContext c) {
192+
return TimeSeriesIdFieldMapper.getInstance(c);
192193
}
193194

194195
@Override
@@ -277,7 +278,7 @@ public TimestampBounds getTimestampBound(IndexMetadata indexMetadata) {
277278
}
278279

279280
@Override
280-
public MetadataFieldMapper timeSeriesIdFieldMapper() {
281+
public MetadataFieldMapper timeSeriesIdFieldMapper(MappingParserContext c) {
281282
// non time-series indices must not have a TimeSeriesIdFieldMapper
282283
return null;
283284
}
@@ -348,7 +349,7 @@ public TimestampBounds getTimestampBound(IndexMetadata indexMetadata) {
348349
}
349350

350351
@Override
351-
public MetadataFieldMapper timeSeriesIdFieldMapper() {
352+
public MetadataFieldMapper timeSeriesIdFieldMapper(MappingParserContext c) {
352353
// non time-series indices must not have a TimeSeriesIdFieldMapper
353354
return null;
354355
}
@@ -518,7 +519,7 @@ public String getName() {
518519
* the _tsid field. The field mapper will be added to the list of the metadata
519520
* field mappers for the index.
520521
*/
521-
public abstract MetadataFieldMapper timeSeriesIdFieldMapper();
522+
public abstract MetadataFieldMapper timeSeriesIdFieldMapper(MappingParserContext c);
522523

523524
/**
524525
* Return an instance of the {@link TimeSeriesRoutingHashFieldMapper} that generates

server/src/main/java/org/elasticsearch/index/IndexVersions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ private static Version parseUnchecked(String version) {
147147
public static final IndexVersion UPGRADE_TO_LUCENE_10_1_0 = def(9_009_0_00, Version.LUCENE_10_1_0);
148148
public static final IndexVersion USE_SYNTHETIC_SOURCE_FOR_RECOVERY_BY_DEFAULT = def(9_010_00_0, Version.LUCENE_10_1_0);
149149
public static final IndexVersion TIMESTAMP_DOC_VALUES_SPARSE_INDEX = def(9_011_0_00, Version.LUCENE_10_1_0);
150+
public static final IndexVersion TIME_SERIES_ID_DOC_VALUES_SPARSE_INDEX = def(9_012_0_00, Version.LUCENE_10_1_0);
150151
/*
151152
* STOP! READ THIS FIRST! No, really,
152153
* ____ _____ ___ ____ _ ____ _____ _ ____ _____ _ _ ___ ____ _____ ___ ____ ____ _____ _

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

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,36 @@ public class TimeSeriesIdFieldMapper extends MetadataFieldMapper {
4646
public static final String NAME = "_tsid";
4747
public static final String CONTENT_TYPE = "_tsid";
4848
public static final TimeSeriesIdFieldType FIELD_TYPE = new TimeSeriesIdFieldType();
49-
public static final TimeSeriesIdFieldMapper INSTANCE = new TimeSeriesIdFieldMapper();
49+
50+
private static final TimeSeriesIdFieldMapper INSTANCE_WITHOUT_SKIPPER = new TimeSeriesIdFieldMapper(false);
51+
private static final TimeSeriesIdFieldMapper INSTANCE_WITH_SKIPPER = new TimeSeriesIdFieldMapper(true);
52+
53+
public static TimeSeriesIdFieldMapper getInstance(boolean useDocValuesSkipper) {
54+
if (useDocValuesSkipper) {
55+
return INSTANCE_WITH_SKIPPER;
56+
} else {
57+
return INSTANCE_WITHOUT_SKIPPER;
58+
}
59+
}
60+
61+
public static TimeSeriesIdFieldMapper getInstance(MappingParserContext context) {
62+
boolean useDocValuesSkipper = context.indexVersionCreated().onOrAfter(IndexVersions.TIME_SERIES_ID_DOC_VALUES_SPARSE_INDEX)
63+
&& context.getIndexSettings().useDocValuesSkipper();
64+
return TimeSeriesIdFieldMapper.getInstance(useDocValuesSkipper);
65+
}
5066

5167
@Override
5268
public FieldMapper.Builder getMergeBuilder() {
53-
return new Builder().init(this);
69+
return new Builder(this.useDocValuesSkipper).init(this);
5470
}
5571

5672
public static class Builder extends MetadataFieldMapper.Builder {
5773

58-
protected Builder() {
74+
private final boolean useDocValuesSkipper;
75+
76+
protected Builder(boolean useDocValuesSkipper) {
5977
super(NAME);
78+
this.useDocValuesSkipper = useDocValuesSkipper;
6079
}
6180

6281
@Override
@@ -66,11 +85,11 @@ protected Parameter<?>[] getParameters() {
6685

6786
@Override
6887
public TimeSeriesIdFieldMapper build() {
69-
return INSTANCE;
88+
return TimeSeriesIdFieldMapper.getInstance(useDocValuesSkipper);
7089
}
7190
}
7291

73-
public static final TypeParser PARSER = new FixedTypeParser(c -> c.getIndexSettings().getMode().timeSeriesIdFieldMapper());
92+
public static final TypeParser PARSER = new FixedTypeParser(c -> c.getIndexSettings().getMode().timeSeriesIdFieldMapper(c));
7493

7594
public static final class TimeSeriesIdFieldType extends MappedFieldType {
7695
private TimeSeriesIdFieldType() {
@@ -115,8 +134,11 @@ public Query termQuery(Object value, SearchExecutionContext context) {
115134
}
116135
}
117136

118-
private TimeSeriesIdFieldMapper() {
137+
private final boolean useDocValuesSkipper;
138+
139+
private TimeSeriesIdFieldMapper(boolean useDocValuesSkipper) {
119140
super(FIELD_TYPE);
141+
this.useDocValuesSkipper = useDocValuesSkipper;
120142
}
121143

122144
@Override
@@ -135,7 +157,12 @@ public void postParse(DocumentParserContext context) throws IOException {
135157
} else {
136158
timeSeriesId = routingPathFields.buildHash().toBytesRef();
137159
}
138-
context.doc().add(new SortedDocValuesField(fieldType().name(), timeSeriesId));
160+
161+
if (this.useDocValuesSkipper) {
162+
context.doc().add(SortedDocValuesField.indexedField(fieldType().name(), timeSeriesId));
163+
} else {
164+
context.doc().add(new SortedDocValuesField(fieldType().name(), timeSeriesId));
165+
}
139166

140167
BytesRef uidEncoded = TsidExtractingIdFieldMapper.createField(
141168
context,

0 commit comments

Comments
 (0)