Rename field_masking_span to span_field_masking (#1606)

* Rename field_masking_span to span_field_masking

Signed-off-by: Xue Zhou <xuezhou@amazon.com>

* Update SearchModuleTests.java

Signed-off-by: Xue Zhou <xuezhou@amazon.com>

* Rename field_masking_span to span_field_masking

Signed-off-by: Xue Zhou <xuezhou@amazon.com>
This commit is contained in:
Xue Zhou 2021-11-29 19:01:43 +00:00 committed by GitHub
parent 84772904be
commit 74943c43c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 13 deletions

View File

@ -48,7 +48,7 @@ import java.io.IOException;
import java.util.Objects; import java.util.Objects;
public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder<FieldMaskingSpanQueryBuilder> implements SpanQueryBuilder { public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder<FieldMaskingSpanQueryBuilder> 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 FIELD_FIELD = new ParseField("field");
private static final ParseField QUERY_FIELD = new ParseField("query"); private static final ParseField QUERY_FIELD = new ParseField("query");
@ -105,7 +105,7 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder<FieldMask
@Override @Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException { protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME); builder.startObject(SPAN_FIELD_MASKING_FIELD.getPreferredName());
builder.field(QUERY_FIELD.getPreferredName()); builder.field(QUERY_FIELD.getPreferredName());
queryBuilder.toXContent(builder, params); queryBuilder.toXContent(builder, params);
builder.field(FIELD_FIELD.getPreferredName(), fieldName); builder.field(FIELD_FIELD.getPreferredName(), fieldName);
@ -129,13 +129,16 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder<FieldMask
if (QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { if (QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
QueryBuilder query = parseInnerQueryBuilder(parser); QueryBuilder query = parseInnerQueryBuilder(parser);
if (query instanceof SpanQueryBuilder == false) { 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; inner = (SpanQueryBuilder) query;
} else { } else {
throw new ParsingException( throw new ParsingException(
parser.getTokenLocation(), parser.getTokenLocation(),
"[field_masking_span] query does not support [" + currentFieldName + "]" "[" + SPAN_FIELD_MASKING_FIELD.getPreferredName() + "] query does not support [" + currentFieldName + "]"
); );
} }
} else { } else {
@ -148,16 +151,22 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder<FieldMask
} else { } else {
throw new ParsingException( throw new ParsingException(
parser.getTokenLocation(), parser.getTokenLocation(),
"[field_masking_span] query does not support [" + currentFieldName + "]" "[" + SPAN_FIELD_MASKING_FIELD.getPreferredName() + "] query does not support [" + currentFieldName + "]"
); );
} }
} }
} }
if (inner == null) { 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) { 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); FieldMaskingSpanQueryBuilder queryBuilder = new FieldMaskingSpanQueryBuilder(inner, field);
@ -190,6 +199,6 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder<FieldMask
@Override @Override
public String getWriteableName() { public String getWriteableName() {
return NAME; return SPAN_FIELD_MASKING_FIELD.getPreferredName();
} }
} }

View File

@ -1172,7 +1172,7 @@ public class SearchModule {
); );
registerQuery( registerQuery(
new QuerySpec<>( new QuerySpec<>(
FieldMaskingSpanQueryBuilder.NAME, FieldMaskingSpanQueryBuilder.SPAN_FIELD_MASKING_FIELD,
FieldMaskingSpanQueryBuilder::new, FieldMaskingSpanQueryBuilder::new,
FieldMaskingSpanQueryBuilder::fromXContent FieldMaskingSpanQueryBuilder::fromXContent
) )

View File

@ -38,6 +38,7 @@ import org.opensearch.test.AbstractQueryTestCase;
import java.io.IOException; 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.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.instanceOf;
@ -73,7 +74,9 @@ public class FieldMaskingSpanQueryBuilderTests extends AbstractQueryTestCase<Fie
public void testFromJson() throws IOException { public void testFromJson() throws IOException {
String json = "{\n" String json = "{\n"
+ " \"field_masking_span\" : {\n" + " \""
+ SPAN_FIELD_MASKING_FIELD.getPreferredName()
+ "\" : {\n"
+ " \"query\" : {\n" + " \"query\" : {\n"
+ " \"span_term\" : {\n" + " \"span_term\" : {\n"
+ " \"value\" : {\n" + " \"value\" : {\n"
@ -92,4 +95,25 @@ public class FieldMaskingSpanQueryBuilderTests extends AbstractQueryTestCase<Fie
assertEquals(json, 42.0, parsed.boost(), 0.00001); assertEquals(json, 42.0, parsed.boost(), 0.00001);
assertEquals(json, 0.23, parsed.innerQuery().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"
);
}
} }

View File

@ -337,7 +337,7 @@ public class SearchModuleTests extends OpenSearchTestCase {
Set<String> registeredNonDeprecated = module.getNamedXContents() Set<String> registeredNonDeprecated = module.getNamedXContents()
.stream() .stream()
.filter(e -> e.categoryClass.equals(QueryBuilder.class)) .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()) .map(e -> e.name.getPreferredName())
.collect(toSet()); .collect(toSet());
Set<String> registeredAll = module.getNamedXContents() Set<String> registeredAll = module.getNamedXContents()
@ -422,7 +422,6 @@ public class SearchModuleTests extends OpenSearchTestCase {
"constant_score", "constant_score",
"dis_max", "dis_max",
"exists", "exists",
"field_masking_span",
"function_score", "function_score",
"fuzzy", "fuzzy",
"geo_bounding_box", "geo_bounding_box",
@ -448,6 +447,7 @@ public class SearchModuleTests extends OpenSearchTestCase {
"script_score", "script_score",
"simple_query_string", "simple_query_string",
"span_containing", "span_containing",
"span_field_masking",
"span_first", "span_first",
"span_gap", "span_gap",
"span_multi", "span_multi",
@ -465,7 +465,7 @@ public class SearchModuleTests extends OpenSearchTestCase {
"distance_feature" }; "distance_feature" };
// add here deprecated queries to make sure we log a deprecation warnings when they are used // 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. * Dummy test {@link AggregationBuilder} used to test registering aggregation builders.