|
@@ -43,6 +43,7 @@ import org.elasticsearch.search.DocValueFormat;
|
|
|
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
|
|
|
import org.elasticsearch.search.lookup.FieldValues;
|
|
|
import org.elasticsearch.search.lookup.SearchLookup;
|
|
|
+import org.elasticsearch.xcontent.XContentParser;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.net.InetAddress;
|
|
@@ -51,8 +52,10 @@ import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Collection;
|
|
|
import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
+import java.util.Set;
|
|
|
import java.util.function.BiFunction;
|
|
|
|
|
|
import static org.elasticsearch.index.mapper.FieldArrayContext.getOffsetsFieldName;
|
|
@@ -213,7 +216,8 @@ public class IpFieldMapper extends FieldMapper {
|
|
|
parseNullValue(),
|
|
|
scriptValues(),
|
|
|
meta.getValue(),
|
|
|
- dimension.getValue()
|
|
|
+ dimension.getValue(),
|
|
|
+ context.isSourceSynthetic()
|
|
|
),
|
|
|
builderParams(this, context),
|
|
|
context.isSourceSynthetic(),
|
|
@@ -234,6 +238,7 @@ public class IpFieldMapper extends FieldMapper {
|
|
|
private final InetAddress nullValue;
|
|
|
private final FieldValues<InetAddress> scriptValues;
|
|
|
private final boolean isDimension;
|
|
|
+ private final boolean isSyntheticSource;
|
|
|
|
|
|
public IpFieldType(
|
|
|
String name,
|
|
@@ -243,12 +248,14 @@ public class IpFieldMapper extends FieldMapper {
|
|
|
InetAddress nullValue,
|
|
|
FieldValues<InetAddress> scriptValues,
|
|
|
Map<String, String> meta,
|
|
|
- boolean isDimension
|
|
|
+ boolean isDimension,
|
|
|
+ boolean isSyntheticSource
|
|
|
) {
|
|
|
super(name, indexed, stored, hasDocValues, TextSearchInfo.SIMPLE_MATCH_WITHOUT_TERMS, meta);
|
|
|
this.nullValue = nullValue;
|
|
|
this.scriptValues = scriptValues;
|
|
|
this.isDimension = isDimension;
|
|
|
+ this.isSyntheticSource = isSyntheticSource;
|
|
|
}
|
|
|
|
|
|
public IpFieldType(String name) {
|
|
@@ -260,7 +267,7 @@ public class IpFieldMapper extends FieldMapper {
|
|
|
}
|
|
|
|
|
|
public IpFieldType(String name, boolean isIndexed, boolean hasDocValues) {
|
|
|
- this(name, isIndexed, false, hasDocValues, null, null, Collections.emptyMap(), false);
|
|
|
+ this(name, isIndexed, false, hasDocValues, null, null, Collections.emptyMap(), false, false);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -452,10 +459,79 @@ public class IpFieldMapper extends FieldMapper {
|
|
|
|
|
|
@Override
|
|
|
public BlockLoader blockLoader(BlockLoaderContext blContext) {
|
|
|
- if (hasDocValues()) {
|
|
|
+ if (hasDocValues() && (blContext.fieldExtractPreference() != FieldExtractPreference.STORED || isSyntheticSource)) {
|
|
|
return new BlockDocValuesReader.BytesRefsFromOrdsBlockLoader(name());
|
|
|
}
|
|
|
- return null;
|
|
|
+
|
|
|
+ if (isStored()) {
|
|
|
+ return new BlockStoredFieldsReader.BytesFromBytesRefsBlockLoader(name());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isSyntheticSource) {
|
|
|
+ return blockLoaderFromFallbackSyntheticSource(blContext);
|
|
|
+ }
|
|
|
+
|
|
|
+ // see #indexValue
|
|
|
+ BlockSourceReader.LeafIteratorLookup lookup = hasDocValues() == false && isIndexed()
|
|
|
+ ? BlockSourceReader.lookupFromFieldNames(blContext.fieldNames(), name())
|
|
|
+ : BlockSourceReader.lookupMatchingAll();
|
|
|
+ return new BlockSourceReader.IpsBlockLoader(sourceValueFetcher(blContext.sourcePaths(name())), lookup);
|
|
|
+ }
|
|
|
+
|
|
|
+ private BlockLoader blockLoaderFromFallbackSyntheticSource(BlockLoaderContext blContext) {
|
|
|
+ var reader = new FallbackSyntheticSourceBlockLoader.SingleValueReader<InetAddress>(nullValue) {
|
|
|
+ @Override
|
|
|
+ public void convertValue(Object value, List<InetAddress> accumulator) {
|
|
|
+ if (value instanceof InetAddress ia) {
|
|
|
+ accumulator.add(ia);
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ var address = InetAddresses.forString(value.toString());
|
|
|
+ accumulator.add(address);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // Malformed value, skip it
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void parseNonNullValue(XContentParser parser, List<InetAddress> accumulator) throws IOException {
|
|
|
+ // aligned with #parseCreateField()
|
|
|
+ String value = parser.text();
|
|
|
+
|
|
|
+ try {
|
|
|
+ var address = InetAddresses.forString(value);
|
|
|
+ accumulator.add(address);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // Malformed value, skip it
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void writeToBlock(List<InetAddress> values, BlockLoader.Builder blockBuilder) {
|
|
|
+ var bytesRefBuilder = (BlockLoader.BytesRefBuilder) blockBuilder;
|
|
|
+
|
|
|
+ for (var value : values) {
|
|
|
+ bytesRefBuilder.appendBytesRef(new BytesRef(InetAddressPoint.encode(value)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ return new FallbackSyntheticSourceBlockLoader(reader, name()) {
|
|
|
+ @Override
|
|
|
+ public Builder builder(BlockFactory factory, int expectedCount) {
|
|
|
+ return factory.bytesRefs(expectedCount);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ private SourceValueFetcher sourceValueFetcher(Set<String> sourcePaths) {
|
|
|
+ return new SourceValueFetcher(sourcePaths, nullValue) {
|
|
|
+ @Override
|
|
|
+ public InetAddress parseSourceValue(Object value) {
|
|
|
+ return parse(value);
|
|
|
+ }
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
@Override
|