Skip to content

Commit 2286562

Browse files
committed
generalize some code
1 parent 62a3b50 commit 2286562

File tree

3 files changed

+57
-49
lines changed

3 files changed

+57
-49
lines changed

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import org.apache.lucene.util.BitUtil;
1414
import org.elasticsearch.common.io.stream.BytesStreamOutput;
1515
import org.elasticsearch.common.io.stream.StreamInput;
16+
import org.elasticsearch.index.IndexVersion;
17+
import org.elasticsearch.index.IndexVersions;
1618

1719
import java.io.IOException;
1820
import java.util.ArrayList;
@@ -23,7 +25,7 @@
2325

2426
public class FieldArrayContext {
2527

26-
public static final String OFFSETS_FIELD_NAME_SUFFIX = ".offsets";
28+
private static final String OFFSETS_FIELD_NAME_SUFFIX = ".offsets";
2729
private final Map<String, Offsets> offsetsPerField = new HashMap<>();
2830

2931
void recordOffset(String field, Comparable<?> value) {
@@ -80,6 +82,42 @@ static int[] parseOffsetArray(StreamInput in) throws IOException {
8082
return offsetToOrd;
8183
}
8284

85+
static String getOffsetsFieldName(
86+
MapperBuilderContext context,
87+
Mapper.SourceKeepMode indexSourceKeepMode,
88+
boolean hasDocValues,
89+
boolean isStored,
90+
IndexVersion indexCreatedVersion,
91+
FieldMapper.Builder fieldMapperBuilder
92+
) {
93+
var sourceKeepMode = fieldMapperBuilder.sourceKeepMode.orElse(indexSourceKeepMode);
94+
if (context.isSourceSynthetic()
95+
&& sourceKeepMode == Mapper.SourceKeepMode.ARRAYS
96+
&& hasDocValues
97+
&& isStored == false
98+
&& fieldMapperBuilder.copyTo.copyToFields().isEmpty()
99+
&& fieldMapperBuilder.multiFieldsBuilder.hasMultiFields() == false
100+
&& indexVersionSupportStoringArraysNatively(indexCreatedVersion)) {
101+
// Skip stored, we will be synthesizing from stored fields, no point to keep track of the offsets
102+
// Skip copy_to and multi fields, supporting that requires more work. However, copy_to usage is rare in metrics and
103+
// logging use cases
104+
105+
// keep track of value offsets so that we can reconstruct arrays from doc values in order as was specified during indexing
106+
// (if field is stored then there is no point of doing this)
107+
return context.buildFullName(fieldMapperBuilder.leafName() + FieldArrayContext.OFFSETS_FIELD_NAME_SUFFIX);
108+
} else {
109+
return null;
110+
}
111+
}
112+
113+
private static boolean indexVersionSupportStoringArraysNatively(IndexVersion indexCreatedVersion) {
114+
return indexCreatedVersion.onOrAfter(IndexVersions.SYNTHETIC_SOURCE_STORE_ARRAYS_NATIVELY_KEYWORD)
115+
|| indexCreatedVersion.between(
116+
IndexVersions.SYNTHETIC_SOURCE_STORE_ARRAYS_NATIVELY_KEYWORD_BACKPORT_8_X,
117+
IndexVersions.UPGRADE_TO_LUCENE_10_0_0
118+
);
119+
}
120+
83121
private static class Offsets {
84122

85123
int currentOffset;

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

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.util.Objects;
5656
import java.util.function.BiFunction;
5757

58+
import static org.elasticsearch.index.mapper.FieldArrayContext.getOffsetsFieldName;
5859
import static org.elasticsearch.index.mapper.IpPrefixAutomatonUtil.buildIpPrefixAutomaton;
5960

6061
/**
@@ -193,26 +194,14 @@ public IpFieldMapper build(MapperBuilderContext context) {
193194
hasScript = script.get() != null;
194195
onScriptError = onScriptErrorParam.getValue();
195196

196-
var sourceKeepMode = this.sourceKeepMode.orElse(indexSourceKeepMode);
197-
String offsetsFieldName;
198-
if (context.isSourceSynthetic()
199-
&& sourceKeepMode == SourceKeepMode.ARRAYS
200-
&& hasDocValues.get()
201-
&& stored.get() == false
202-
&& copyTo.copyToFields().isEmpty()
203-
&& multiFieldsBuilder.hasMultiFields() == false
204-
&& indexCreatedVersion.onOrAfter(IndexVersions.SYNTHETIC_SOURCE_STORE_ARRAYS_NATIVELY_KEYWORD)) {
205-
// Skip stored, we will be synthesizing from stored fields, no point to keep track of the offsets
206-
// Skip copy_to and multi fields, supporting that requires more work. However, copy_to usage is rare in metrics and
207-
// logging use cases
208-
209-
// keep track of value offsets so that we can reconstruct arrays from doc values in order as was specified during indexing
210-
// (if field is stored then there is no point of doing this)
211-
offsetsFieldName = context.buildFullName(leafName() + FieldArrayContext.OFFSETS_FIELD_NAME_SUFFIX);
212-
} else {
213-
offsetsFieldName = null;
214-
}
215-
197+
String offsetsFieldName = getOffsetsFieldName(
198+
context,
199+
indexSourceKeepMode,
200+
hasDocValues.getValue(),
201+
stored.getValue(),
202+
indexCreatedVersion,
203+
this
204+
);
216205
return new IpFieldMapper(
217206
leafName(),
218207
new IpFieldType(

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

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import static org.elasticsearch.core.Strings.format;
8686
import static org.elasticsearch.index.IndexSettings.IGNORE_ABOVE_SETTING;
8787
import static org.elasticsearch.index.IndexSettings.USE_DOC_VALUES_SKIPPER;
88+
import static org.elasticsearch.index.mapper.FieldArrayContext.getOffsetsFieldName;
8889

8990
/**
9091
* A field mapper for keywords. This mapper accepts strings and indexes them as-is.
@@ -438,26 +439,14 @@ public KeywordFieldMapper build(MapperBuilderContext context) {
438439
super.hasScript = script.get() != null;
439440
super.onScriptError = onScriptError.getValue();
440441

441-
var sourceKeepMode = this.sourceKeepMode.orElse(indexSourceKeepMode);
442-
String offsetsFieldName;
443-
if (context.isSourceSynthetic()
444-
&& sourceKeepMode == SourceKeepMode.ARRAYS
445-
&& hasDocValues()
446-
&& fieldtype.stored() == false
447-
&& copyTo.copyToFields().isEmpty()
448-
&& multiFieldsBuilder.hasMultiFields() == false
449-
&& indexVersionSupportStoringArraysNatively()) {
450-
// Skip stored, we will be synthesizing from stored fields, no point to keep track of the offsets
451-
// Skip copy_to and multi fields, supporting that requires more work. However, copy_to usage is rare in metrics and
452-
// logging use cases
453-
454-
// keep track of value offsets so that we can reconstruct arrays from doc values in order as was specified during indexing
455-
// (if field is stored then there is no point of doing this)
456-
offsetsFieldName = context.buildFullName(leafName() + FieldArrayContext.OFFSETS_FIELD_NAME_SUFFIX);
457-
} else {
458-
offsetsFieldName = null;
459-
}
460-
442+
String offsetsFieldName = getOffsetsFieldName(
443+
context,
444+
indexSourceKeepMode,
445+
hasDocValues.getValue(),
446+
stored.getValue(),
447+
indexCreatedVersion,
448+
this
449+
);
461450
return new KeywordFieldMapper(
462451
leafName(),
463452
fieldtype,
@@ -471,14 +460,6 @@ && indexVersionSupportStoringArraysNatively()) {
471460
);
472461
}
473462

474-
private boolean indexVersionSupportStoringArraysNatively() {
475-
return indexCreatedVersion.onOrAfter(IndexVersions.SYNTHETIC_SOURCE_STORE_ARRAYS_NATIVELY_KEYWORD)
476-
|| indexCreatedVersion.between(
477-
IndexVersions.SYNTHETIC_SOURCE_STORE_ARRAYS_NATIVELY_KEYWORD_BACKPORT_8_X,
478-
IndexVersions.UPGRADE_TO_LUCENE_10_0_0
479-
);
480-
}
481-
482463
private FieldType resolveFieldType(
483464
final boolean useDocValuesSkipper,
484465
final IndexVersion indexCreatedVersion,

0 commit comments

Comments
 (0)