BooleanFilterTestpublic class BooleanFilterTest extends TestCase
Fields Summary |
---|
private RAMDirectory | directory | private IndexReader | reader |
Methods Summary |
---|
private void | addDoc(org.apache.lucene.index.IndexWriter writer, java.lang.String accessRights, java.lang.String price, java.lang.String date, java.lang.String inStock)
Document doc=new Document();
doc.add(new Field("accessRights",accessRights,Field.Store.YES,Field.Index.TOKENIZED));
doc.add(new Field("price",price,Field.Store.YES,Field.Index.TOKENIZED));
doc.add(new Field("date",date,Field.Store.YES,Field.Index.TOKENIZED));
doc.add(new Field("inStock",inStock,Field.Store.YES,Field.Index.TOKENIZED));
writer.addDocument(doc);
| private org.apache.lucene.search.Filter | getRangeFilter(java.lang.String field, java.lang.String lowerPrice, java.lang.String upperPrice)
return new RangeFilter(field,lowerPrice,upperPrice,true,true);
| private TermsFilter | getTermsFilter(java.lang.String field, java.lang.String text)
TermsFilter tf=new TermsFilter();
tf.addTerm(new Term(field,text));
return tf;
| protected void | setUp()
directory = new RAMDirectory();
IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
//Add series of docs with filterable fields : acces rights, prices, dates and "in-stock" flags
addDoc(writer, "admin guest", "010", "20040101","Y");
addDoc(writer, "guest", "020", "20040101","Y");
addDoc(writer, "guest", "020", "20050101","Y");
addDoc(writer, "admin", "020", "20050101","Maybe");
addDoc(writer, "admin guest", "030", "20050101","N");
writer.close();
reader=IndexReader.open(directory);
| public void | testJustMust()
BooleanFilter booleanFilter = new BooleanFilter();
booleanFilter.add(new FilterClause(getTermsFilter("accessRights", "admin"),BooleanClause.Occur.MUST));
BitSet bits = booleanFilter.bits(reader);
assertEquals("MUST",3,bits.cardinality());
| public void | testJustMustNot()
BooleanFilter booleanFilter = new BooleanFilter();
booleanFilter.add(new FilterClause(getTermsFilter("inStock","N"),BooleanClause.Occur.MUST_NOT));
BitSet bits = booleanFilter.bits(reader);
assertEquals("MUST_NOT",4,bits.cardinality());
| public void | testMustAndMustNot()
BooleanFilter booleanFilter = new BooleanFilter();
booleanFilter.add(new FilterClause(getTermsFilter("inStock","N"),BooleanClause.Occur.MUST));
booleanFilter.add(new FilterClause(getTermsFilter("price","030"),BooleanClause.Occur.MUST_NOT));
BitSet bits = booleanFilter.bits(reader);
assertEquals("MUST_NOT wins over MUST for same docs",0,bits.cardinality());
| public void | testShould()
BooleanFilter booleanFilter = new BooleanFilter();
booleanFilter.add(new FilterClause(getTermsFilter("price","030"),BooleanClause.Occur.SHOULD));
BitSet bits = booleanFilter.bits(reader);
assertEquals("Should retrieves only 1 doc",1,bits.cardinality());
| public void | testShoulds()
BooleanFilter booleanFilter = new BooleanFilter();
booleanFilter.add(new FilterClause(getRangeFilter("price","010", "020"),BooleanClause.Occur.SHOULD));
booleanFilter.add(new FilterClause(getRangeFilter("price","020", "030"),BooleanClause.Occur.SHOULD));
BitSet bits = booleanFilter.bits(reader);
assertEquals("Shoulds are Ored together",5,bits.cardinality());
| public void | testShouldsAndMust()
BooleanFilter booleanFilter = new BooleanFilter();
booleanFilter.add(new FilterClause(getRangeFilter("price","010", "020"),BooleanClause.Occur.SHOULD));
booleanFilter.add(new FilterClause(getRangeFilter("price","020", "030"),BooleanClause.Occur.SHOULD));
booleanFilter.add(new FilterClause(getTermsFilter("accessRights", "admin"),BooleanClause.Occur.MUST));
BitSet bits = booleanFilter.bits(reader);
assertEquals("Shoulds Ored but MUST",3,bits.cardinality());
| public void | testShouldsAndMustNot()
BooleanFilter booleanFilter = new BooleanFilter();
booleanFilter.add(new FilterClause(getRangeFilter("price","010", "020"),BooleanClause.Occur.SHOULD));
booleanFilter.add(new FilterClause(getRangeFilter("price","020", "030"),BooleanClause.Occur.SHOULD));
booleanFilter.add(new FilterClause(getTermsFilter("inStock", "N"),BooleanClause.Occur.MUST_NOT));
BitSet bits = booleanFilter.bits(reader);
assertEquals("Shoulds Ored but AndNot",4,bits.cardinality());
booleanFilter.add(new FilterClause(getTermsFilter("inStock", "Maybe"),BooleanClause.Occur.MUST_NOT));
bits = booleanFilter.bits(reader);
assertEquals("Shoulds Ored but AndNots",3,bits.cardinality());
| public void | testShouldsAndMusts()
BooleanFilter booleanFilter = new BooleanFilter();
booleanFilter.add(new FilterClause(getRangeFilter("price","010", "020"),BooleanClause.Occur.SHOULD));
booleanFilter.add(new FilterClause(getRangeFilter("price","020", "030"),BooleanClause.Occur.SHOULD));
booleanFilter.add(new FilterClause(getTermsFilter("accessRights", "admin"),BooleanClause.Occur.MUST));
booleanFilter.add(new FilterClause(getRangeFilter("date","20040101", "20041231"),BooleanClause.Occur.MUST));
BitSet bits = booleanFilter.bits(reader);
assertEquals("Shoulds Ored but MUSTs ANDED",1,bits.cardinality());
| public void | testShouldsAndMustsAndMustNot()
BooleanFilter booleanFilter = new BooleanFilter();
booleanFilter.add(new FilterClause(getRangeFilter("price","030", "040"),BooleanClause.Occur.SHOULD));
booleanFilter.add(new FilterClause(getTermsFilter("accessRights", "admin"),BooleanClause.Occur.MUST));
booleanFilter.add(new FilterClause(getRangeFilter("date","20050101", "20051231"),BooleanClause.Occur.MUST));
booleanFilter.add(new FilterClause(getTermsFilter("inStock","N"),BooleanClause.Occur.MUST_NOT));
BitSet bits = booleanFilter.bits(reader);
assertEquals("Shoulds Ored but MUSTs ANDED and MustNot",0,bits.cardinality());
|
|