FileDocCategorySizeDatePackage
BooleanFilterTest.javaAPI DocApache Lucene 2.0.06675Fri May 26 09:54:02 BST 2006org.apache.lucene.search

BooleanFilterTest

public class BooleanFilterTest extends TestCase

Fields Summary
private RAMDirectory
directory
private IndexReader
reader
Constructors Summary
Methods Summary
private voidaddDoc(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.FiltergetRangeFilter(java.lang.String field, java.lang.String lowerPrice, java.lang.String upperPrice)

		return new RangeFilter(field,lowerPrice,upperPrice,true,true);
	
private TermsFiltergetTermsFilter(java.lang.String field, java.lang.String text)

		TermsFilter tf=new TermsFilter();
		tf.addTerm(new Term(field,text));
		return tf;
	
protected voidsetUp()

		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 voidtestJustMust()

		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 voidtestJustMustNot()

		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 voidtestMustAndMustNot()

		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 voidtestShould()

		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 voidtestShoulds()

		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 voidtestShouldsAndMust()

		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 voidtestShouldsAndMustNot()

		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 voidtestShouldsAndMusts()

		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 voidtestShouldsAndMustsAndMustNot()

		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());