Skip to content

Commit

Permalink
Convert TermQuery to PrefixQuery if PHRASE_PREFIX is set
Browse files Browse the repository at this point in the history
We miss to add a single term to a prefix query if the query in only a
single term.

Closes #5551
  • Loading branch information
s1monw committed Mar 26, 2014
1 parent 3fd89be commit a12a36e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/main/java/org/elasticsearch/index/search/MatchQuery.java
Expand Up @@ -251,28 +251,28 @@ protected Query newTermQuery(Term term) {


public Query createPhrasePrefixQuery(String field, String queryText, int phraseSlop, int maxExpansions) {
Query query = createFieldQuery(getAnalyzer(), Occur.MUST, field, queryText, true, phraseSlop);
final Query query = createFieldQuery(getAnalyzer(), Occur.MUST, field, queryText, true, phraseSlop);
final MultiPhrasePrefixQuery prefixQuery = new MultiPhrasePrefixQuery();
prefixQuery.setMaxExpansions(maxExpansions);
prefixQuery.setSlop(phraseSlop);
if (query instanceof PhraseQuery) {
PhraseQuery pq = (PhraseQuery)query;
MultiPhrasePrefixQuery prefixQuery = new MultiPhrasePrefixQuery();
prefixQuery.setMaxExpansions(maxExpansions);
Term[] terms = pq.getTerms();
int[] positions = pq.getPositions();
for (int i = 0; i < terms.length; i++) {
prefixQuery.add(new Term[] {terms[i]}, positions[i]);
}
prefixQuery.setSlop(phraseSlop);
return prefixQuery;
} else if (query instanceof MultiPhraseQuery) {
MultiPhraseQuery pq = (MultiPhraseQuery)query;
MultiPhrasePrefixQuery prefixQuery = new MultiPhrasePrefixQuery();
prefixQuery.setMaxExpansions(maxExpansions);
List<Term[]> terms = pq.getTermArrays();
int[] positions = pq.getPositions();
for (int i = 0; i < terms.size(); i++) {
prefixQuery.add(terms.get(i), positions[i]);
}
prefixQuery.setSlop(phraseSlop);
return prefixQuery;
} else if (query instanceof TermQuery) {
prefixQuery.add(((TermQuery) query).getTerm());
return prefixQuery;
}
return query;
Expand Down
Expand Up @@ -2295,11 +2295,18 @@ public void testNGramCopyField() {
public void testMatchPhrasePrefixQuery() {
createIndex("test1");
client().prepareIndex("test1", "type1", "1").setSource("field", "Johnnie Walker Black Label").get();
client().prepareIndex("test1", "type1", "2").setSource("field", "trying out Elasticsearch").get();
refresh();

SearchResponse searchResponse = client().prepareSearch().setQuery(matchQuery("field", "Johnnie la").slop(between(2,5)).type(Type.PHRASE_PREFIX)).get();
assertHitCount(searchResponse, 1l);
assertSearchHits(searchResponse, "1");
searchResponse = client().prepareSearch().setQuery(matchQuery("field", "trying").type(Type.PHRASE_PREFIX)).get();
assertHitCount(searchResponse, 1l);
assertSearchHits(searchResponse, "2");
searchResponse = client().prepareSearch().setQuery(matchQuery("field", "try").type(Type.PHRASE_PREFIX)).get();
assertHitCount(searchResponse, 1l);
assertSearchHits(searchResponse, "2");
}

}

0 comments on commit a12a36e

Please sign in to comment.