1 package org.apache.lucene.search; 2 3 18 19 import org.apache.lucene.search.IndexSearcher; 20 import org.apache.lucene.index.Term; 21 import org.apache.lucene.index.IndexWriter; 22 import org.apache.lucene.store.RAMDirectory; 23 import org.apache.lucene.analysis.SimpleAnalyzer; 24 import org.apache.lucene.document.Document; 25 import org.apache.lucene.document.Field; 26 27 import junit.framework.TestCase; 28 29 import java.io.IOException ; 30 31 36 public class TestWildcard 37 extends TestCase 38 { 39 44 public TestWildcard(String name) 45 { 46 super(name); 47 } 48 49 53 public void testAsterisk() 54 throws IOException 55 { 56 RAMDirectory indexStore = getIndexStore("body", new String [] 57 { "metal", "metals" } 58 ); 59 IndexSearcher searcher = new IndexSearcher(indexStore); 60 Query query1 = new TermQuery(new Term("body", "metal")); 61 Query query2 = new WildcardQuery(new Term("body", "metal*")); 62 Query query3 = new WildcardQuery(new Term("body", "m*tal")); 63 Query query4 = new WildcardQuery(new Term("body", "m*tal*")); 64 Query query5 = new WildcardQuery(new Term("body", "m*tals")); 65 66 BooleanQuery query6 = new BooleanQuery(); 67 query6.add(query5, false, false); 68 69 BooleanQuery query7 = new BooleanQuery(); 70 query7.add(query3, false, false); 71 query7.add(query5, false, false); 72 73 Query query8 = new WildcardQuery(new Term("body", "M*tal*")); 75 76 assertMatches(searcher, query1, 1); 77 assertMatches(searcher, query2, 2); 78 assertMatches(searcher, query3, 1); 79 assertMatches(searcher, query4, 2); 80 assertMatches(searcher, query5, 1); 81 assertMatches(searcher, query6, 1); 82 assertMatches(searcher, query7, 2); 83 assertMatches(searcher, query8, 0); 84 } 85 86 91 public void testQuestionmark() 92 throws IOException 93 { 94 RAMDirectory indexStore = getIndexStore("body", new String [] 95 { "metal", "metals", "mXtals", "mXtXls" } 96 ); 97 IndexSearcher searcher = new IndexSearcher(indexStore); 98 Query query1 = new WildcardQuery(new Term("body", "m?tal")); 99 Query query2 = new WildcardQuery(new Term("body", "metal?")); 100 Query query3 = new WildcardQuery(new Term("body", "metals?")); 101 Query query4 = new WildcardQuery(new Term("body", "m?t?ls")); 102 Query query5 = new WildcardQuery(new Term("body", "M?t?ls")); 103 104 assertMatches(searcher, query1, 1); 105 assertMatches(searcher, query2, 2); 106 assertMatches(searcher, query3, 1); 107 assertMatches(searcher, query4, 3); 108 assertMatches(searcher, query5, 0); 109 } 110 111 private RAMDirectory getIndexStore(String field, String [] contents) 112 throws IOException 113 { 114 RAMDirectory indexStore = new RAMDirectory(); 115 IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true); 116 for (int i = 0; i < contents.length; ++i) { 117 Document doc = new Document(); 118 doc.add(Field.Text(field, contents[i])); 119 writer.addDocument(doc); 120 } 121 writer.optimize(); 122 writer.close(); 123 124 return indexStore; 125 } 126 127 private void assertMatches(IndexSearcher searcher, Query q, int expectedMatches) 128 throws IOException 129 { 130 Hits result = searcher.search(q); 131 assertEquals(expectedMatches, result.length()); 132 } 133 } 134 | Popular Tags |