From 107635b194c5bd727734ebd3fd4ff153b6a49305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Tue, 16 Jun 2015 17:58:49 +0200 Subject: [PATCH] Query refactoring: FieldMaskingSpanQueryBuilder and Parser Moving the query building functionality from the parser to the builders new toQuery() method analogous to other recent query refactorings. Relates to #10217 Closes #11717 --- .../query/FieldMaskingSpanQueryBuilder.java | 123 ++++++++++++++++-- .../query/FieldMaskingSpanQueryParser.java | 33 ++--- .../FieldMaskingSpanQueryBuilderTest.java | 94 +++++++++++++ 3 files changed, 214 insertions(+), 36 deletions(-) create mode 100644 core/src/test/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilderTest.java 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)); + } +}