1 package org.apache.lucene.search; 2 3 18 19 import junit.framework.TestCase; 20 import org.apache.lucene.store.RAMDirectory; 21 import org.apache.lucene.index.IndexWriter; 22 import org.apache.lucene.index.Term; 23 import org.apache.lucene.analysis.WhitespaceAnalyzer; 24 import org.apache.lucene.document.Document; 25 import org.apache.lucene.document.Field; 26 27 32 public class TestPrefixQuery extends TestCase { 33 public void testPrefixQuery() throws Exception { 34 RAMDirectory directory = new RAMDirectory(); 35 36 String [] categories = new String [] {"/Computers", 37 "/Computers/Mac", 38 "/Computers/Windows"}; 39 IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true); 40 for (int i = 0; i < categories.length; i++) { 41 Document doc = new Document(); 42 doc.add(Field.Keyword("category", categories[i])); 43 writer.addDocument(doc); 44 } 45 writer.close(); 46 47 PrefixQuery query = new PrefixQuery(new Term("category", "/Computers")); 48 IndexSearcher searcher = new IndexSearcher(directory); 49 Hits hits = searcher.search(query); 50 assertEquals("All documents in /Computers category and below", 3, hits.length()); 51 52 query = new PrefixQuery(new Term("category", "/Computers/Mac")); 53 hits = searcher.search(query); 54 assertEquals("One in /Computers/Mac", 1, hits.length()); 55 } 56 } 57 | Popular Tags |