|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.index.mapper; |
| 10 | + |
| 11 | +import org.apache.lucene.search.MatchAllDocsQuery; |
| 12 | +import org.apache.lucene.search.Query; |
| 13 | +import org.opensearch.OpenSearchParseException; |
| 14 | +import org.opensearch.common.annotation.PublicApi; |
| 15 | +import org.opensearch.common.regex.Regex; |
| 16 | +import org.opensearch.index.fielddata.IndexFieldData; |
| 17 | +import org.opensearch.index.fielddata.plain.ConstantIndexFieldData; |
| 18 | +import org.opensearch.index.query.QueryShardContext; |
| 19 | +import org.opensearch.search.aggregations.support.CoreValuesSourceType; |
| 20 | +import org.opensearch.search.lookup.SearchLookup; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.function.Supplier; |
| 28 | + |
| 29 | +/** |
| 30 | + * Index specific field mapper |
| 31 | + * |
| 32 | + * @opensearch.api |
| 33 | + */ |
| 34 | +@PublicApi(since = "2.14.0") |
| 35 | +public class ConstantKeywordFieldMapper extends ParametrizedFieldMapper { |
| 36 | + |
| 37 | + public static final String CONTENT_TYPE = "constant_keyword"; |
| 38 | + |
| 39 | + private static final String valuePropertyName = "value"; |
| 40 | + |
| 41 | + /** |
| 42 | + * A {@link Mapper.TypeParser} for the constant keyword field. |
| 43 | + * |
| 44 | + * @opensearch.internal |
| 45 | + */ |
| 46 | + public static class TypeParser implements Mapper.TypeParser { |
| 47 | + @Override |
| 48 | + public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException { |
| 49 | + if (!node.containsKey(valuePropertyName)) { |
| 50 | + throw new OpenSearchParseException("Field [" + name + "] is missing required parameter [value]"); |
| 51 | + } |
| 52 | + Object value = node.remove(valuePropertyName); |
| 53 | + if (!(value instanceof String)) { |
| 54 | + throw new OpenSearchParseException("Field [" + name + "] is expected to be a string value"); |
| 55 | + } |
| 56 | + return new Builder(name, (String) value); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private static ConstantKeywordFieldMapper toType(FieldMapper in) { |
| 61 | + return (ConstantKeywordFieldMapper) in; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Builder for the binary field mapper |
| 66 | + * |
| 67 | + * @opensearch.internal |
| 68 | + */ |
| 69 | + public static class Builder extends ParametrizedFieldMapper.Builder { |
| 70 | + |
| 71 | + private final Parameter<String> value; |
| 72 | + |
| 73 | + public Builder(String name, String value) { |
| 74 | + super(name); |
| 75 | + this.value = Parameter.stringParam(valuePropertyName, false, m -> toType(m).value, value); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public List<Parameter<?>> getParameters() { |
| 80 | + return Arrays.asList(value); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public ConstantKeywordFieldMapper build(BuilderContext context) { |
| 85 | + return new ConstantKeywordFieldMapper( |
| 86 | + name, |
| 87 | + new ConstantKeywordFieldMapper.ConstantKeywordFieldType(buildFullName(context), value.getValue()), |
| 88 | + multiFieldsBuilder.build(this, context), |
| 89 | + copyTo.build(), |
| 90 | + this |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Field type for Index field mapper |
| 97 | + * |
| 98 | + * @opensearch.internal |
| 99 | + */ |
| 100 | + @PublicApi(since = "2.14.0") |
| 101 | + protected static final class ConstantKeywordFieldType extends ConstantFieldType { |
| 102 | + |
| 103 | + protected final String value; |
| 104 | + |
| 105 | + public ConstantKeywordFieldType(String name, String value) { |
| 106 | + super(name, Collections.emptyMap()); |
| 107 | + this.value = value; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String typeName() { |
| 112 | + return CONTENT_TYPE; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + protected boolean matches(String pattern, boolean caseInsensitive, QueryShardContext context) { |
| 117 | + return Regex.simpleMatch(pattern, value, caseInsensitive); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public Query existsQuery(QueryShardContext context) { |
| 122 | + return new MatchAllDocsQuery(); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, Supplier<SearchLookup> searchLookup) { |
| 127 | + return new ConstantIndexFieldData.Builder(fullyQualifiedIndexName, name(), CoreValuesSourceType.BYTES); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { |
| 132 | + if (format != null) { |
| 133 | + throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't " + "support formats."); |
| 134 | + } |
| 135 | + |
| 136 | + return new SourceValueFetcher(name(), context) { |
| 137 | + @Override |
| 138 | + protected Object parseSourceValue(Object value) { |
| 139 | + String keywordValue = value.toString(); |
| 140 | + return Collections.singletonList(keywordValue); |
| 141 | + } |
| 142 | + }; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private final String value; |
| 147 | + |
| 148 | + protected ConstantKeywordFieldMapper( |
| 149 | + String simpleName, |
| 150 | + MappedFieldType mappedFieldType, |
| 151 | + MultiFields multiFields, |
| 152 | + CopyTo copyTo, |
| 153 | + ConstantKeywordFieldMapper.Builder builder |
| 154 | + ) { |
| 155 | + super(simpleName, mappedFieldType, multiFields, copyTo); |
| 156 | + this.value = builder.value.getValue(); |
| 157 | + } |
| 158 | + |
| 159 | + public ParametrizedFieldMapper.Builder getMergeBuilder() { |
| 160 | + return new ConstantKeywordFieldMapper.Builder(simpleName(), this.value).init(this); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + protected void parseCreateField(ParseContext context) throws IOException { |
| 165 | + |
| 166 | + final String value; |
| 167 | + if (context.externalValueSet()) { |
| 168 | + value = context.externalValue().toString(); |
| 169 | + } else { |
| 170 | + value = context.parser().textOrNull(); |
| 171 | + } |
| 172 | + if (value == null) { |
| 173 | + throw new IllegalArgumentException("constant keyword field [" + name() + "] must have a value"); |
| 174 | + } |
| 175 | + |
| 176 | + if (!value.equals(fieldType().value)) { |
| 177 | + throw new IllegalArgumentException("constant keyword field [" + name() + "] must have a value of [" + this.value + "]"); |
| 178 | + } |
| 179 | + |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public ConstantKeywordFieldMapper.ConstantKeywordFieldType fieldType() { |
| 184 | + return (ConstantKeywordFieldMapper.ConstantKeywordFieldType) super.fieldType(); |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + protected String contentType() { |
| 189 | + return CONTENT_TYPE; |
| 190 | + } |
| 191 | +} |
0 commit comments