|
28 | 28 | import org.opensearch.common.unit.Fuzziness;
|
29 | 29 | import org.opensearch.common.xcontent.JsonToStringXContentParser;
|
30 | 30 | import org.opensearch.core.common.ParsingException;
|
| 31 | +import org.opensearch.core.common.io.stream.StreamOutput; |
31 | 32 | import org.opensearch.core.xcontent.DeprecationHandler;
|
32 | 33 | import org.opensearch.core.xcontent.NamedXContentRegistry;
|
33 | 34 | import org.opensearch.core.xcontent.XContentParser;
|
|
36 | 37 | import org.opensearch.index.fielddata.plain.SortedSetOrdinalsIndexFieldData;
|
37 | 38 | import org.opensearch.index.mapper.KeywordFieldMapper.KeywordFieldType;
|
38 | 39 | import org.opensearch.index.query.QueryShardContext;
|
| 40 | +import org.opensearch.search.DocValueFormat; |
39 | 41 | import org.opensearch.search.aggregations.support.CoreValuesSourceType;
|
40 | 42 | import org.opensearch.search.lookup.SearchLookup;
|
41 | 43 |
|
42 | 44 | import java.io.IOException;
|
43 | 45 | import java.io.UncheckedIOException;
|
| 46 | +import java.time.ZoneId; |
44 | 47 | import java.util.ArrayList;
|
45 | 48 | import java.util.Collections;
|
46 | 49 | import java.util.Iterator;
|
|
63 | 66 | public final class FlatObjectFieldMapper extends DynamicKeyFieldMapper {
|
64 | 67 |
|
65 | 68 | public static final String CONTENT_TYPE = "flat_object";
|
| 69 | + public static final String NO_VALUE = new String(""); |
66 | 70 |
|
67 | 71 | /**
|
68 | 72 | * In flat_object field mapper, field type is similar to keyword field type
|
@@ -272,7 +276,7 @@ NamedAnalyzer normalizer() {
|
272 | 276 | @Override
|
273 | 277 | public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, Supplier<SearchLookup> searchLookup) {
|
274 | 278 | failIfNoDocValues();
|
275 |
| - return new SortedSetOrdinalsIndexFieldData.Builder(name(), CoreValuesSourceType.BYTES); |
| 279 | + return new SortedSetOrdinalsIndexFieldData.Builder(valueFieldType().name(), CoreValuesSourceType.BYTES); |
276 | 280 | }
|
277 | 281 |
|
278 | 282 | @Override
|
@@ -304,6 +308,30 @@ protected String parseSourceValue(Object value) {
|
304 | 308 | };
|
305 | 309 | }
|
306 | 310 |
|
| 311 | + @Override |
| 312 | + public DocValueFormat docValueFormat(@Nullable String format, ZoneId timeZone) { |
| 313 | + if (format != null) { |
| 314 | + throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom formats"); |
| 315 | + } |
| 316 | + if (timeZone != null) { |
| 317 | + throw new IllegalArgumentException( |
| 318 | + "Field [" + name() + "] of type [" + typeName() + "] does not support custom time zones" |
| 319 | + ); |
| 320 | + } |
| 321 | + if (mappedFieldTypeName != null) { |
| 322 | + return new FlatObjectDocValueFormat(mappedFieldTypeName + DOT_SYMBOL + name() + EQUAL_SYMBOL); |
| 323 | + } else { |
| 324 | + throw new IllegalArgumentException( |
| 325 | + "Field [" + name() + "] of type [" + typeName() + "] does not support doc_value in root field" |
| 326 | + ); |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + @Override |
| 331 | + public boolean isAggregatable() { |
| 332 | + return false; |
| 333 | + } |
| 334 | + |
307 | 335 | @Override
|
308 | 336 | public Object valueForDisplay(Object value) {
|
309 | 337 | if (value == null) {
|
@@ -530,6 +558,36 @@ public Query wildcardQuery(
|
530 | 558 | return valueFieldType().wildcardQuery(rewriteValue(value), method, caseInsensitve, context);
|
531 | 559 | }
|
532 | 560 |
|
| 561 | + public class FlatObjectDocValueFormat implements DocValueFormat { |
| 562 | + private static final String NAME = "flat_object"; |
| 563 | + private final String prefix; |
| 564 | + |
| 565 | + public FlatObjectDocValueFormat(String prefix) { |
| 566 | + this.prefix = prefix; |
| 567 | + } |
| 568 | + |
| 569 | + @Override |
| 570 | + public String getWriteableName() { |
| 571 | + return NAME; |
| 572 | + } |
| 573 | + |
| 574 | + @Override |
| 575 | + public void writeTo(StreamOutput out) {} |
| 576 | + |
| 577 | + @Override |
| 578 | + public String format(BytesRef value) { |
| 579 | + String parsedValue = inputToString(value); |
| 580 | + if (parsedValue.startsWith(prefix) == false) { |
| 581 | + return NO_VALUE; |
| 582 | + } |
| 583 | + return parsedValue.substring(prefix.length()); |
| 584 | + } |
| 585 | + |
| 586 | + @Override |
| 587 | + public BytesRef parseBytesRef(String value) { |
| 588 | + return new BytesRef((String) valueFieldType.rewriteForDocValue(rewriteValue(value))); |
| 589 | + } |
| 590 | + } |
533 | 591 | }
|
534 | 592 |
|
535 | 593 | private final ValueFieldMapper valueFieldMapper;
|
|
0 commit comments