FileDocCategorySizeDatePackage
TestRangeQuery.javaAPI DocApache Lucene 1.4.32961Sat Sep 20 23:12:42 BST 2003org.apache.lucene.search

TestRangeQuery

public class TestRangeQuery extends TestCase
author
goller

Fields Summary
private int
docCount
private RAMDirectory
dir
Constructors Summary
Methods Summary
private voidaddDoc(java.lang.String content)

    IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false);
    insertDoc(writer, content);
    writer.close();
  
private voidinitializeIndex(java.lang.String[] values)

    IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
    for (int i = 0; i < values.length; i++) {
      insertDoc(writer, values[i]);
    }
    writer.close();
  
private voidinsertDoc(org.apache.lucene.index.IndexWriter writer, java.lang.String content)

    Document doc = new Document();

    doc.add(Field.Keyword("id", "id" + docCount));
    doc.add(Field.UnStored("content", content));

    writer.addDocument(doc);
    docCount++;
  
public voidsetUp()


     
    dir = new RAMDirectory();
  
public voidtestExclusive()

    Query query = new RangeQuery(new Term("content", "A"),
                                 new Term("content", "C"),
                                 false);
    initializeIndex(new String[] {"A", "B", "C", "D"});
    IndexSearcher searcher = new IndexSearcher(dir);
    Hits hits = searcher.search(query);
    assertEquals("A,B,C,D, only B in range", 1, hits.length());
    searcher.close();

    initializeIndex(new String[] {"A", "B", "D"});
    searcher = new IndexSearcher(dir);
    hits = searcher.search(query);
    assertEquals("A,B,D, only B in range", 1, hits.length());
    searcher.close();

    addDoc("C");
    searcher = new IndexSearcher(dir);
    hits = searcher.search(query);
    assertEquals("C added, still only B in range", 1, hits.length());
    searcher.close();
  
public voidtestInclusive()

    Query query = new RangeQuery(new Term("content", "A"),
                                 new Term("content", "C"),
                                 true);

    initializeIndex(new String[]{"A", "B", "C", "D"});
    IndexSearcher searcher = new IndexSearcher(dir);
    Hits hits = searcher.search(query);
    assertEquals("A,B,C,D - A,B,C in range", 3, hits.length());
    searcher.close();

    initializeIndex(new String[]{"A", "B", "D"});
    searcher = new IndexSearcher(dir);
    hits = searcher.search(query);
    assertEquals("A,B,D - A and B in range", 2, hits.length());
    searcher.close();

    addDoc("C");
    searcher = new IndexSearcher(dir);
    hits = searcher.search(query);
    assertEquals("C added - A, B, C in range", 3, hits.length());
    searcher.close();