diff --git a/core/src/main/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilder.java index c1e20e5b5c4..c783682a3e5 100644 --- a/core/src/main/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilder.java @@ -19,10 +19,15 @@ package org.elasticsearch.index.query; +import org.apache.lucene.search.spans.FieldMaskingSpanQuery; import org.apache.lucene.search.spans.SpanQuery; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.index.mapper.MappedFieldType; import java.io.IOException; +import java.util.Objects; public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder implements SpanQueryBuilder, BoostableQueryBuilder { @@ -30,17 +35,37 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder { + + @Override + protected Query createExpectedQuery(FieldMaskingSpanQueryBuilder testQueryBuilder, QueryParseContext context) throws IOException { + String fieldInQuery = testQueryBuilder.fieldName(); + MappedFieldType fieldType = context.fieldMapper(fieldInQuery); + if (fieldType != null) { + fieldInQuery = fieldType.names().indexName(); + } + SpanQuery innerQuery = testQueryBuilder.innerQuery().toQuery(context); + + Query expectedQuery = new FieldMaskingSpanQuery(innerQuery, fieldInQuery); + expectedQuery.setBoost(testQueryBuilder.boost()); + if (testQueryBuilder.queryName() != null) { + context.addNamedQuery(testQueryBuilder.queryName(), expectedQuery); + } + return expectedQuery; + } + + @Override + protected void assertLuceneQuery(FieldMaskingSpanQueryBuilder queryBuilder, Query query, QueryParseContext context) { + if (queryBuilder.queryName() != null) { + Query namedQuery = context.copyNamedFilters().get(queryBuilder.queryName()); + assertThat(namedQuery, equalTo(query)); + } + } + + @Override + protected FieldMaskingSpanQueryBuilder createTestQueryBuilder() { + String fieldName = null; + if (randomBoolean()) { + fieldName = randomFrom(mappedFieldNames); + } else { + fieldName = randomAsciiOfLengthBetween(1, 10); + } + SpanTermQueryBuilder innerQuery = new SpanTermQueryBuilderTest().createTestQueryBuilder(); + FieldMaskingSpanQueryBuilder query = new FieldMaskingSpanQueryBuilder(innerQuery, fieldName); + if (randomBoolean()) { + query.boost(2.0f / randomIntBetween(1, 20)); + } + if (randomBoolean()) { + query.queryName(randomAsciiOfLengthBetween(1, 10)); + } + return query; + } + + @Test + public void testValidate() { + FieldMaskingSpanQueryBuilder queryBuilder = new FieldMaskingSpanQueryBuilder(new SpanTermQueryBuilder("name", "value"), "fieldName"); + assertNull(queryBuilder.validate()); + + queryBuilder = new FieldMaskingSpanQueryBuilder(null, "fieldName"); + assertThat(queryBuilder.validate().validationErrors().size(), is(1)); + + queryBuilder = new FieldMaskingSpanQueryBuilder(null, ""); + assertThat(queryBuilder.validate().validationErrors().size(), is(2)); + + queryBuilder = new FieldMaskingSpanQueryBuilder(null, null); + assertThat(queryBuilder.validate().validationErrors().size(), is(2)); + } +}