FileDocCategorySizeDatePackage
TestDateFilter.javaAPI DocApache Lucene 1.94632Mon Feb 20 09:20:30 GMT 2006org.apache.lucene.search

TestDateFilter

public class TestDateFilter extends TestCase
DateFilter JUnit tests.
author
Otis Gospodnetic
version
$Revision: 150694 $

Fields Summary
Constructors Summary
public TestDateFilter(String name)

	super(name);
    
Methods Summary
public static voidtestAfter()

	// create an index
        RAMDirectory indexStore = new RAMDirectory();
        IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true);

 	long now = System.currentTimeMillis();

 	Document doc = new Document();
 	// add time that is in the future
 	doc.add(Field.Keyword("datefield", DateField.timeToString(now + 888888)));
 	doc.add(Field.Text("body", "Today is a very sunny day in New York City"));
  	writer.addDocument(doc);
 	writer.optimize();
	writer.close();

	IndexSearcher searcher = new IndexSearcher(indexStore);

	// filter that should preserve matches
	DateFilter df1 = DateFilter.After("datefield", now);

	// filter that should discard matches
	DateFilter df2 = DateFilter.After("datefield", now + 999999);

	// search something that doesn't exist with DateFilter
	Query query1 = new TermQuery(new Term("body", "NoMatchForThis"));

	// search for something that does exists
	Query query2 = new TermQuery(new Term("body", "sunny"));

	Hits result;

	// ensure that queries return expected results without DateFilter first
	result = searcher.search(query1);
	assertEquals(0, result.length());

	result = searcher.search(query2);
	assertEquals(1, result.length());


	// run queries with DateFilter
	result = searcher.search(query1, df1);
	assertEquals(0, result.length());

	result = searcher.search(query1, df2);
	assertEquals(0, result.length());

 	result = searcher.search(query2, df1);
 	assertEquals(1, result.length());

	result = searcher.search(query2, df2);
	assertEquals(0, result.length());
    
public static voidtestBefore()

	// create an index
        RAMDirectory indexStore = new RAMDirectory();
        IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true);

 	long now = System.currentTimeMillis();

 	Document doc = new Document();
 	// add time that is in the past
 	doc.add(Field.Keyword("datefield", DateField.timeToString(now - 1000)));
 	doc.add(Field.Text("body", "Today is a very sunny day in New York City"));
  	writer.addDocument(doc);
 	writer.optimize();
	writer.close();

	IndexSearcher searcher = new IndexSearcher(indexStore);

	// filter that should preserve matches
	DateFilter df1 = DateFilter.Before("datefield", now);

	// filter that should discard matches
	DateFilter df2 = DateFilter.Before("datefield", now - 999999);

	// search something that doesn't exist with DateFilter
	Query query1 = new TermQuery(new Term("body", "NoMatchForThis"));

	// search for something that does exists
	Query query2 = new TermQuery(new Term("body", "sunny"));

	Hits result;

	// ensure that queries return expected results without DateFilter first
	result = searcher.search(query1);
	assertEquals(0, result.length());

	result = searcher.search(query2);
	assertEquals(1, result.length());


	// run queries with DateFilter
	result = searcher.search(query1, df1);
	assertEquals(0, result.length());

	result = searcher.search(query1, df2);
	assertEquals(0, result.length());

 	result = searcher.search(query2, df1);
 	assertEquals(1, result.length());

	result = searcher.search(query2, df2);
	assertEquals(0, result.length());