use public terms filter in all places

This commit is contained in:
kimchy 2010-12-21 23:43:05 +02:00
parent a1df3c637c
commit 59ee8493ce
4 changed files with 51 additions and 30 deletions

View File

@ -25,18 +25,51 @@ import org.apache.lucene.index.TermDocs;
import org.apache.lucene.util.OpenBitSet; import org.apache.lucene.util.OpenBitSet;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator;
import java.util.Set; import java.util.Set;
import java.util.TreeSet;
/** /**
* @author kimchy (shay.banon) * @author kimchy (shay.banon)
*/ */
public class PublicTermsFilter extends TermsFilter { // LUCENE MONITOR: Against TermsFilter
public class PublicTermsFilter extends Filter {
Set<Term> terms = new TreeSet<Term>();
/**
* Adds a term to the list of acceptable terms
*
* @param term
*/
public void addTerm(Term term) {
terms.add(term);
}
public Set<Term> getTerms() { public Set<Term> getTerms() {
return terms; 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<Term> iter = terms.iterator(); iter.hasNext();) {
Term term = iter.next();
hash = 31 * hash + term.hashCode();
}
return hash;
}
@Override @Override
public DocIdSet getDocIdSet(IndexReader reader) throws IOException { public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
@ -49,8 +82,7 @@ public class PublicTermsFilter extends TermsFilter {
result.fastSet(td.doc()); result.fastSet(td.doc());
} }
} }
} } finally {
finally {
td.close(); td.close();
} }
return result; return result;

View File

@ -24,7 +24,7 @@ import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.search.Filter; 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.ImmutableMap;
import org.elasticsearch.common.collect.Sets; import org.elasticsearch.common.collect.Sets;
import org.elasticsearch.common.collect.UnmodifiableIterator; import org.elasticsearch.common.collect.UnmodifiableIterator;
@ -238,7 +238,7 @@ public class MapperService extends AbstractIndexComponent implements Iterable<Do
if (types.length == 1) { if (types.length == 1) {
return documentMapper(types[0]).typeFilter(); return documentMapper(types[0]).typeFilter();
} }
TermsFilter termsFilter = new TermsFilter(); PublicTermsFilter termsFilter = new PublicTermsFilter();
for (String type : types) { for (String type : types) {
termsFilter.addTerm(new Term(TypeFieldMapper.NAME, type)); termsFilter.addTerm(new Term(TypeFieldMapper.NAME, type));
} }

View File

@ -22,7 +22,6 @@ package org.elasticsearch.index.query.xcontent;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.apache.lucene.search.PublicTermsFilter; import org.apache.lucene.search.PublicTermsFilter;
import org.apache.lucene.search.TermsFilter;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
@ -57,7 +56,7 @@ public class TermsFilterParser extends AbstractIndexComponent implements XConten
MapperService.SmartNameFieldMappers smartNameFieldMappers = null; MapperService.SmartNameFieldMappers smartNameFieldMappers = null;
boolean cache = true; boolean cache = true;
TermsFilter termsFilter = new PublicTermsFilter(); PublicTermsFilter termsFilter = new PublicTermsFilter();
String filterName = null; String filterName = null;
String currentFieldName = null; String currentFieldName = null;
XContentParser.Token token; XContentParser.Token token;

View File

