Fix match_phrase_prefix query with single term on _all field (#20471)

* Fix match_phrase_prefix query with single term on _all field

This change fixes the match_phrase_prefix query when a single term is queried on the _all field.
It builds a prefix query instead of an AllTermQuery which would not match any prefix.

Fixes #20470

* Add missing change
This commit is contained in:
Jim Ferenczi 2016-10-10 09:47:26 +02:00 committed by GitHub
parent 8ab7ca5284
commit 767a7e2329
3 changed files with 21 additions and 1 deletions

View File

@ -64,6 +64,10 @@ public final class AllTermQuery extends Query {
this.term = term; this.term = term;
} }
public Term getTerm() {
return term;
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (sameClassAs(obj) == false) { if (sameClassAs(obj) == false) {

View File

@ -37,6 +37,7 @@ import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.lucene.all.AllTermQuery;
import org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery; import org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery;
import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.unit.Fuzziness;
@ -324,6 +325,9 @@ public class MatchQuery {
} else if (query instanceof TermQuery) { } else if (query instanceof TermQuery) {
prefixQuery.add(((TermQuery) query).getTerm()); prefixQuery.add(((TermQuery) query).getTerm());
return prefixQuery; return prefixQuery;
} else if (query instanceof AllTermQuery) {
prefixQuery.add(((AllTermQuery) query).getTerm());
return prefixQuery;
} }
return query; return query;
} }

View File

@ -22,15 +22,16 @@ package org.elasticsearch.index.search;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.queries.BlendedTermQuery; import org.apache.lucene.queries.BlendedTermQuery;
import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BoostQuery; import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.DisjunctionMaxQuery; import org.apache.lucene.search.DisjunctionMaxQuery;
import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery;
import org.elasticsearch.index.IndexService; import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.engine.Engine;
import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.MapperService;
@ -45,6 +46,8 @@ import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery; import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
public class MultiMatchQueryTests extends ESSingleNodeTestCase { public class MultiMatchQueryTests extends ESSingleNodeTestCase {
@ -154,4 +157,13 @@ public class MultiMatchQueryTests extends ESSingleNodeTestCase {
Query actual = MultiMatchQuery.blendTerm(new BytesRef("baz"), null, 1f, new FieldAndFieldType(ft1, 2), new FieldAndFieldType(ft2, 3)); Query actual = MultiMatchQuery.blendTerm(new BytesRef("baz"), null, 1f, new FieldAndFieldType(ft1, 2), new FieldAndFieldType(ft2, 3));
assertEquals(expected, actual); assertEquals(expected, actual);
} }
public void testMultiMatchPrefixWithAllField() throws IOException {
QueryShardContext queryShardContext = indexService.newQueryShardContext();
queryShardContext.setAllowUnmappedFields(true);
Query parsedQuery =
multiMatchQuery("foo").field("_all").type(MultiMatchQueryBuilder.Type.PHRASE_PREFIX).toQuery(queryShardContext);
assertThat(parsedQuery, instanceOf(MultiPhrasePrefixQuery.class));
assertThat(parsedQuery.toString(), equalTo("_all:\"foo*\""));
}
} }