diff --git a/modules/elasticsearch/src/main/java/org/apache/lucene/search/PublicTermsFilter.java b/modules/elasticsearch/src/main/java/org/apache/lucene/search/PublicTermsFilter.java index 4a40da5ada1..13efb83cc37 100644 --- a/modules/elasticsearch/src/main/java/org/apache/lucene/search/PublicTermsFilter.java +++ b/modules/elasticsearch/src/main/java/org/apache/lucene/search/PublicTermsFilter.java @@ -25,18 +25,51 @@ import org.apache.lucene.index.TermDocs; import org.apache.lucene.util.OpenBitSet; import java.io.IOException; +import java.util.Iterator; import java.util.Set; +import java.util.TreeSet; /** * @author kimchy (shay.banon) */ -public class PublicTermsFilter extends TermsFilter { +// LUCENE MONITOR: Against TermsFilter +public class PublicTermsFilter extends Filter { + + Set terms = new TreeSet(); + + /** + * Adds a term to the list of acceptable terms + * + * @param term + */ + public void addTerm(Term term) { + terms.add(term); + } public Set getTerms() { return terms; } - // override default impl here to use fastSet... + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if ((obj == null) || (obj.getClass() != this.getClass())) + return false; + PublicTermsFilter test = (PublicTermsFilter) obj; + return (terms == test.terms || + (terms != null && terms.equals(test.terms))); + } + + @Override + public int hashCode() { + int hash = 9; + for (Iterator iter = terms.iterator(); iter.hasNext();) { + Term term = iter.next(); + hash = 31 * hash + term.hashCode(); + } + return hash; + } @Override public DocIdSet getDocIdSet(IndexReader reader) throws IOException { @@ -49,8 +82,7 @@ public class PublicTermsFilter extends TermsFilter { result.fastSet(td.doc()); } } - } - finally { + } finally { td.close(); } return result; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/MapperService.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/MapperService.java index 2134e544fd8..efefb24c928 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/MapperService.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/MapperService.java @@ -24,7 +24,7 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.document.Fieldable; import org.apache.lucene.index.Term; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.TermsFilter; +import org.apache.lucene.search.PublicTermsFilter; import org.elasticsearch.common.collect.ImmutableMap; import org.elasticsearch.common.collect.Sets; import org.elasticsearch.common.collect.UnmodifiableIterator; @@ -238,7 +238,7 @@ public class MapperService extends AbstractIndexComponent implements Iterable terms = (Set) field.get(termsFilter); - assertThat(terms.size(), equalTo(2)); - assertThat(terms.iterator().next().text(), equalTo("banon")); + assertThat(filteredQuery.getFilter(), instanceOf(PublicTermsFilter.class)); + PublicTermsFilter termsFilter = (PublicTermsFilter) filteredQuery.getFilter(); + assertThat(termsFilter.getTerms().size(), equalTo(2)); + assertThat(termsFilter.getTerms().iterator().next().text(), equalTo("banon")); } @@ -881,13 +877,10 @@ public class SimpleIndexQueryParserTests { Query parsedQuery = queryParser.parse(query).query(); assertThat(parsedQuery, instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; - assertThat(filteredQuery.getFilter(), instanceOf(TermsFilter.class)); - TermsFilter termsFilter = (TermsFilter) filteredQuery.getFilter(); - Field field = TermsFilter.class.getDeclaredField("terms"); - field.setAccessible(true); - Set terms = (Set) field.get(termsFilter); - assertThat(terms.size(), equalTo(2)); - assertThat(terms.iterator().next().text(), equalTo("banon")); + assertThat(filteredQuery.getFilter(), instanceOf(PublicTermsFilter.class)); + PublicTermsFilter termsFilter = (PublicTermsFilter) filteredQuery.getFilter(); + assertThat(termsFilter.getTerms().size(), equalTo(2)); + assertThat(termsFilter.getTerms().iterator().next().text(), equalTo("banon")); } @Test public void testTermsWithNameFilterQuery() throws Exception { @@ -897,13 +890,10 @@ public class SimpleIndexQueryParserTests { assertThat(parsedQuery.namedFilters().containsKey("test"), equalTo(true)); assertThat(parsedQuery.query(), instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery.query(); - assertThat(filteredQuery.getFilter(), instanceOf(TermsFilter.class)); - TermsFilter termsFilter = (TermsFilter) filteredQuery.getFilter(); - Field field = TermsFilter.class.getDeclaredField("terms"); - field.setAccessible(true); - Set terms = (Set) field.get(termsFilter); - assertThat(terms.size(), equalTo(2)); - assertThat(terms.iterator().next().text(), equalTo("banon")); + assertThat(filteredQuery.getFilter(), instanceOf(PublicTermsFilter.class)); + PublicTermsFilter termsFilter = (PublicTermsFilter) filteredQuery.getFilter(); + assertThat(termsFilter.getTerms().size(), equalTo(2)); + assertThat(termsFilter.getTerms().iterator().next().text(), equalTo("banon")); } @Test public void testConstantScoreQueryBuilder() throws IOException {