SpanNearQueryBuilder should return the inner clause when a single clause is provided (#25856)
This change handles the case where a SpanNearQueryBuilder tries to create a query with a single clause. This is not allowed in the SpanNearQuery so instead of throwing an exception when the weight is built, this change builds and returns the singleton inner clause on toQuery. Fixes #25630
This commit is contained in:
parent
93b04fb7bd
commit
d73e17c103
|
@ -203,6 +203,11 @@ public class SpanNearQueryBuilder extends AbstractQueryBuilder<SpanNearQueryBuil
|
|||
|
||||
@Override
|
||||
protected Query doToQuery(QueryShardContext context) throws IOException {
|
||||
if (clauses.size() == 1) {
|
||||
Query query = clauses.get(0).toQuery(context);
|
||||
assert query instanceof SpanQuery;
|
||||
return query;
|
||||
}
|
||||
SpanQuery[] spanQueries = new SpanQuery[clauses.size()];
|
||||
for (int i = 0; i < clauses.size(); i++) {
|
||||
Query query = clauses.get(i).toQuery(context);
|
||||
|
|
|
@ -20,8 +20,10 @@
|
|||
package org.elasticsearch.index.query;
|
||||
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.spans.SpanBoostQuery;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.elasticsearch.common.ParsingException;
|
||||
import org.elasticsearch.search.internal.SearchContext;
|
||||
import org.elasticsearch.test.AbstractQueryTestCase;
|
||||
|
@ -30,6 +32,7 @@ import java.io.IOException;
|
|||
import java.util.Iterator;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.either;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
|
||||
|
@ -47,14 +50,22 @@ public class SpanNearQueryBuilderTests extends AbstractQueryTestCase<SpanNearQue
|
|||
|
||||
@Override
|
||||
protected void doAssertLuceneQuery(SpanNearQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
|
||||
assertThat(query, instanceOf(SpanNearQuery.class));
|
||||
SpanNearQuery spanNearQuery = (SpanNearQuery) query;
|
||||
assertThat(spanNearQuery.getSlop(), equalTo(queryBuilder.slop()));
|
||||
assertThat(spanNearQuery.isInOrder(), equalTo(queryBuilder.inOrder()));
|
||||
assertThat(spanNearQuery.getClauses().length, equalTo(queryBuilder.clauses().size()));
|
||||
Iterator<SpanQueryBuilder> spanQueryBuilderIterator = queryBuilder.clauses().iterator();
|
||||
for (SpanQuery spanQuery : spanNearQuery.getClauses()) {
|
||||
assertThat(spanQuery, equalTo(spanQueryBuilderIterator.next().toQuery(context.getQueryShardContext())));
|
||||
assertThat(query, either(instanceOf(SpanNearQuery.class))
|
||||
.or(instanceOf(SpanTermQuery.class))
|
||||
.or(instanceOf(SpanBoostQuery.class))
|
||||
.or(instanceOf(MatchAllQueryBuilder.class)));
|
||||
if (query instanceof SpanNearQuery) {
|
||||
SpanNearQuery spanNearQuery = (SpanNearQuery) query;
|
||||
assertThat(spanNearQuery.getSlop(), equalTo(queryBuilder.slop()));
|
||||
assertThat(spanNearQuery.isInOrder(), equalTo(queryBuilder.inOrder()));
|
||||
assertThat(spanNearQuery.getClauses().length, equalTo(queryBuilder.clauses().size()));
|
||||
Iterator<SpanQueryBuilder> spanQueryBuilderIterator = queryBuilder.clauses().iterator();
|
||||
for (SpanQuery spanQuery : spanNearQuery.getClauses()) {
|
||||
assertThat(spanQuery, equalTo(spanQueryBuilderIterator.next().toQuery(context.getQueryShardContext())));
|
||||
}
|
||||
} else if (query instanceof SpanTermQuery || query instanceof SpanBoostQuery) {
|
||||
assertThat(queryBuilder.clauses().size(), equalTo(1));
|
||||
assertThat(query, equalTo(queryBuilder.clauses().get(0).toQuery(context.getQueryShardContext())));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue