From 8f863dc5aad8f84e5315c2c58dac8f2674d1c7ed Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Sun, 11 Feb 2024 12:06:46 +0530 Subject: [PATCH 01/32] feat: constant keyword field Signed-off-by: Mohammad Hasnain --- .../index/mapper/ConstantFieldType.java | 8 +- .../mapper/ConstantKeywordFieldMapper.java | 240 ++++++++++++++++++ .../index/mapper/MappedFieldType.java | 2 +- .../org/opensearch/indices/IndicesModule.java | 2 + .../mapper/ConstantKeywordFieldTypeTests.java | 106 ++++++++ 5 files changed, 353 insertions(+), 5 deletions(-) create mode 100644 server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java create mode 100644 server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java diff --git a/server/src/main/java/org/opensearch/index/mapper/ConstantFieldType.java b/server/src/main/java/org/opensearch/index/mapper/ConstantFieldType.java index a28a6369b1aa4..d23ab53116529 100644 --- a/server/src/main/java/org/opensearch/index/mapper/ConstantFieldType.java +++ b/server/src/main/java/org/opensearch/index/mapper/ConstantFieldType.java @@ -91,7 +91,7 @@ public final Query termQuery(Object value, QueryShardContext context) { } @Override - public final Query termQueryCaseInsensitive(Object value, QueryShardContext context) { + public Query termQueryCaseInsensitive(Object value, QueryShardContext context) { String pattern = valueToString(value); if (matches(pattern, true, context)) { return Queries.newMatchAllQuery(); @@ -101,7 +101,7 @@ public final Query termQueryCaseInsensitive(Object value, QueryShardContext cont } @Override - public final Query termsQuery(List values, QueryShardContext context) { + public Query termsQuery(List values, QueryShardContext context) { for (Object value : values) { String pattern = valueToString(value); if (matches(pattern, false, context)) { @@ -113,7 +113,7 @@ public final Query termsQuery(List values, QueryShardContext context) { } @Override - public final Query prefixQuery( + public Query prefixQuery( String prefix, @Nullable MultiTermQuery.RewriteMethod method, boolean caseInsensitive, @@ -128,7 +128,7 @@ public final Query prefixQuery( } @Override - public final Query wildcardQuery( + public Query wildcardQuery( String value, @Nullable MultiTermQuery.RewriteMethod method, boolean caseInsensitive, diff --git a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java new file mode 100644 index 0000000000000..aab2ab6e52e31 --- /dev/null +++ b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java @@ -0,0 +1,240 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +package org.opensearch.index.mapper; + +import org.apache.lucene.search.MatchAllDocsQuery; +import org.apache.lucene.search.MultiTermQuery; +import org.apache.lucene.search.Query; +import org.opensearch.OpenSearchParseException; +import org.opensearch.common.Explicit; +import org.opensearch.common.Nullable; +import org.opensearch.common.annotation.PublicApi; +import org.opensearch.common.xcontent.support.XContentMapValues; +import org.opensearch.core.common.Strings; +import org.opensearch.index.fielddata.IndexFieldData; +import org.opensearch.index.fielddata.plain.ConstantIndexFieldData; +import org.opensearch.index.query.QueryShardContext; +import org.opensearch.index.query.QueryShardException; +import org.opensearch.search.aggregations.support.CoreValuesSourceType; +import org.opensearch.search.lookup.SearchLookup; + +import java.io.IOException; +import java.util.*; +import java.util.function.Supplier; + +/** + * Index specific field mapper + * + * @opensearch.api + */ +@PublicApi(since = "1.0.0") +public class ConstantKeywordFieldMapper extends ParametrizedFieldMapper { + +// public static final String NAME = "_index"; + + public static final String CONTENT_TYPE = "constant_keyword"; + +// public static final TypeParser PARSER = new TypeParser((n, c) -> new ConstantKeywordFieldMapper.Builder(n, c)); + + public static class TypeParser implements Mapper.TypeParser { + @Override + public Mapper.Builder parse(String name, Map node, ParserContext parserContext) throws MapperParsingException { + if (!node.containsKey("value")) { + throw new OpenSearchParseException("Field [" + name + "] is missing required parameter [value]"); + } + Object value = node.get("value"); + if (!(value instanceof String)) { + throw new OpenSearchParseException("Field [" + name + "] is expected to be a string value"); + } + return new Builder(name, (String) value); + } + } + + + private static ConstantKeywordFieldMapper toType(FieldMapper in) { + return (ConstantKeywordFieldMapper) in; + } + + /** + * Builder for the binary field mapper + * + * @opensearch.internal + */ + public static class Builder extends ParametrizedFieldMapper.Builder { + + private final Parameter value; + + public Builder(String name, String value) { + super(name); + this.value = Parameter.stringParam("value", false, m -> toType(m).value, value); + } + + @Override + public List> getParameters() { + return Arrays.asList(value); + } + + @Override + public ConstantKeywordFieldMapper build(BuilderContext context) { + return new ConstantKeywordFieldMapper( + name, + new ConstantKeywordFieldMapper.ConstantKeywordFieldType(buildFullName(context), value.getValue()), + multiFieldsBuilder.build(this, context), + copyTo.build(), + this + ); + } + } + + /** + * Field type for Index field mapper + * + * @opensearch.internal + */ + static final class ConstantKeywordFieldType extends ConstantFieldType { + + protected final String value; + +// static final ConstantKeywordFieldType INSTANCE = new ConstantKeywordFieldType(); + + public ConstantKeywordFieldType(String name, String value) { + super(name, Collections.emptyMap()); + this.value = value; + } + + @Override + public String typeName() { + return CONTENT_TYPE; + } + + @Override + protected boolean matches(String searchValue, boolean caseInsensitive, QueryShardContext context) { + System.out.println(value); + + return value.equals(searchValue); + } + + @Override + public Query existsQuery(QueryShardContext context) { + return new MatchAllDocsQuery(); + } + + public Query termQueryCaseInsensitive(Object value, QueryShardContext context) { + throw new QueryShardException( + context, + "Fields of type [" + typeName() + "], does not support case insensitive term queries" + ); + } + + public Query prefixQuery(Object value, QueryShardContext context) { + throw new QueryShardException( + context, + "Fields of type [" + typeName() + "], does not support prefix queries" + ); + } + + public Query wildcardQuery(String value, @Nullable MultiTermQuery.RewriteMethod method, QueryShardContext context) { + throw new QueryShardException( + context, + "Fields of type [" + typeName() + "], does not support wildcard queries" + ); + } + + public Query wildcardQuery( + String value, + @Nullable MultiTermQuery.RewriteMethod method, + boolean caseInsensitve, + QueryShardContext context + ) { + System.out.println(("tes")); + throw new QueryShardException( + context, + "Fields of type [" + typeName() + "], does not support wildcard queries" + ); + } + + @Override + public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, Supplier searchLookup) { + return new ConstantIndexFieldData.Builder(fullyQualifiedIndexName, name(), CoreValuesSourceType.BYTES); + } + + @Override + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { + throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "]."); + } + } + + private final String value; + +// public ConstantKeywordFieldMapper() { +// super(IndexFieldType.INSTANCE); +// } + protected ConstantKeywordFieldMapper( + String simpleName, + MappedFieldType mappedFieldType, + MultiFields multiFields, + CopyTo copyTo, + ConstantKeywordFieldMapper.Builder builder + ) { + super(simpleName, mappedFieldType, multiFields, copyTo); + this.value = builder.value.getValue(); + } + + public ParametrizedFieldMapper.Builder getMergeBuilder() { + return new ConstantKeywordFieldMapper.Builder(simpleName(), this.value).init(this); + } + + @Override + protected void parseCreateField(ParseContext context) throws IOException { + String value = context.parseExternalValue(String.class); + + if (value == null) { + throw new IllegalArgumentException("constant keyword field [" + name() + "] must have a value"); + } + + if (!value.equals(fieldType().value)) { + throw new IllegalArgumentException("constant keyword field [" + name() + "] must have a value of [" + this.value + "]"); + } + + } + + @Override + public ConstantKeywordFieldMapper.ConstantKeywordFieldType fieldType() { + return (ConstantKeywordFieldMapper.ConstantKeywordFieldType) super.fieldType(); + } + + @Override + protected String contentType() { + return CONTENT_TYPE; + } +} diff --git a/server/src/main/java/org/opensearch/index/mapper/MappedFieldType.java b/server/src/main/java/org/opensearch/index/mapper/MappedFieldType.java index 66d4654e543a2..797608b46c890 100644 --- a/server/src/main/java/org/opensearch/index/mapper/MappedFieldType.java +++ b/server/src/main/java/org/opensearch/index/mapper/MappedFieldType.java @@ -304,7 +304,7 @@ public Query prefixQuery( } // Case sensitive form of wildcard query - public final Query wildcardQuery(String value, @Nullable MultiTermQuery.RewriteMethod method, QueryShardContext context) { + public Query wildcardQuery(String value, @Nullable MultiTermQuery.RewriteMethod method, QueryShardContext context) { return wildcardQuery(value, method, false, context); } diff --git a/server/src/main/java/org/opensearch/indices/IndicesModule.java b/server/src/main/java/org/opensearch/indices/IndicesModule.java index b86e98f4ebcbc..dfbc3c558b6e0 100644 --- a/server/src/main/java/org/opensearch/indices/IndicesModule.java +++ b/server/src/main/java/org/opensearch/indices/IndicesModule.java @@ -70,6 +70,7 @@ import org.opensearch.index.mapper.SourceFieldMapper; import org.opensearch.index.mapper.TextFieldMapper; import org.opensearch.index.mapper.VersionFieldMapper; +import org.opensearch.index.mapper.ConstantKeywordFieldMapper; import org.opensearch.index.remote.RemoteStorePressureService; import org.opensearch.index.seqno.GlobalCheckpointSyncAction; import org.opensearch.index.seqno.RetentionLeaseBackgroundSyncAction; @@ -168,6 +169,7 @@ public static Map getMappers(List mappe mappers.put(FieldAliasMapper.CONTENT_TYPE, new FieldAliasMapper.TypeParser()); mappers.put(GeoPointFieldMapper.CONTENT_TYPE, new GeoPointFieldMapper.TypeParser()); mappers.put(FlatObjectFieldMapper.CONTENT_TYPE, FlatObjectFieldMapper.PARSER); + mappers.put(ConstantKeywordFieldMapper.CONTENT_TYPE, new ConstantKeywordFieldMapper.TypeParser()); for (MapperPlugin mapperPlugin : mapperPlugins) { for (Map.Entry entry : mapperPlugin.getMappers().entrySet()) { diff --git a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java new file mode 100644 index 0000000000000..6c645d8850bf8 --- /dev/null +++ b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java @@ -0,0 +1,106 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +package org.opensearch.index.mapper; + +import org.apache.lucene.search.MatchAllDocsQuery; +import org.apache.lucene.search.MatchNoDocsQuery; +import org.opensearch.Version; +import org.opensearch.cluster.metadata.IndexMetadata; +import org.opensearch.common.regex.Regex; +import org.opensearch.common.settings.Settings; +import org.opensearch.index.IndexSettings; +import org.opensearch.index.query.QueryShardContext; +import org.opensearch.index.query.QueryShardException; + +import java.util.function.Predicate; + +import static org.hamcrest.Matchers.containsString; + +public class ConstantKeywordFieldTypeTests extends FieldTypeTestCase { + + public void testPrefixQuery() { + MappedFieldType ft = new ConstantKeywordFieldMapper.ConstantKeywordFieldType("field", "default"); + + assertEquals(new MatchAllDocsQuery(), ft.termQuery("default", createContext())); + assertEquals(new MatchNoDocsQuery(), ft.prefixQuery("not_default", null, createContext())); + } + + public void testWildcardQuery() { + ConstantKeywordFieldMapper.ConstantKeywordFieldType ft = new ConstantKeywordFieldMapper.ConstantKeywordFieldType("field", "default"); + + QueryShardException e = expectThrows( + QueryShardException.class, + () -> assertEquals(new MatchAllDocsQuery(), ft.wildcardQuery("ind*x", null, createContext())) + ); + assertThat(e.getMessage(), containsString("Fields of type [constant_keyword], does not support wildcard queries")); + } + + public void testRegexpQuery() { + MappedFieldType ft = IndexFieldMapper.IndexFieldType.INSTANCE; + + QueryShardException e = expectThrows( + QueryShardException.class, + () -> assertEquals(new MatchAllDocsQuery(), ft.regexpQuery("ind.x", 0, 0, 10, null, createContext())) + ); + assertThat(e.getMessage(), containsString("Can only use regexp queries on keyword and text fields")); + } + + private QueryShardContext createContext() { + IndexMetadata indexMetadata = IndexMetadata.builder("index") + .settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)) + .numberOfShards(1) + .numberOfReplicas(0) + .build(); + IndexSettings indexSettings = new IndexSettings(indexMetadata, Settings.EMPTY); + + Predicate indexNameMatcher = pattern -> Regex.simpleMatch(pattern, "index"); + return new QueryShardContext( + 0, + indexSettings, + null, + null, + null, + null, + null, + null, + xContentRegistry(), + writableRegistry(), + null, + null, + System::currentTimeMillis, + null, + indexNameMatcher, + () -> true, + null + ); + } +} From e279e4234dee5de4f0d305e8ad9c9ebaf457dd1c Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Mon, 12 Feb 2024 13:08:23 +0530 Subject: [PATCH 02/32] feat: constant keyword field mapper tests Signed-off-by: Mohammad Hasnain --- .../ConstantKeywordFieldMapperTests.java | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java diff --git a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java new file mode 100644 index 0000000000000..8c0d1e597b97f --- /dev/null +++ b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java @@ -0,0 +1,111 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +package org.opensearch.index.mapper; + +import org.apache.lucene.index.IndexableField; +import org.junit.Before; +import org.opensearch.common.CheckedConsumer; +import org.opensearch.common.compress.CompressedXContent; +import org.opensearch.common.xcontent.XContentFactory; +import org.opensearch.common.xcontent.json.JsonXContent; +import org.opensearch.core.common.bytes.BytesReference; +import org.opensearch.core.xcontent.MediaTypeRegistry; +import org.opensearch.core.xcontent.XContentBuilder; +import org.opensearch.plugins.Plugin; +import org.opensearch.test.InternalSettingsPlugin; +import org.opensearch.test.OpenSearchSingleNodeTestCase; +import org.opensearch.index.IndexService; + +import java.io.IOException; +import java.util.Collection; + +import static org.hamcrest.Matchers.containsString; + +public class ConstantKeywordFieldMapperTests extends OpenSearchSingleNodeTestCase { + + private IndexService indexService; + private DocumentMapperParser parser; + + @Override + protected Collection> getPlugins() { + return pluginList(InternalSettingsPlugin.class); + } + + @Before + public void setup() { + indexService = createIndex("test"); + parser = indexService.mapperService().documentMapperParser(); + } + + public void testDefaultDisabledIndexMapper() throws Exception { + + + XContentBuilder mapping = XContentFactory.jsonBuilder() + .startObject() + .startObject("type") + .startObject("properties") + .startObject("field") + .field("type", "constant_keyword") + .field("value", "default_value").endObject() + .startObject("field2") + .field("type", "keyword").endObject(); + mapping = mapping.endObject().endObject().endObject(); + DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping.toString())); + + MapperParsingException e = expectThrows( + MapperParsingException.class, + () -> mapper.parse(source(b -> { + b.field("field", "sdf"); + b.field("field2", "szdfvsddf"); + })) + ); + assertThat(e.getMessage(), containsString("failed to parse field [field] of type [constant_keyword] in document with id '1'. Preview of field's value: 'sdf'")); + + final ParsedDocument doc = mapper.parse(source(b -> { + b.field("field", "default_value"); + b.field("field2", "field_2_value"); + })); + + final IndexableField field = doc.rootDoc().getField("field"); + + // constantKeywordField should not be stored + assertNull(field); + } + + private final SourceToParse source(CheckedConsumer build) throws IOException { + XContentBuilder builder = JsonXContent.contentBuilder().startObject(); + build.accept(builder); + builder.endObject(); + return new SourceToParse("test", "1", BytesReference.bytes(builder), MediaTypeRegistry.JSON); + } +} From 0b299fdadab1393f86b2384ae190be496eb2a6e5 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Mon, 12 Feb 2024 13:08:33 +0530 Subject: [PATCH 03/32] feat: constant keyword field mapper tests Signed-off-by: Mohammad Hasnain --- .../index/mapper/ConstantKeywordFieldMapper.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java index aab2ab6e52e31..b8d6b41a75c33 100644 --- a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java @@ -72,7 +72,7 @@ public Mapper.Builder parse(String name, Map node, ParserContext if (!node.containsKey("value")) { throw new OpenSearchParseException("Field [" + name + "] is missing required parameter [value]"); } - Object value = node.get("value"); + Object value = node.remove("value"); if (!(value instanceof String)) { throw new OpenSearchParseException("Field [" + name + "] is expected to be a string value"); } @@ -216,8 +216,13 @@ public ParametrizedFieldMapper.Builder getMergeBuilder() { @Override protected void parseCreateField(ParseContext context) throws IOException { - String value = context.parseExternalValue(String.class); + final String value; + if (context.externalValueSet()) { + value = context.externalValue().toString(); + } else { + value = context.parser().textOrNull(); + } if (value == null) { throw new IllegalArgumentException("constant keyword field [" + name() + "] must have a value"); } From ffd9985f4409b37ff87fd51a672ee136cebd7c23 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Mon, 12 Feb 2024 23:10:34 +0530 Subject: [PATCH 04/32] feat: remove unwanted elasticsearch blurb Signed-off-by: Mohammad Hasnain --- .../mapper/ConstantKeywordFieldMapper.java | 38 ++----------------- .../index/mapper/IndexFieldMapper.java | 24 ------------ 2 files changed, 4 insertions(+), 58 deletions(-) diff --git a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java index b8d6b41a75c33..1b2f59e57dfe3 100644 --- a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java @@ -6,41 +6,14 @@ * compatible open source license. */ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - package org.opensearch.index.mapper; import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.MultiTermQuery; import org.apache.lucene.search.Query; import org.opensearch.OpenSearchParseException; -import org.opensearch.common.Explicit; import org.opensearch.common.Nullable; import org.opensearch.common.annotation.PublicApi; -import org.opensearch.common.xcontent.support.XContentMapValues; -import org.opensearch.core.common.Strings; import org.opensearch.index.fielddata.IndexFieldData; import org.opensearch.index.fielddata.plain.ConstantIndexFieldData; import org.opensearch.index.query.QueryShardContext; @@ -49,7 +22,10 @@ import org.opensearch.search.lookup.SearchLookup; import java.io.IOException; -import java.util.*; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; import java.util.function.Supplier; /** @@ -60,12 +36,8 @@ @PublicApi(since = "1.0.0") public class ConstantKeywordFieldMapper extends ParametrizedFieldMapper { -// public static final String NAME = "_index"; - public static final String CONTENT_TYPE = "constant_keyword"; -// public static final TypeParser PARSER = new TypeParser((n, c) -> new ConstantKeywordFieldMapper.Builder(n, c)); - public static class TypeParser implements Mapper.TypeParser { @Override public Mapper.Builder parse(String name, Map node, ParserContext parserContext) throws MapperParsingException { @@ -125,8 +97,6 @@ static final class ConstantKeywordFieldType extends ConstantFieldType { protected final String value; -// static final ConstantKeywordFieldType INSTANCE = new ConstantKeywordFieldType(); - public ConstantKeywordFieldType(String name, String value) { super(name, Collections.emptyMap()); this.value = value; diff --git a/server/src/main/java/org/opensearch/index/mapper/IndexFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/IndexFieldMapper.java index 22fcb66848ed1..6511b9f69838f 100644 --- a/server/src/main/java/org/opensearch/index/mapper/IndexFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/IndexFieldMapper.java @@ -6,30 +6,6 @@ * compatible open source license. */ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - package org.opensearch.index.mapper; import org.apache.lucene.search.MatchAllDocsQuery; From e8803800295259332fb56ddbff966006fd781c76 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Mon, 12 Feb 2024 23:11:01 +0530 Subject: [PATCH 05/32] feat: remove unwanted elasticsearch blurb Signed-off-by: Mohammad Hasnain --- .../index/mapper/IndexFieldMapper.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/server/src/main/java/org/opensearch/index/mapper/IndexFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/IndexFieldMapper.java index 6511b9f69838f..22fcb66848ed1 100644 --- a/server/src/main/java/org/opensearch/index/mapper/IndexFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/IndexFieldMapper.java @@ -6,6 +6,30 @@ * compatible open source license. */ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + package org.opensearch.index.mapper; import org.apache.lucene.search.MatchAllDocsQuery; From f714ed7ba249734d6327b321fab12059f2193e48 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Mon, 12 Feb 2024 23:19:41 +0530 Subject: [PATCH 06/32] feat: remove unwanted elasticsearch blurb Signed-off-by: Mohammad Hasnain --- .../ConstantKeywordFieldMapperTests.java | 24 ------------------- .../mapper/ConstantKeywordFieldTypeTests.java | 23 ------------------ 2 files changed, 47 deletions(-) diff --git a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java index 8c0d1e597b97f..838ec857701b4 100644 --- a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java @@ -6,30 +6,6 @@ * compatible open source license. */ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - package org.opensearch.index.mapper; import org.apache.lucene.index.IndexableField; diff --git a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java index 6c645d8850bf8..02d3a82606061 100644 --- a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java @@ -6,29 +6,6 @@ * compatible open source license. */ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - package org.opensearch.index.mapper; import org.apache.lucene.search.MatchAllDocsQuery; From 4a60f2473a5c92f4f39b57e045878e7cfa8e85b3 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Mon, 12 Feb 2024 23:26:24 +0530 Subject: [PATCH 07/32] chore: remove unwanted comments Signed-off-by: Mohammad Hasnain --- .../opensearch/index/mapper/ConstantKeywordFieldMapper.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java index 1b2f59e57dfe3..bed3bb29f5a07 100644 --- a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java @@ -166,9 +166,6 @@ public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchL private final String value; -// public ConstantKeywordFieldMapper() { -// super(IndexFieldType.INSTANCE); -// } protected ConstantKeywordFieldMapper( String simpleName, MappedFieldType mappedFieldType, From 80ffe223fcebdcababa9303a09ccf11b3d4be352 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Tue, 13 Feb 2024 11:52:46 +0530 Subject: [PATCH 08/32] chore: remove unallowed system apis Signed-off-by: Mohammad Hasnain --- .../opensearch/index/mapper/ConstantKeywordFieldMapper.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java index bed3bb29f5a07..3135ea06f03cf 100644 --- a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java @@ -109,8 +109,6 @@ public String typeName() { @Override protected boolean matches(String searchValue, boolean caseInsensitive, QueryShardContext context) { - System.out.println(value); - return value.equals(searchValue); } @@ -146,7 +144,6 @@ public Query wildcardQuery( boolean caseInsensitve, QueryShardContext context ) { - System.out.println(("tes")); throw new QueryShardException( context, "Fields of type [" + typeName() + "], does not support wildcard queries" From ff9ede4d3f6570f7af174f0ef75515c698ea6ee4 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Wed, 14 Feb 2024 22:14:38 +0530 Subject: [PATCH 09/32] chore: run ./gradlew :server:spotlessApply Signed-off-by: Mohammad Hasnain --- .../mapper/ConstantKeywordFieldMapper.java | 25 +++++------------ .../org/opensearch/indices/IndicesModule.java | 2 +- .../ConstantKeywordFieldMapperTests.java | 27 ++++++++++--------- .../mapper/ConstantKeywordFieldTypeTests.java | 5 +++- 4 files changed, 26 insertions(+), 33 deletions(-) diff --git a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java index 3135ea06f03cf..57c14dbdf35ff 100644 --- a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java @@ -36,7 +36,7 @@ @PublicApi(since = "1.0.0") public class ConstantKeywordFieldMapper extends ParametrizedFieldMapper { - public static final String CONTENT_TYPE = "constant_keyword"; + public static final String CONTENT_TYPE = "constant_keyword"; public static class TypeParser implements Mapper.TypeParser { @Override @@ -52,7 +52,6 @@ public Mapper.Builder parse(String name, Map node, ParserContext } } - private static ConstantKeywordFieldMapper toType(FieldMapper in) { return (ConstantKeywordFieldMapper) in; } @@ -66,7 +65,7 @@ public static class Builder extends ParametrizedFieldMapper.Builder { private final Parameter value; - public Builder(String name, String value) { + public Builder(String name, String value) { super(name); this.value = Parameter.stringParam("value", false, m -> toType(m).value, value); } @@ -118,24 +117,15 @@ public Query existsQuery(QueryShardContext context) { } public Query termQueryCaseInsensitive(Object value, QueryShardContext context) { - throw new QueryShardException( - context, - "Fields of type [" + typeName() + "], does not support case insensitive term queries" - ); + throw new QueryShardException(context, "Fields of type [" + typeName() + "], does not support case insensitive term queries"); } public Query prefixQuery(Object value, QueryShardContext context) { - throw new QueryShardException( - context, - "Fields of type [" + typeName() + "], does not support prefix queries" - ); + throw new QueryShardException(context, "Fields of type [" + typeName() + "], does not support prefix queries"); } public Query wildcardQuery(String value, @Nullable MultiTermQuery.RewriteMethod method, QueryShardContext context) { - throw new QueryShardException( - context, - "Fields of type [" + typeName() + "], does not support wildcard queries" - ); + throw new QueryShardException(context, "Fields of type [" + typeName() + "], does not support wildcard queries"); } public Query wildcardQuery( @@ -144,10 +134,7 @@ public Query wildcardQuery( boolean caseInsensitve, QueryShardContext context ) { - throw new QueryShardException( - context, - "Fields of type [" + typeName() + "], does not support wildcard queries" - ); + throw new QueryShardException(context, "Fields of type [" + typeName() + "], does not support wildcard queries"); } @Override diff --git a/server/src/main/java/org/opensearch/indices/IndicesModule.java b/server/src/main/java/org/opensearch/indices/IndicesModule.java index dfbc3c558b6e0..fee2888c7a3fb 100644 --- a/server/src/main/java/org/opensearch/indices/IndicesModule.java +++ b/server/src/main/java/org/opensearch/indices/IndicesModule.java @@ -46,6 +46,7 @@ import org.opensearch.index.mapper.BinaryFieldMapper; import org.opensearch.index.mapper.BooleanFieldMapper; import org.opensearch.index.mapper.CompletionFieldMapper; +import org.opensearch.index.mapper.ConstantKeywordFieldMapper; import org.opensearch.index.mapper.DataStreamFieldMapper; import org.opensearch.index.mapper.DateFieldMapper; import org.opensearch.index.mapper.DocCountFieldMapper; @@ -70,7 +71,6 @@ import org.opensearch.index.mapper.SourceFieldMapper; import org.opensearch.index.mapper.TextFieldMapper; import org.opensearch.index.mapper.VersionFieldMapper; -import org.opensearch.index.mapper.ConstantKeywordFieldMapper; import org.opensearch.index.remote.RemoteStorePressureService; import org.opensearch.index.seqno.GlobalCheckpointSyncAction; import org.opensearch.index.seqno.RetentionLeaseBackgroundSyncAction; diff --git a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java index 838ec857701b4..53f41c30329d9 100644 --- a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java @@ -9,7 +9,6 @@ package org.opensearch.index.mapper; import org.apache.lucene.index.IndexableField; -import org.junit.Before; import org.opensearch.common.CheckedConsumer; import org.opensearch.common.compress.CompressedXContent; import org.opensearch.common.xcontent.XContentFactory; @@ -17,10 +16,11 @@ import org.opensearch.core.common.bytes.BytesReference; import org.opensearch.core.xcontent.MediaTypeRegistry; import org.opensearch.core.xcontent.XContentBuilder; +import org.opensearch.index.IndexService; import org.opensearch.plugins.Plugin; import org.opensearch.test.InternalSettingsPlugin; import org.opensearch.test.OpenSearchSingleNodeTestCase; -import org.opensearch.index.IndexService; +import org.junit.Before; import java.io.IOException; import java.util.Collection; @@ -45,27 +45,30 @@ public void setup() { public void testDefaultDisabledIndexMapper() throws Exception { - XContentBuilder mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .startObject("properties") .startObject("field") .field("type", "constant_keyword") - .field("value", "default_value").endObject() + .field("value", "default_value") + .endObject() .startObject("field2") - .field("type", "keyword").endObject(); + .field("type", "keyword") + .endObject(); mapping = mapping.endObject().endObject().endObject(); DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping.toString())); - MapperParsingException e = expectThrows( - MapperParsingException.class, - () -> mapper.parse(source(b -> { - b.field("field", "sdf"); - b.field("field2", "szdfvsddf"); - })) + MapperParsingException e = expectThrows(MapperParsingException.class, () -> mapper.parse(source(b -> { + b.field("field", "sdf"); + b.field("field2", "szdfvsddf"); + }))); + assertThat( + e.getMessage(), + containsString( + "failed to parse field [field] of type [constant_keyword] in document with id '1'. Preview of field's value: 'sdf'" + ) ); - assertThat(e.getMessage(), containsString("failed to parse field [field] of type [constant_keyword] in document with id '1'. Preview of field's value: 'sdf'")); final ParsedDocument doc = mapper.parse(source(b -> { b.field("field", "default_value"); diff --git a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java index 02d3a82606061..2a1a7fa8fe9a7 100644 --- a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java @@ -32,7 +32,10 @@ public void testPrefixQuery() { } public void testWildcardQuery() { - ConstantKeywordFieldMapper.ConstantKeywordFieldType ft = new ConstantKeywordFieldMapper.ConstantKeywordFieldType("field", "default"); + ConstantKeywordFieldMapper.ConstantKeywordFieldType ft = new ConstantKeywordFieldMapper.ConstantKeywordFieldType( + "field", + "default" + ); QueryShardException e = expectThrows( QueryShardException.class, From d67b8aa68197d00c1ce9dcf803173b6d895003c4 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Wed, 14 Feb 2024 22:27:10 +0530 Subject: [PATCH 10/32] chore: Triggering ci Signed-off-by: Mohammad Hasnain From 96133c66c4e1536aecacdbf963dfcbf982013c03 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Thu, 15 Feb 2024 00:29:20 +0530 Subject: [PATCH 11/32] feat: add wildcard, prefix query support Signed-off-by: Mohammad Hasnain --- .../index/mapper/ConstantFieldType.java | 8 +-- .../mapper/ConstantKeywordFieldMapper.java | 29 ++-------- .../mapper/ConstantKeywordFieldTypeTests.java | 54 +++++++++++++------ 3 files changed, 44 insertions(+), 47 deletions(-) diff --git a/server/src/main/java/org/opensearch/index/mapper/ConstantFieldType.java b/server/src/main/java/org/opensearch/index/mapper/ConstantFieldType.java index d23ab53116529..a28a6369b1aa4 100644 --- a/server/src/main/java/org/opensearch/index/mapper/ConstantFieldType.java +++ b/server/src/main/java/org/opensearch/index/mapper/ConstantFieldType.java @@ -91,7 +91,7 @@ public final Query termQuery(Object value, QueryShardContext context) { } @Override - public Query termQueryCaseInsensitive(Object value, QueryShardContext context) { + public final Query termQueryCaseInsensitive(Object value, QueryShardContext context) { String pattern = valueToString(value); if (matches(pattern, true, context)) { return Queries.newMatchAllQuery(); @@ -101,7 +101,7 @@ public Query termQueryCaseInsensitive(Object value, QueryShardContext context) { } @Override - public Query termsQuery(List values, QueryShardContext context) { + public final Query termsQuery(List values, QueryShardContext context) { for (Object value : values) { String pattern = valueToString(value); if (matches(pattern, false, context)) { @@ -113,7 +113,7 @@ public Query termsQuery(List values, QueryShardContext context) { } @Override - public Query prefixQuery( + public final Query prefixQuery( String prefix, @Nullable MultiTermQuery.RewriteMethod method, boolean caseInsensitive, @@ -128,7 +128,7 @@ public Query prefixQuery( } @Override - public Query wildcardQuery( + public final Query wildcardQuery( String value, @Nullable MultiTermQuery.RewriteMethod method, boolean caseInsensitive, diff --git a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java index 57c14dbdf35ff..b9d485bfcb084 100644 --- a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java @@ -9,15 +9,13 @@ package org.opensearch.index.mapper; import org.apache.lucene.search.MatchAllDocsQuery; -import org.apache.lucene.search.MultiTermQuery; import org.apache.lucene.search.Query; import org.opensearch.OpenSearchParseException; -import org.opensearch.common.Nullable; import org.opensearch.common.annotation.PublicApi; +import org.opensearch.common.regex.Regex; import org.opensearch.index.fielddata.IndexFieldData; import org.opensearch.index.fielddata.plain.ConstantIndexFieldData; import org.opensearch.index.query.QueryShardContext; -import org.opensearch.index.query.QueryShardException; import org.opensearch.search.aggregations.support.CoreValuesSourceType; import org.opensearch.search.lookup.SearchLookup; @@ -107,8 +105,8 @@ public String typeName() { } @Override - protected boolean matches(String searchValue, boolean caseInsensitive, QueryShardContext context) { - return value.equals(searchValue); + protected boolean matches(String pattern, boolean caseInsensitive, QueryShardContext context) { + return Regex.simpleMatch(pattern, value, caseInsensitive); } @Override @@ -116,27 +114,6 @@ public Query existsQuery(QueryShardContext context) { return new MatchAllDocsQuery(); } - public Query termQueryCaseInsensitive(Object value, QueryShardContext context) { - throw new QueryShardException(context, "Fields of type [" + typeName() + "], does not support case insensitive term queries"); - } - - public Query prefixQuery(Object value, QueryShardContext context) { - throw new QueryShardException(context, "Fields of type [" + typeName() + "], does not support prefix queries"); - } - - public Query wildcardQuery(String value, @Nullable MultiTermQuery.RewriteMethod method, QueryShardContext context) { - throw new QueryShardException(context, "Fields of type [" + typeName() + "], does not support wildcard queries"); - } - - public Query wildcardQuery( - String value, - @Nullable MultiTermQuery.RewriteMethod method, - boolean caseInsensitve, - QueryShardContext context - ) { - throw new QueryShardException(context, "Fields of type [" + typeName() + "], does not support wildcard queries"); - } - @Override public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, Supplier searchLookup) { return new ConstantIndexFieldData.Builder(fullyQualifiedIndexName, name(), CoreValuesSourceType.BYTES); diff --git a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java index 2a1a7fa8fe9a7..80c7decda5da8 100644 --- a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java @@ -16,42 +16,62 @@ import org.opensearch.common.settings.Settings; import org.opensearch.index.IndexSettings; import org.opensearch.index.query.QueryShardContext; -import org.opensearch.index.query.QueryShardException; +import java.util.Arrays; +import java.util.List; import java.util.function.Predicate; -import static org.hamcrest.Matchers.containsString; - public class ConstantKeywordFieldTypeTests extends FieldTypeTestCase { - public void testPrefixQuery() { - MappedFieldType ft = new ConstantKeywordFieldMapper.ConstantKeywordFieldType("field", "default"); + public void testTermQuery() { + ConstantKeywordFieldMapper.ConstantKeywordFieldType ft = new ConstantKeywordFieldMapper.ConstantKeywordFieldType( + "field", + "default" + ); assertEquals(new MatchAllDocsQuery(), ft.termQuery("default", createContext())); - assertEquals(new MatchNoDocsQuery(), ft.prefixQuery("not_default", null, createContext())); + assertEquals(new MatchNoDocsQuery(), ft.termQuery("not_default", createContext())); } - public void testWildcardQuery() { + public void testTermsQuery() { ConstantKeywordFieldMapper.ConstantKeywordFieldType ft = new ConstantKeywordFieldMapper.ConstantKeywordFieldType( "field", "default" ); - QueryShardException e = expectThrows( - QueryShardException.class, - () -> assertEquals(new MatchAllDocsQuery(), ft.wildcardQuery("ind*x", null, createContext())) + assertEquals(new MatchAllDocsQuery(), ft.termsQuery(Arrays.asList("default", "not_default"), createContext())); + assertEquals(new MatchNoDocsQuery(), ft.termQuery(Arrays.asList("no_default", "not_default"), createContext())); + assertEquals(new MatchNoDocsQuery(), ft.termsQuery(List.of(), createContext())); + } + + public void testInsensitiveTermQuery() { + ConstantKeywordFieldMapper.ConstantKeywordFieldType ft = new ConstantKeywordFieldMapper.ConstantKeywordFieldType( + "field", + "default" ); - assertThat(e.getMessage(), containsString("Fields of type [constant_keyword], does not support wildcard queries")); + + assertEquals(new MatchAllDocsQuery(), ft.termQueryCaseInsensitive("defaUlt", createContext())); + assertEquals(new MatchNoDocsQuery(), ft.termQueryCaseInsensitive("not_defaUlt", createContext())); } - public void testRegexpQuery() { - MappedFieldType ft = IndexFieldMapper.IndexFieldType.INSTANCE; + public void testPrefixQuery() { + ConstantKeywordFieldMapper.ConstantKeywordFieldType ft = new ConstantKeywordFieldMapper.ConstantKeywordFieldType( + "field", + "default" + ); - QueryShardException e = expectThrows( - QueryShardException.class, - () -> assertEquals(new MatchAllDocsQuery(), ft.regexpQuery("ind.x", 0, 0, 10, null, createContext())) + assertEquals(new MatchAllDocsQuery(), ft.prefixQuery("defau", null, createContext())); + assertEquals(new MatchNoDocsQuery(), ft.prefixQuery("not_default", null, createContext())); + } + + public void testWildcardQuery() { + ConstantKeywordFieldMapper.ConstantKeywordFieldType ft = new ConstantKeywordFieldMapper.ConstantKeywordFieldType( + "field", + "default" ); - assertThat(e.getMessage(), containsString("Can only use regexp queries on keyword and text fields")); + assertEquals(new MatchAllDocsQuery(), ft.wildcardQuery("defa*lt", null, createContext())); + assertEquals(new MatchNoDocsQuery(), ft.wildcardQuery("no_defa*lt", null, createContext())); + } private QueryShardContext createContext() { From d65029ee5db497cd612c5af2933e8518a88a6cbe Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Thu, 15 Feb 2024 00:57:48 +0530 Subject: [PATCH 12/32] test: add wildcard complicated examples like *ault Signed-off-by: Mohammad Hasnain --- .../opensearch/index/mapper/ConstantKeywordFieldTypeTests.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java index 80c7decda5da8..8659e7d09243c 100644 --- a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java @@ -71,6 +71,8 @@ public void testWildcardQuery() { ); assertEquals(new MatchAllDocsQuery(), ft.wildcardQuery("defa*lt", null, createContext())); assertEquals(new MatchNoDocsQuery(), ft.wildcardQuery("no_defa*lt", null, createContext())); + assertEquals(new MatchAllDocsQuery(), ft.wildcardQuery("defa*", null, createContext())); + assertEquals(new MatchAllDocsQuery(), ft.wildcardQuery("*ult", null, createContext())); } From 1a62a32e223f3c1611b72e5b961b5f1481ddbccd Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Fri, 16 Feb 2024 00:15:32 +0530 Subject: [PATCH 13/32] chore: add javadocs Signed-off-by: Mohammad Hasnain --- .../opensearch/index/mapper/ConstantKeywordFieldMapper.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java index b9d485bfcb084..0e3e6bd88d4ad 100644 --- a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java @@ -36,6 +36,11 @@ public class ConstantKeywordFieldMapper extends ParametrizedFieldMapper { public static final String CONTENT_TYPE = "constant_keyword"; + /** + * A {@link Mapper.TypeParser} for the constant keyword field. + * + * @opensearch.internal + */ public static class TypeParser implements Mapper.TypeParser { @Override public Mapper.Builder parse(String name, Map node, ParserContext parserContext) throws MapperParsingException { From 6ab5ab2eeed599341d70bb28f4542042b2b71cb2 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Fri, 16 Feb 2024 00:51:19 +0530 Subject: [PATCH 14/32] test: handle AggregatorTestCase.testSupportedFieldTypes Signed-off-by: Mohammad Hasnain --- .../opensearch/search/aggregations/AggregatorTestCase.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/framework/src/main/java/org/opensearch/search/aggregations/AggregatorTestCase.java b/test/framework/src/main/java/org/opensearch/search/aggregations/AggregatorTestCase.java index 4eb49ebb42241..f83163bd139cd 100644 --- a/test/framework/src/main/java/org/opensearch/search/aggregations/AggregatorTestCase.java +++ b/test/framework/src/main/java/org/opensearch/search/aggregations/AggregatorTestCase.java @@ -95,6 +95,7 @@ import org.opensearch.index.fielddata.IndexFieldDataService; import org.opensearch.index.mapper.BinaryFieldMapper; import org.opensearch.index.mapper.CompletionFieldMapper; +import org.opensearch.index.mapper.ConstantKeywordFieldMapper; import org.opensearch.index.mapper.ContentPath; import org.opensearch.index.mapper.DateFieldMapper; import org.opensearch.index.mapper.FieldAliasMapper; @@ -778,6 +779,10 @@ public void testSupportedFieldTypes() throws IOException { source.put("doc_values", "true"); } + if (mappedType.getKey().equals(ConstantKeywordFieldMapper.CONTENT_TYPE) == true) { + source.put("value", "default_value"); + } + Mapper.Builder builder = mappedType.getValue().parse(fieldName, source, new MockParserContext()); FieldMapper mapper = (FieldMapper) builder.build(new BuilderContext(settings, new ContentPath())); From 602d7ea7e1e19555f6028f7aa5f4d47dcc74d816 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Thu, 22 Feb 2024 00:57:43 +0530 Subject: [PATCH 15/32] test: handle SignificantTextAggregatorTests.testSupportedFieldTypes Signed-off-by: Mohammad Hasnain --- .../bucket/terms/SignificantTextAggregatorTests.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/server/src/test/java/org/opensearch/search/aggregations/bucket/terms/SignificantTextAggregatorTests.java b/server/src/test/java/org/opensearch/search/aggregations/bucket/terms/SignificantTextAggregatorTests.java index e9b2d40fd4ede..644cee57bd5a4 100644 --- a/server/src/test/java/org/opensearch/search/aggregations/bucket/terms/SignificantTextAggregatorTests.java +++ b/server/src/test/java/org/opensearch/search/aggregations/bucket/terms/SignificantTextAggregatorTests.java @@ -50,6 +50,7 @@ import org.opensearch.index.analysis.AnalyzerScope; import org.opensearch.index.analysis.NamedAnalyzer; import org.opensearch.index.mapper.BinaryFieldMapper; +import org.opensearch.index.mapper.ConstantKeywordFieldMapper; import org.opensearch.index.mapper.FlatObjectFieldMapper; import org.opensearch.index.mapper.GeoPointFieldMapper; import org.opensearch.index.mapper.MappedFieldType; @@ -104,7 +105,8 @@ protected List unsupportedMappedFieldTypes() { return Arrays.asList( BinaryFieldMapper.CONTENT_TYPE, // binary fields are not supported because they do not have analyzers GeoPointFieldMapper.CONTENT_TYPE, // geopoint fields cannot use term queries - FlatObjectFieldMapper.CONTENT_TYPE // flat_object fields are not supported aggregations + FlatObjectFieldMapper.CONTENT_TYPE, // flat_object fields are not supported aggregations + ConstantKeywordFieldMapper.CONTENT_TYPE // binary fields are not supported because they do not have analyzers ); } From 2b52fd930eeab2e04e0046f4de1b80ca3e46f8f3 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Thu, 22 Feb 2024 01:27:11 +0530 Subject: [PATCH 16/32] test: trigger ci Signed-off-by: Mohammad Hasnain From 5edbb0b9773926e3b910bbcde81cb858f01cf412 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Thu, 22 Feb 2024 09:34:18 +0530 Subject: [PATCH 17/32] chore: update public api Signed-off-by: Mohammad Hasnain --- .../org/opensearch/index/mapper/ConstantKeywordFieldMapper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java index 0e3e6bd88d4ad..12981ef04fbd1 100644 --- a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java @@ -31,7 +31,7 @@ * * @opensearch.api */ -@PublicApi(since = "1.0.0") +@PublicApi(since = "2.13.0") public class ConstantKeywordFieldMapper extends ParametrizedFieldMapper { public static final String CONTENT_TYPE = "constant_keyword"; From 155521a33a35123da1fc43ce9932f982603b8a6b Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Thu, 22 Feb 2024 09:34:43 +0530 Subject: [PATCH 18/32] test: Fix typo Signed-off-by: Mohammad Hasnain --- .../opensearch/index/mapper/ConstantKeywordFieldTypeTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java index 8659e7d09243c..1907be6564822 100644 --- a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java @@ -40,7 +40,7 @@ public void testTermsQuery() { ); assertEquals(new MatchAllDocsQuery(), ft.termsQuery(Arrays.asList("default", "not_default"), createContext())); - assertEquals(new MatchNoDocsQuery(), ft.termQuery(Arrays.asList("no_default", "not_default"), createContext())); + assertEquals(new MatchNoDocsQuery(), ft.termsQuery(Arrays.asList("no_default", "not_default"), createContext())); assertEquals(new MatchNoDocsQuery(), ft.termsQuery(List.of(), createContext())); } From e2852c714763e48ea28c166e2a57bfde0b75e839 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Thu, 22 Feb 2024 09:36:25 +0530 Subject: [PATCH 19/32] fix: make MappedFieldType.wildcardQuery Final Signed-off-by: Mohammad Hasnain --- .../main/java/org/opensearch/index/mapper/MappedFieldType.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/main/java/org/opensearch/index/mapper/MappedFieldType.java b/server/src/main/java/org/opensearch/index/mapper/MappedFieldType.java index 797608b46c890..66d4654e543a2 100644 --- a/server/src/main/java/org/opensearch/index/mapper/MappedFieldType.java +++ b/server/src/main/java/org/opensearch/index/mapper/MappedFieldType.java @@ -304,7 +304,7 @@ public Query prefixQuery( } // Case sensitive form of wildcard query - public Query wildcardQuery(String value, @Nullable MultiTermQuery.RewriteMethod method, QueryShardContext context) { + public final Query wildcardQuery(String value, @Nullable MultiTermQuery.RewriteMethod method, QueryShardContext context) { return wildcardQuery(value, method, false, context); } From ed4cafb89e236ee541f8a0a25d8cc9798fbffb67 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Thu, 22 Feb 2024 10:05:28 +0530 Subject: [PATCH 20/32] feature: implement valueFetcher Signed-off-by: Mohammad Hasnain --- .../index/mapper/ConstantKeywordFieldMapper.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java index 12981ef04fbd1..fafe5c219c9db 100644 --- a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java @@ -126,7 +126,17 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, S @Override public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { - throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "]."); + if (format != null) { + throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't " + "support formats."); + } + + return new SourceValueFetcher(name(), context) { + @Override + protected Object parseSourceValue(Object value) { + String keywordValue = value.toString(); + return Collections.singletonList(keywordValue); + } + }; } } From cd6e682cd71cb28b5cb0a0f70a11b719ea69a653 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Thu, 14 Mar 2024 13:19:05 +0530 Subject: [PATCH 21/32] test: add exists query tests Signed-off-by: Mohammad Hasnain --- .../index/mapper/ConstantKeywordFieldTypeTests.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java index 1907be6564822..680db5bc72927 100644 --- a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java @@ -76,6 +76,14 @@ public void testWildcardQuery() { } + public void testExistsQuery() { + ConstantKeywordFieldMapper.ConstantKeywordFieldType ft = new ConstantKeywordFieldMapper.ConstantKeywordFieldType( + "field", + "default" + ); + assertEquals(new MatchAllDocsQuery(), ft.existsQuery(createContext())); + } + private QueryShardContext createContext() { IndexMetadata indexMetadata = IndexMetadata.builder("index") .settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)) From b3716a43259336ffb795f2a98751ffc492d0e0b1 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Fri, 15 Mar 2024 00:24:33 +0530 Subject: [PATCH 22/32] test: add no default test Signed-off-by: Mohammad Hasnain --- .../ConstantKeywordFieldMapperTests.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java index 53f41c30329d9..aeaa732399f86 100644 --- a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java @@ -9,6 +9,7 @@ package org.opensearch.index.mapper; import org.apache.lucene.index.IndexableField; +import org.opensearch.OpenSearchParseException; import org.opensearch.common.CheckedConsumer; import org.opensearch.common.compress.CompressedXContent; import org.opensearch.common.xcontent.XContentFactory; @@ -81,6 +82,28 @@ public void testDefaultDisabledIndexMapper() throws Exception { assertNull(field); } + public void testMissingDefaultIndexMapper() throws Exception { + + final XContentBuilder mapping = XContentFactory.jsonBuilder() + .startObject() + .startObject("type") + .startObject("properties") + .startObject("field") + .field("type", "constant_keyword") + .endObject() + .startObject("field2") + .field("type", "keyword") + .endObject().endObject().endObject().endObject(); + + OpenSearchParseException e = expectThrows(OpenSearchParseException.class, () -> parser.parse("type", new CompressedXContent(mapping.toString()))); + assertThat( + e.getMessage(), + containsString( + "Field [field] is missing required parameter [value]" + ) + ); + } + private final SourceToParse source(CheckedConsumer build) throws IOException { XContentBuilder builder = JsonXContent.contentBuilder().startObject(); build.accept(builder); From fea6b8fff1064c032c11fc6d16294d7c66a3f397 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Fri, 15 Mar 2024 00:46:07 +0530 Subject: [PATCH 23/32] test: formatting Signed-off-by: Mohammad Hasnain --- .../mapper/ConstantKeywordFieldMapperTests.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java index aeaa732399f86..65dd3b6447663 100644 --- a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java @@ -93,15 +93,16 @@ public void testMissingDefaultIndexMapper() throws Exception { .endObject() .startObject("field2") .field("type", "keyword") - .endObject().endObject().endObject().endObject(); + .endObject() + .endObject() + .endObject() + .endObject(); - OpenSearchParseException e = expectThrows(OpenSearchParseException.class, () -> parser.parse("type", new CompressedXContent(mapping.toString()))); - assertThat( - e.getMessage(), - containsString( - "Field [field] is missing required parameter [value]" - ) + OpenSearchParseException e = expectThrows( + OpenSearchParseException.class, + () -> parser.parse("type", new CompressedXContent(mapping.toString())) ); + assertThat(e.getMessage(), containsString("Field [field] is missing required parameter [value]")); } private final SourceToParse source(CheckedConsumer build) throws IOException { From 79ba93fff962ae0a1e7461c9277c92f6aee47e3b Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Fri, 15 Mar 2024 21:02:25 +0530 Subject: [PATCH 24/32] Trigger Build Signed-off-by: Mohammad Hasnain From d49782c25987dc26fcb97e0a134e74e5d9dcd429 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Fri, 15 Mar 2024 22:54:12 +0530 Subject: [PATCH 25/32] chore: add CHANGELOG.md entry Signed-off-by: Mohammad Hasnain --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38246f566cf6e..bbad7e0a9a0b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Tracing for deep search path ([#12103](https://github.com/opensearch-project/OpenSearch/pull/12103)) - Add explicit dependency to validatePom and generatePom tasks ([#12807](https://github.com/opensearch-project/OpenSearch/pull/12807)) - Replace configureEach with all for publication iteration ([#12876](https://github.com/opensearch-project/OpenSearch/pull/12876)) +- Constant Keyword Field ([#12285](https://github.com/opensearch-project/OpenSearch/pull/12285)) ### Dependencies - Bump `log4j-core` from 2.18.0 to 2.19.0 From 3408f4a4e4f5a5f9b5901695cf22b36c94af016e Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Fri, 22 Mar 2024 20:56:04 +0530 Subject: [PATCH 26/32] fix: use constants Signed-off-by: Mohammad Hasnain --- .../mapper/ConstantKeywordFieldMapper.java | 10 +++--- .../mapper/ConstantKeywordFieldTypeTests.java | 33 +++---------------- 2 files changed, 11 insertions(+), 32 deletions(-) diff --git a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java index fafe5c219c9db..f2f2e1e12a046 100644 --- a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java @@ -31,11 +31,13 @@ * * @opensearch.api */ -@PublicApi(since = "2.13.0") +@PublicApi(since = "2.14.0") public class ConstantKeywordFieldMapper extends ParametrizedFieldMapper { public static final String CONTENT_TYPE = "constant_keyword"; + private static final String valuePropertyName = "value"; + /** * A {@link Mapper.TypeParser} for the constant keyword field. * @@ -44,10 +46,10 @@ public class ConstantKeywordFieldMapper extends ParametrizedFieldMapper { public static class TypeParser implements Mapper.TypeParser { @Override public Mapper.Builder parse(String name, Map node, ParserContext parserContext) throws MapperParsingException { - if (!node.containsKey("value")) { + if (!node.containsKey(valuePropertyName)) { throw new OpenSearchParseException("Field [" + name + "] is missing required parameter [value]"); } - Object value = node.remove("value"); + Object value = node.remove(valuePropertyName); if (!(value instanceof String)) { throw new OpenSearchParseException("Field [" + name + "] is expected to be a string value"); } @@ -70,7 +72,7 @@ public static class Builder extends ParametrizedFieldMapper.Builder { public Builder(String name, String value) { super(name); - this.value = Parameter.stringParam("value", false, m -> toType(m).value, value); + this.value = Parameter.stringParam(valuePropertyName, false, m -> toType(m).value, value); } @Override diff --git a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java index 680db5bc72927..235811539a299 100644 --- a/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldTypeTests.java @@ -23,52 +23,33 @@ public class ConstantKeywordFieldTypeTests extends FieldTypeTestCase { - public void testTermQuery() { - ConstantKeywordFieldMapper.ConstantKeywordFieldType ft = new ConstantKeywordFieldMapper.ConstantKeywordFieldType( - "field", - "default" - ); + final ConstantKeywordFieldMapper.ConstantKeywordFieldType ft = new ConstantKeywordFieldMapper.ConstantKeywordFieldType( + "field", + "default" + ); + public void testTermQuery() { assertEquals(new MatchAllDocsQuery(), ft.termQuery("default", createContext())); assertEquals(new MatchNoDocsQuery(), ft.termQuery("not_default", createContext())); } public void testTermsQuery() { - ConstantKeywordFieldMapper.ConstantKeywordFieldType ft = new ConstantKeywordFieldMapper.ConstantKeywordFieldType( - "field", - "default" - ); - assertEquals(new MatchAllDocsQuery(), ft.termsQuery(Arrays.asList("default", "not_default"), createContext())); assertEquals(new MatchNoDocsQuery(), ft.termsQuery(Arrays.asList("no_default", "not_default"), createContext())); assertEquals(new MatchNoDocsQuery(), ft.termsQuery(List.of(), createContext())); } public void testInsensitiveTermQuery() { - ConstantKeywordFieldMapper.ConstantKeywordFieldType ft = new ConstantKeywordFieldMapper.ConstantKeywordFieldType( - "field", - "default" - ); - assertEquals(new MatchAllDocsQuery(), ft.termQueryCaseInsensitive("defaUlt", createContext())); assertEquals(new MatchNoDocsQuery(), ft.termQueryCaseInsensitive("not_defaUlt", createContext())); } public void testPrefixQuery() { - ConstantKeywordFieldMapper.ConstantKeywordFieldType ft = new ConstantKeywordFieldMapper.ConstantKeywordFieldType( - "field", - "default" - ); - assertEquals(new MatchAllDocsQuery(), ft.prefixQuery("defau", null, createContext())); assertEquals(new MatchNoDocsQuery(), ft.prefixQuery("not_default", null, createContext())); } public void testWildcardQuery() { - ConstantKeywordFieldMapper.ConstantKeywordFieldType ft = new ConstantKeywordFieldMapper.ConstantKeywordFieldType( - "field", - "default" - ); assertEquals(new MatchAllDocsQuery(), ft.wildcardQuery("defa*lt", null, createContext())); assertEquals(new MatchNoDocsQuery(), ft.wildcardQuery("no_defa*lt", null, createContext())); assertEquals(new MatchAllDocsQuery(), ft.wildcardQuery("defa*", null, createContext())); @@ -77,10 +58,6 @@ public void testWildcardQuery() { } public void testExistsQuery() { - ConstantKeywordFieldMapper.ConstantKeywordFieldType ft = new ConstantKeywordFieldMapper.ConstantKeywordFieldType( - "field", - "default" - ); assertEquals(new MatchAllDocsQuery(), ft.existsQuery(createContext())); } From 97b0347d33f4e058671bf8ef1cb147763b519a5f Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Tue, 26 Mar 2024 04:35:32 +0530 Subject: [PATCH 27/32] chore: move changelog from 3.x -> 2.x Signed-off-by: Mohammad Hasnain --- CHANGELOG.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbad7e0a9a0b6..19e302a4003ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,9 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Remote reindex: Add support for configurable retry mechanism ([#12561](https://github.com/opensearch-project/OpenSearch/pull/12561)) - [Admission Control] Integrate IO Usage Tracker to the Resource Usage Collector Service and Emit IO Usage Stats ([#11880](https://github.com/opensearch-project/OpenSearch/pull/11880)) - Tracing for deep search path ([#12103](https://github.com/opensearch-project/OpenSearch/pull/12103)) -- Add explicit dependency to validatePom and generatePom tasks ([#12807](https://github.com/opensearch-project/OpenSearch/pull/12807)) -- Replace configureEach with all for publication iteration ([#12876](https://github.com/opensearch-project/OpenSearch/pull/12876)) -- Constant Keyword Field ([#12285](https://github.com/opensearch-project/OpenSearch/pull/12285)) +- Add explicit dependency to validatePom and generatePom tasks ([#12103](https://github.com/opensearch-project/OpenSearch/pull/12807)) ### Dependencies - Bump `log4j-core` from 2.18.0 to 2.19.0 @@ -104,6 +102,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased 2.x] ### Added +- Constant Keyword Field ([#12285](https://github.com/opensearch-project/OpenSearch/pull/12285)) - Convert ingest processor supports ip type ([#12818](https://github.com/opensearch-project/OpenSearch/pull/12818)) - Add a counter to node stat api to track shard going from idle to non-idle ([#12768](https://github.com/opensearch-project/OpenSearch/pull/12768)) - Allow setting KEYSTORE_PASSWORD through env variable ([#12865](https://github.com/opensearch-project/OpenSearch/pull/12865)) From 18adb365dfb6a33acf7cea98b83d484d81ba6434 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Tue, 26 Mar 2024 05:40:48 +0530 Subject: [PATCH 28/32] chore: remove unwanted changelog changess Signed-off-by: Mohammad Hasnain --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19e302a4003ec..c055db1b00fdd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - [Admission Control] Integrate IO Usage Tracker to the Resource Usage Collector Service and Emit IO Usage Stats ([#11880](https://github.com/opensearch-project/OpenSearch/pull/11880)) - Tracing for deep search path ([#12103](https://github.com/opensearch-project/OpenSearch/pull/12103)) - Add explicit dependency to validatePom and generatePom tasks ([#12103](https://github.com/opensearch-project/OpenSearch/pull/12807)) +- Replace configureEach with all for publication iteration ([#12876](https://github.com/opensearch-project/OpenSearch/pull/12876)) ### Dependencies - Bump `log4j-core` from 2.18.0 to 2.19.0 From 8125ad19b596a10f7c19472cbbceb9585dd75949 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Tue, 26 Mar 2024 05:42:13 +0530 Subject: [PATCH 29/32] chore: remove unwanted changelog changess Signed-off-by: Mohammad Hasnain --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c055db1b00fdd..2a43b676ce166 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Remote reindex: Add support for configurable retry mechanism ([#12561](https://github.com/opensearch-project/OpenSearch/pull/12561)) - [Admission Control] Integrate IO Usage Tracker to the Resource Usage Collector Service and Emit IO Usage Stats ([#11880](https://github.com/opensearch-project/OpenSearch/pull/11880)) - Tracing for deep search path ([#12103](https://github.com/opensearch-project/OpenSearch/pull/12103)) -- Add explicit dependency to validatePom and generatePom tasks ([#12103](https://github.com/opensearch-project/OpenSearch/pull/12807)) +- Add explicit dependency to validatePom and generatePom tasks ([#12807](https://github.com/opensearch-project/OpenSearch/pull/12807)) - Replace configureEach with all for publication iteration ([#12876](https://github.com/opensearch-project/OpenSearch/pull/12876)) ### Dependencies From bd3f2f7789f888e27517d5f5f64b2da617408305 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Wed, 27 Mar 2024 21:55:27 +0530 Subject: [PATCH 30/32] chore: make ConstantKeywordFieldType public api Signed-off-by: Mohammad Hasnain --- .../org/opensearch/index/mapper/ConstantKeywordFieldMapper.java | 1 + 1 file changed, 1 insertion(+) diff --git a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java index f2f2e1e12a046..cafea3937310d 100644 --- a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java @@ -97,6 +97,7 @@ public ConstantKeywordFieldMapper build(BuilderContext context) { * * @opensearch.internal */ + @PublicApi(since = "2.14.0") static final class ConstantKeywordFieldType extends ConstantFieldType { protected final String value; From 330e0ab18aa69a57c0ca91732f458bb91b9b03c7 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Sat, 30 Mar 2024 17:26:48 +0530 Subject: [PATCH 31/32] fix: make ConstantKeywordFieldType protected Signed-off-by: Mohammad Hasnain --- .../org/opensearch/index/mapper/ConstantKeywordFieldMapper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java index cafea3937310d..f4730c70362d1 100644 --- a/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java @@ -98,7 +98,7 @@ public ConstantKeywordFieldMapper build(BuilderContext context) { * @opensearch.internal */ @PublicApi(since = "2.14.0") - static final class ConstantKeywordFieldType extends ConstantFieldType { + protected static final class ConstantKeywordFieldType extends ConstantFieldType { protected final String value; From 917b2bb769908b6de2fd39814e7af9e44ca74ab7 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Sat, 30 Mar 2024 18:34:36 +0530 Subject: [PATCH 32/32] chore: trigger ci Signed-off-by: Mohammad Hasnain