Skip to content

Commit

Permalink
Rename field_masking_span to span_field_masking (opensearch-project#1606
Browse files Browse the repository at this point in the history
) (opensearch-project#1623)

* Rename field_masking_span to span_field_masking

Signed-off-by: Xue Zhou <[email protected]>

* Update SearchModuleTests.java

Signed-off-by: Xue Zhou <[email protected]>

* Rename field_masking_span to span_field_masking

Signed-off-by: Xue Zhou <[email protected]>
  • Loading branch information
xuezhou25 authored Nov 30, 2021
1 parent c0f49b2 commit f9ee754
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import java.util.Objects;

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 QUERY_FIELD = new ParseField("query");
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand All @@ -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);
Expand Down Expand Up @@ -190,6 +199,6 @@ protected boolean doEquals(FieldMaskingSpanQueryBuilder other) {

@Override
public String getWriteableName() {
return NAME;
return SPAN_FIELD_MASKING_FIELD.getPreferredName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ private void registerQueryParsers(List<SearchPlugin> plugins) {
);
registerQuery(
new QuerySpec<>(
FieldMaskingSpanQueryBuilder.NAME,
FieldMaskingSpanQueryBuilder.SPAN_FIELD_MASKING_FIELD,
FieldMaskingSpanQueryBuilder::new,
FieldMaskingSpanQueryBuilder::fromXContent
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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"
Expand All @@ -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"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public void testRegisteredQueries() {
Set<String> 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<String> registeredAll = module.getNamedXContents()
Expand Down Expand Up @@ -422,7 +422,6 @@ public List<RescorerSpec<?>> getRescorers() {
"constant_score",
"dis_max",
"exists",
"field_masking_span",
"function_score",
"fuzzy",
"geo_bounding_box",
Expand All @@ -448,6 +447,7 @@ public List<RescorerSpec<?>> getRescorers() {
"script_score",
"simple_query_string",
"span_containing",
"span_field_masking",
"span_first",
"span_gap",
"span_multi",
Expand All @@ -465,7 +465,7 @@ public List<RescorerSpec<?>> 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.
Expand Down

0 comments on commit f9ee754

Please sign in to comment.