From ed7d1c7b0d4551ff044c2938dcef49e45f63ed97 Mon Sep 17 00:00:00 2001 From: Xue Zhou <85715413+xuezhou25@users.noreply.github.com> Date: Mon, 29 Nov 2021 19:01:43 +0000 Subject: [PATCH] Rename field_masking_span to span_field_masking (#1606) * Rename field_masking_span to span_field_masking Signed-off-by: Xue Zhou * Update SearchModuleTests.java Signed-off-by: Xue Zhou * Rename field_masking_span to span_field_masking Signed-off-by: Xue Zhou --- .../query/FieldMaskingSpanQueryBuilder.java | 25 ++++++++++++------ .../org/opensearch/search/SearchModule.java | 2 +- .../FieldMaskingSpanQueryBuilderTests.java | 26 ++++++++++++++++++- .../opensearch/search/SearchModuleTests.java | 6 ++--- 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/server/src/main/java/org/opensearch/index/query/FieldMaskingSpanQueryBuilder.java b/server/src/main/java/org/opensearch/index/query/FieldMaskingSpanQueryBuilder.java index 88d3910d31bb0..ebc451a4493ed 100644 --- a/server/src/main/java/org/opensearch/index/query/FieldMaskingSpanQueryBuilder.java +++ b/server/src/main/java/org/opensearch/index/query/FieldMaskingSpanQueryBuilder.java @@ -48,7 +48,7 @@ import java.util.Objects; public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder implements SpanQueryBuilder { - public static final String NAME = "field_masking_span"; + public static final ParseField SPAN_FIELD_MASKING_FIELD = new ParseField("span_field_masking", "field_masking_span"); private static final ParseField FIELD_FIELD = new ParseField("field"); private static final ParseField QUERY_FIELD = new ParseField("query"); @@ -105,7 +105,7 @@ public SpanQueryBuilder innerQuery() { @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(NAME); + builder.startObject(SPAN_FIELD_MASKING_FIELD.getPreferredName()); builder.field(QUERY_FIELD.getPreferredName()); queryBuilder.toXContent(builder, params); builder.field(FIELD_FIELD.getPreferredName(), fieldName); @@ -129,13 +129,16 @@ public static FieldMaskingSpanQueryBuilder fromXContent(XContentParser parser) t if (QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { QueryBuilder query = parseInnerQueryBuilder(parser); if (query instanceof SpanQueryBuilder == false) { - throw new ParsingException(parser.getTokenLocation(), "[field_masking_span] query must be of type span query"); + throw new ParsingException( + parser.getTokenLocation(), + "[" + SPAN_FIELD_MASKING_FIELD.getPreferredName() + "] query must be of type span query" + ); } inner = (SpanQueryBuilder) query; } else { throw new ParsingException( parser.getTokenLocation(), - "[field_masking_span] query does not support [" + currentFieldName + "]" + "[" + SPAN_FIELD_MASKING_FIELD.getPreferredName() + "] query does not support [" + currentFieldName + "]" ); } } else { @@ -148,16 +151,22 @@ public static FieldMaskingSpanQueryBuilder fromXContent(XContentParser parser) t } else { throw new ParsingException( parser.getTokenLocation(), - "[field_masking_span] query does not support [" + currentFieldName + "]" + "[" + SPAN_FIELD_MASKING_FIELD.getPreferredName() + "] query does not support [" + currentFieldName + "]" ); } } } if (inner == null) { - throw new ParsingException(parser.getTokenLocation(), "field_masking_span must have [query] span query clause"); + throw new ParsingException( + parser.getTokenLocation(), + SPAN_FIELD_MASKING_FIELD.getPreferredName() + " must have [query] span query clause" + ); } if (field == null) { - throw new ParsingException(parser.getTokenLocation(), "field_masking_span must have [field] set for it"); + throw new ParsingException( + parser.getTokenLocation(), + SPAN_FIELD_MASKING_FIELD.getPreferredName() + " must have [field] set for it" + ); } FieldMaskingSpanQueryBuilder queryBuilder = new FieldMaskingSpanQueryBuilder(inner, field); @@ -190,6 +199,6 @@ protected boolean doEquals(FieldMaskingSpanQueryBuilder other) { @Override public String getWriteableName() { - return NAME; + return SPAN_FIELD_MASKING_FIELD.getPreferredName(); } } diff --git a/server/src/main/java/org/opensearch/search/SearchModule.java b/server/src/main/java/org/opensearch/search/SearchModule.java index d4b801e974b1c..704777946dc1d 100644 --- a/server/src/main/java/org/opensearch/search/SearchModule.java +++ b/server/src/main/java/org/opensearch/search/SearchModule.java @@ -1172,7 +1172,7 @@ private void registerQueryParsers(List plugins) { ); registerQuery( new QuerySpec<>( - FieldMaskingSpanQueryBuilder.NAME, + FieldMaskingSpanQueryBuilder.SPAN_FIELD_MASKING_FIELD, FieldMaskingSpanQueryBuilder::new, FieldMaskingSpanQueryBuilder::fromXContent ) diff --git a/server/src/test/java/org/opensearch/index/query/FieldMaskingSpanQueryBuilderTests.java b/server/src/test/java/org/opensearch/index/query/FieldMaskingSpanQueryBuilderTests.java index 88ac55fee7311..10d4a7918fb1e 100644 --- a/server/src/test/java/org/opensearch/index/query/FieldMaskingSpanQueryBuilderTests.java +++ b/server/src/test/java/org/opensearch/index/query/FieldMaskingSpanQueryBuilderTests.java @@ -38,6 +38,7 @@ import java.io.IOException; +import static org.opensearch.index.query.FieldMaskingSpanQueryBuilder.SPAN_FIELD_MASKING_FIELD; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; @@ -73,7 +74,9 @@ public void testIllegalArguments() { public void testFromJson() throws IOException { String json = "{\n" - + " \"field_masking_span\" : {\n" + + " \"" + + SPAN_FIELD_MASKING_FIELD.getPreferredName() + + "\" : {\n" + " \"query\" : {\n" + " \"span_term\" : {\n" + " \"value\" : {\n" @@ -92,4 +95,25 @@ public void testFromJson() throws IOException { assertEquals(json, 42.0, parsed.boost(), 0.00001); assertEquals(json, 0.23, parsed.innerQuery().boost(), 0.00001); } + + public void testDeprecatedName() throws IOException { + String json = "{\n" + + " \"field_masking_span\" : {\n" + + " \"query\" : {\n" + + " \"span_term\" : {\n" + + " \"value\" : {\n" + + " \"value\" : \"foo\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"field\" : \"mapped_geo_shape\",\n" + + " \"boost\" : 42.0,\n" + + " \"_name\" : \"KPI\"\n" + + " }\n" + + "}"; + FieldMaskingSpanQueryBuilder parsed = (FieldMaskingSpanQueryBuilder) parseQuery(json); + assertWarnings( + "Deprecated field [field_masking_span] used, expected [" + SPAN_FIELD_MASKING_FIELD.getPreferredName() + "] instead" + ); + } } diff --git a/server/src/test/java/org/opensearch/search/SearchModuleTests.java b/server/src/test/java/org/opensearch/search/SearchModuleTests.java index a2d3b39d11a8e..cef54e5b8e14d 100644 --- a/server/src/test/java/org/opensearch/search/SearchModuleTests.java +++ b/server/src/test/java/org/opensearch/search/SearchModuleTests.java @@ -337,7 +337,7 @@ public void testRegisteredQueries() { Set registeredNonDeprecated = module.getNamedXContents() .stream() .filter(e -> e.categoryClass.equals(QueryBuilder.class)) - .filter(e -> e.name.getDeprecatedNames().length == 0) + .filter(e -> e.name.getAllReplacedWith() == null) .map(e -> e.name.getPreferredName()) .collect(toSet()); Set registeredAll = module.getNamedXContents() @@ -422,7 +422,6 @@ public List> getRescorers() { "constant_score", "dis_max", "exists", - "field_masking_span", "function_score", "fuzzy", "geo_bounding_box", @@ -448,6 +447,7 @@ public List> getRescorers() { "script_score", "simple_query_string", "span_containing", + "span_field_masking", "span_first", "span_gap", "span_multi", @@ -465,7 +465,7 @@ public List> getRescorers() { "distance_feature" }; // add here deprecated queries to make sure we log a deprecation warnings when they are used - private static final String[] DEPRECATED_QUERIES = new String[] { "common" }; + private static final String[] DEPRECATED_QUERIES = new String[] { "common", "field_masking_span" }; /** * Dummy test {@link AggregationBuilder} used to test registering aggregation builders.