percolator: Add support for the synonym query.

This commit is contained in:
Martijn van Groningen 2016-06-24 18:12:44 +02:00
parent bfb19676a6
commit 9a0ce62550
2 changed files with 22 additions and 0 deletions

View File

@ -37,6 +37,7 @@ import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.DisjunctionMaxQuery;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.SynonymQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.spans.SpanFirstQuery;
import org.apache.lucene.search.spans.SpanNearQuery;
@ -90,6 +91,7 @@ public final class ExtractQueryTermsService {
map.put(SpanNotQuery.class, spanNotQuery());
map.put(BooleanQuery.class, booleanQuery());
map.put(DisjunctionMaxQuery.class, disjunctionMaxQuery());
map.put(SynonymQuery.class, synonymQuery());
queryProcessors = Collections.unmodifiableMap(map);
}
@ -221,6 +223,13 @@ public final class ExtractQueryTermsService {
};
}
static Function<Query, Result> synonymQuery() {
return query -> {
Set<Term> terms = new HashSet<>(((SynonymQuery) query).getTerms());
return new Result(true, terms);
};
}
static Function<Query, Result> commonTermsQuery() {
return query -> {
List<Term> terms = ((CommonTermsQuery) query).getTerms();

View File

@ -36,6 +36,7 @@ import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.DisjunctionMaxQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.SynonymQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.search.spans.SpanFirstQuery;
@ -558,6 +559,18 @@ public class ExtractQueryTermsServiceTests extends ESTestCase {
assertThat(terms.get(3).bytes(), equalTo(termQuery4.getTerm().bytes()));
}
public void testSynonymQuery() {
SynonymQuery query = new SynonymQuery();
Result result = extractQueryTerms(query);
assertThat(result.verified, is(true));
assertThat(result.terms.isEmpty(), is(true));
query = new SynonymQuery(new Term("_field", "_value1"), new Term("_field", "_value2"));
result = extractQueryTerms(query);
assertThat(result.verified, is(true));
assertTermsEqual(result.terms, new Term("_field", "_value1"), new Term("_field", "_value2"));
}
public void testCreateQueryMetadataQuery() throws Exception {
MemoryIndex memoryIndex = new MemoryIndex(false);
memoryIndex.addField("field1", "the quick brown fox jumps over the lazy dog", new WhitespaceAnalyzer());