Skip to content

Commit

Permalink
Fix DerivedFieldMapperQueryTests
Browse files Browse the repository at this point in the history
Needed to add doc values to date and IP fields.

Signed-off-by: Michael Froh <[email protected]>
  • Loading branch information
msfroh committed Dec 18, 2024
1 parent b2ac498 commit 59e7d03
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.FloatField;
import org.apache.lucene.document.InetAddressPoint;
import org.apache.lucene.document.KeywordField;
import org.apache.lucene.document.LatLonPoint;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.IndexableField;
Expand Down Expand Up @@ -70,7 +68,7 @@ public enum DerivedFieldSupportedTypes {
);
return builder.build(context);
},
name -> o -> new LongPoint(name, (long) o),
name -> o -> new LongField(name, (long) o, Field.Store.NO),
formatter -> o -> formatter == null
? DateFieldMapper.getDefaultDateTimeFormatter().formatMillis((long) o)
: formatter.formatMillis((long) o)
Expand All @@ -95,7 +93,7 @@ public enum DerivedFieldSupportedTypes {
} else {
address = InetAddresses.forString(o.toString());
}
return new InetAddressPoint(name, address);
return IpFieldMapper.buildInetAddressField(name, address);
}, formatter -> o -> o),
KEYWORD("keyword", (name, context, indexAnalyzers) -> {
FieldType dummyFieldType = new FieldType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@

package org.opensearch.index.mapper;

import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.InetAddressPoint;
import org.apache.lucene.document.SortedSetDocValuesField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.index.DocValuesType;
import org.apache.lucene.index.SortedSetDocValues;
import org.apache.lucene.sandbox.search.MultiRangeQuery;
import org.apache.lucene.search.BooleanClause;
Expand Down Expand Up @@ -607,19 +610,40 @@ protected void parseCreateField(ParseContext context) throws IOException {
}
}

if (indexed) {
if (indexed && hasDocValues) {
context.doc().add(new InetAddressField(fieldType().name(), address));
} else if (indexed) {
context.doc().add(new InetAddressPoint(fieldType().name(), address));
}
if (hasDocValues) {
} else if (hasDocValues) {
context.doc().add(new SortedSetDocValuesField(fieldType().name(), new BytesRef(InetAddressPoint.encode(address))));
} else if (stored || indexed) {
}
if ((stored || indexed) && hasDocValues == false) {
createFieldNamesField(context);
}
if (stored) {
context.doc().add(new StoredField(fieldType().name(), new BytesRef(InetAddressPoint.encode(address))));
}
}

public static InetAddressField buildInetAddressField(String name, InetAddress value) {
return new InetAddressField(name, value);
}

// Field type that combines dimensional points and
private static class InetAddressField extends Field {
private static FieldType FIELD_TYPE = new FieldType();
static {
FIELD_TYPE.setDimensions(1, InetAddressPoint.BYTES);
FIELD_TYPE.setDocValuesType(DocValuesType.SORTED_SET);
FIELD_TYPE.freeze();
}

public InetAddressField(String name, InetAddress value) {
super(name, FIELD_TYPE);
fieldsData = new BytesRef(InetAddressPoint.encode(value));
}
}

@Override
public ParametrizedFieldMapper.Builder getMergeBuilder() {
return new Builder(simpleName(), ignoreMalformedByDefault, indexCreatedVersion).init(this);
Expand Down

0 comments on commit 59e7d03

Please sign in to comment.