Skip to content

Commit

Permalink
add proper testing for bool filter
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Mar 2, 2013
1 parent 361d6bf commit ea097af
Showing 1 changed file with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.lucene.index.Term;
import org.apache.lucene.queries.BoostingQuery;
import org.apache.lucene.queries.FilterClause;
import org.apache.lucene.queries.TermsFilter;
import org.apache.lucene.sandbox.queries.FuzzyLikeThisQuery;
import org.apache.lucene.search.*;
Expand Down Expand Up @@ -67,6 +68,7 @@

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.List;

import static org.elasticsearch.common.io.Streams.copyToBytesFromClasspath;
Expand Down Expand Up @@ -902,6 +904,40 @@ public void testNumericRangeFilteredQuery() throws IOException {
assertThat(rangeFilter.isIncludeUpper(), equalTo(false));
}

@Test
public void testBoolFilteredQueryBuilder() throws IOException {
IndexQueryParserService queryParser = queryParser();
Query parsedQuery = queryParser.parse(filteredQuery(termQuery("name.first", "shay"), boolFilter().must(termFilter("name.first", "shay1"), termFilter("name.first", "shay4")).mustNot(termFilter("name.first", "shay2")).should(termFilter("name.first", "shay3")))).query();

assertThat(parsedQuery, instanceOf(XFilteredQuery.class));
XFilteredQuery filteredQuery = (XFilteredQuery) parsedQuery;
XBooleanFilter booleanFilter = (XBooleanFilter) filteredQuery.getFilter();

Iterator<FilterClause> iterator = booleanFilter.iterator();
assertThat(iterator.hasNext(), equalTo(true));
FilterClause clause = iterator.next();
assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.MUST));
assertThat(((TermFilter) clause.getFilter()).getTerm(), equalTo(new Term("name.first", "shay1")));

assertThat(iterator.hasNext(), equalTo(true));
clause = iterator.next();
assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.MUST));
assertThat(((TermFilter) clause.getFilter()).getTerm(), equalTo(new Term("name.first", "shay4")));

assertThat(iterator.hasNext(), equalTo(true));
clause = iterator.next();
assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.MUST_NOT));
assertThat(((TermFilter) clause.getFilter()).getTerm(), equalTo(new Term("name.first", "shay2")));

assertThat(iterator.hasNext(), equalTo(true));
clause = iterator.next();
assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.SHOULD));
assertThat(((TermFilter) clause.getFilter()).getTerm(), equalTo(new Term("name.first", "shay3")));

assertThat(iterator.hasNext(), equalTo(false));
}


@Test
public void testBoolFilteredQuery() throws IOException {
IndexQueryParserService queryParser = queryParser();
Expand All @@ -911,7 +947,28 @@ public void testBoolFilteredQuery() throws IOException {
XFilteredQuery filteredQuery = (XFilteredQuery) parsedQuery;
XBooleanFilter booleanFilter = (XBooleanFilter) filteredQuery.getFilter();

// TODO get the content and test
Iterator<FilterClause> iterator = booleanFilter.iterator();
assertThat(iterator.hasNext(), equalTo(true));
FilterClause clause = iterator.next();
assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.MUST));
assertThat(((TermFilter) clause.getFilter()).getTerm(), equalTo(new Term("name.first", "shay1")));

assertThat(iterator.hasNext(), equalTo(true));
clause = iterator.next();
assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.MUST));
assertThat(((TermFilter) clause.getFilter()).getTerm(), equalTo(new Term("name.first", "shay4")));

assertThat(iterator.hasNext(), equalTo(true));
clause = iterator.next();
assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.MUST_NOT));
assertThat(((TermFilter) clause.getFilter()).getTerm(), equalTo(new Term("name.first", "shay2")));

assertThat(iterator.hasNext(), equalTo(true));
clause = iterator.next();
assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.SHOULD));
assertThat(((TermFilter) clause.getFilter()).getTerm(), equalTo(new Term("name.first", "shay3")));

assertThat(iterator.hasNext(), equalTo(false));
}

@Test
Expand Down

0 comments on commit ea097af

Please sign in to comment.