@ -56,7 +56,6 @@ import org.testng.annotations.Test;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.List; import java.util.List;
import java.util.Set;
import static org.elasticsearch.common.io.Streams.*; import static org.elasticsearch.common.io.Streams.*;
import static org.elasticsearch.index.query.xcontent.FilterBuilders.*; import static org.elasticsearch.index.query.xcontent.FilterBuilders.*;
@ -865,13 +864,10 @@ public class SimpleIndexQueryParserTests {
Query parsedQuery = queryParser.parse(filteredQuery(termQuery("name.first", "shay"), termsFilter("name.last", "banon", "kimchy"))).query(); Query parsedQuery = queryParser.parse(filteredQuery(termQuery("name.first", "shay"), termsFilter("name.last", "banon", "kimchy"))).query();
assertThat(parsedQuery, instanceOf(FilteredQuery.class)); assertThat(parsedQuery, instanceOf(FilteredQuery.class));
FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; FilteredQuery filteredQuery = (FilteredQuery) parsedQuery;
assertThat(filteredQuery.getFilter(), instanceOf(TermsFilter.class)); assertThat(filteredQuery.getFilter(), instanceOf(PublicTermsFilter.class));
TermsFilter termsFilter = (TermsFilter) filteredQuery.getFilter(); PublicTermsFilter termsFilter = (PublicTermsFilter) filteredQuery.getFilter();
Field field = TermsFilter.class.getDeclaredField("terms"); assertThat(termsFilter.getTerms().size(), equalTo(2));
field.setAccessible(true); assertThat(termsFilter.getTerms().iterator().next().text(), equalTo("banon"));
Set<Term> terms = (Set<Term>) field.get(termsFilter);
assertThat(terms.size(), equalTo(2));
assertThat(terms.iterator().next().text(), equalTo("banon"));
} }
@ -881,13 +877,10 @@ public class SimpleIndexQueryParserTests {
Query parsedQuery = queryParser.parse(query).query(); Query parsedQuery = queryParser.parse(query).query();
assertThat(parsedQuery, instanceOf(FilteredQuery.class)); assertThat(parsedQuery, instanceOf(FilteredQuery.class));
FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; FilteredQuery filteredQuery = (FilteredQuery) parsedQuery;
assertThat(filteredQuery.getFilter(), instanceOf(TermsFilter.class)); assertThat(filteredQuery.getFilter(), instanceOf(PublicTermsFilter.class));
TermsFilter termsFilter = (TermsFilter) filteredQuery.getFilter(); PublicTermsFilter termsFilter = (PublicTermsFilter) filteredQuery.getFilter();
Field field = TermsFilter.class.getDeclaredField("terms"); assertThat(termsFilter.getTerms().size(), equalTo(2));
field.setAccessible(true); assertThat(termsFilter.getTerms().iterator().next().text(), equalTo("banon"));
Set<Term> terms = (Set<Term>) field.get(termsFilter);
assertThat(terms.size(), equalTo(2));
assertThat(terms.iterator().next().text(), equalTo("banon"));
} }
@Test public void testTermsWithNameFilterQuery() throws Exception { @Test public void testTermsWithNameFilterQuery() throws Exception {
@ -897,13 +890,10 @@ public class SimpleIndexQueryParserTests {
assertThat(parsedQuery.namedFilters().containsKey("test"), equalTo(true)); assertThat(parsedQuery.namedFilters().containsKey("test"), equalTo(true));
assertThat(parsedQuery.query(), instanceOf(FilteredQuery.class)); assertThat(parsedQuery.query(), instanceOf(FilteredQuery.class));
FilteredQuery filteredQuery = (FilteredQuery) parsedQuery.query(); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery.query();
assertThat(filteredQuery.getFilter(), instanceOf(TermsFilter.class)); assertThat(filteredQuery.getFilter(), instanceOf(PublicTermsFilter.class));
TermsFilter termsFilter = (TermsFilter) filteredQuery.getFilter(); PublicTermsFilter termsFilter = (PublicTermsFilter) filteredQuery.getFilter();
Field field = TermsFilter.class.getDeclaredField("terms"); assertThat(termsFilter.getTerms().size(), equalTo(2));
field.setAccessible(true); assertThat(termsFilter.getTerms().iterator().next().text(), equalTo("banon"));
Set<Term> terms = (Set<Term>) field.get(termsFilter);
assertThat(terms.size(), equalTo(2));
assertThat(terms.iterator().next().text(), equalTo("banon"));
} }
@Test public void testConstantScoreQueryBuilder() throws IOException { @Test public void testConstantScoreQueryBuilder() throws IOException {