FileDocCategorySizeDatePackage
ChainedFilterTest.javaAPI DocApache Lucene 2.1.04901Wed Feb 14 10:46:22 GMT 2007org.apache.lucene.misc

ChainedFilterTest

public class ChainedFilterTest extends TestCase
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Fields Summary
public static final int
MAX
private RAMDirectory
directory
private IndexSearcher
searcher
private Query
query
private RangeFilter
dateFilter
private QueryFilter
bobFilter
private QueryFilter
sueFilter
Constructors Summary
Methods Summary
private java.util.DateparseDate(java.lang.String s)

    return new SimpleDateFormat("yyyy MMM dd").parse(s);
  
public voidsetUp()


       
    directory = new RAMDirectory();
    IndexWriter writer =
       new IndexWriter(directory, new WhitespaceAnalyzer(), true);

    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(1041397200000L); // 2003 January 01

    for (int i = 0; i < MAX; i++) {
      Document doc = new Document();
      doc.add(new Field("key", "" + (i + 1), Field.Store.YES, Field.Index.UN_TOKENIZED));
      doc.add(new Field("owner", (i < MAX / 2) ? "bob" : "sue", Field.Store.YES, Field.Index.UN_TOKENIZED));
      doc.add(new Field("date", cal.getTime().toString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
      writer.addDocument(doc);

      cal.add(Calendar.DATE, 1);
    }

    writer.close();

    searcher = new IndexSearcher(directory);

    // query for everything to make life easier
    BooleanQuery bq = new BooleanQuery();
    bq.add(new TermQuery(new Term("owner", "bob")), BooleanClause.Occur.SHOULD);
    bq.add(new TermQuery(new Term("owner", "sue")), BooleanClause.Occur.SHOULD);
    query = bq;

    // date filter matches everything too
    Date pastTheEnd = parseDate("2099 Jan 1");
    // dateFilter = DateFilter.Before("date", pastTheEnd);
    // just treat dates as strings and select the whole range for now...
    dateFilter = new RangeFilter("date","","ZZZZ",true,true);

    bobFilter = new QueryFilter(
        new TermQuery(new Term("owner", "bob")));
    sueFilter = new QueryFilter(
        new TermQuery(new Term("owner", "sue")));
  
public voidtestAND()

    ChainedFilter chain = new ChainedFilter(
      new Filter[] {dateFilter, bobFilter}, ChainedFilter.AND);

    Hits hits = searcher.search(query, chain);
    assertEquals("AND matches just bob", MAX / 2, hits.length());
    assertEquals("bob", hits.doc(0).get("owner"));
  
public voidtestANDNOT()

    ChainedFilter chain = new ChainedFilter(
      new Filter[]{dateFilter, sueFilter},
        new int[] {ChainedFilter.AND, ChainedFilter.ANDNOT});

    Hits hits = searcher.search(query, chain);
    assertEquals("ANDNOT matches just bob",
        MAX / 2, hits.length());
    assertEquals("bob", hits.doc(0).get("owner"));
  
public voidtestOR()

    ChainedFilter chain = new ChainedFilter(
      new Filter[] {sueFilter, bobFilter});

    Hits hits = searcher.search(query, chain);
    assertEquals("OR matches all", MAX, hits.length());
  
public voidtestSingleFilter()

    ChainedFilter chain = new ChainedFilter(
        new Filter[] {dateFilter});

    Hits hits = searcher.search(query, chain);
    assertEquals(MAX, hits.length());

    chain = new ChainedFilter(new Filter[] {bobFilter});
    hits = searcher.search(query, chain);
    assertEquals(MAX / 2, hits.length());
  
public voidtestXOR()

    ChainedFilter chain = new ChainedFilter(
      new Filter[]{dateFilter, bobFilter}, ChainedFilter.XOR);

    Hits hits = searcher.search(query, chain);
    assertEquals("XOR matches sue", MAX / 2, hits.length());
    assertEquals("sue", hits.doc(0).get("owner"));