diff --git a/core/src/main/java/org/elasticsearch/index/percolator/ExtractQueryTermsService.java b/core/src/main/java/org/elasticsearch/index/percolator/ExtractQueryTermsService.java index 8ee9d55c312..a8669c98cd8 100644 --- a/core/src/main/java/org/elasticsearch/index/percolator/ExtractQueryTermsService.java +++ b/core/src/main/java/org/elasticsearch/index/percolator/ExtractQueryTermsService.java @@ -23,6 +23,7 @@ import org.apache.lucene.document.FieldType; import org.apache.lucene.index.Fields; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.MultiFields; +import org.apache.lucene.index.PrefixCodedTerms; import org.apache.lucene.index.Term; import org.apache.lucene.index.Terms; import org.apache.lucene.index.TermsEnum; @@ -92,10 +93,17 @@ public final class ExtractQueryTermsService { * an UnsupportedQueryException is thrown. */ static Set extractQueryTerms(Query query) { - // TODO: add support for the TermsQuery when it has methods to access the actual terms it encapsulates // TODO: add support for span queries if (query instanceof TermQuery) { return Collections.singleton(((TermQuery) query).getTerm()); + } else if (query instanceof TermsQuery) { + Set terms = new HashSet<>(); + TermsQuery termsQuery = (TermsQuery) query; + PrefixCodedTerms.TermIterator iterator = termsQuery.getTermData().iterator(); + for (BytesRef term = iterator.next(); term != null; term = iterator.next()) { + terms.add(new Term(iterator.field(), term)); + } + return terms; } else if (query instanceof PhraseQuery) { Term[] terms = ((PhraseQuery) query).getTerms(); if (terms.length == 0) { diff --git a/core/src/test/java/org/elasticsearch/index/percolator/ExtractQueryTermsServiceTests.java b/core/src/test/java/org/elasticsearch/index/percolator/ExtractQueryTermsServiceTests.java index f23ec6d9595..f17a4fc664d 100644 --- a/core/src/test/java/org/elasticsearch/index/percolator/ExtractQueryTermsServiceTests.java +++ b/core/src/test/java/org/elasticsearch/index/percolator/ExtractQueryTermsServiceTests.java @@ -39,6 +39,7 @@ import org.elasticsearch.test.ESTestCase; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -102,6 +103,27 @@ public class ExtractQueryTermsServiceTests extends ESTestCase { assertThat(terms.get(0).bytes(), equalTo(termQuery.getTerm().bytes())); } + public void testExtractQueryMetadata_termsQuery() { + TermsQuery termsQuery = new TermsQuery("_field", new BytesRef("_term1"), new BytesRef("_term2")); + List terms = new ArrayList<>(ExtractQueryTermsService.extractQueryTerms(termsQuery)); + Collections.sort(terms); + assertThat(terms.size(), equalTo(2)); + assertThat(terms.get(0).field(), equalTo("_field")); + assertThat(terms.get(0).text(), equalTo("_term1")); + assertThat(terms.get(1).field(), equalTo("_field")); + assertThat(terms.get(1).text(), equalTo("_term2")); + + // test with different fields + termsQuery = new TermsQuery(new Term("_field1", "_term1"), new Term("_field2", "_term2")); + terms = new ArrayList<>(ExtractQueryTermsService.extractQueryTerms(termsQuery)); + Collections.sort(terms); + assertThat(terms.size(), equalTo(2)); + assertThat(terms.get(0).field(), equalTo("_field1")); + assertThat(terms.get(0).text(), equalTo("_term1")); + assertThat(terms.get(1).field(), equalTo("_field2")); + assertThat(terms.get(1).text(), equalTo("_term2")); + } + public void testExtractQueryMetadata_phraseQuery() { PhraseQuery phraseQuery = new PhraseQuery("_field", "_term1", "term2"); List terms = new ArrayList<>(ExtractQueryTermsService.extractQueryTerms(phraseQuery));