KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > TestWildcard


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 JavaDoc;
30
31 /**
32  * TestWildcard tests the '*' and '?' wildard characters.
33  *
34  * @author Otis Gospodnetic
35  */

36 public class TestWildcard
37     extends TestCase
38 {
39     /**
40      * Creates a new <code>TestWildcard</code> instance.
41      *
42      * @param name the name of the test
43      */

44     public TestWildcard(String JavaDoc name)
45     {
46     super(name);
47     }
48
49     /**
50      * Tests Wildcard queries with an asterisk.
51      *
52      */

53     public void testAsterisk()
54         throws IOException JavaDoc
55     {
56         RAMDirectory indexStore = getIndexStore("body", new String JavaDoc[]
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     // Queries do not automatically lower-case search terms:
74
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     /**
87      * Tests Wildcard queries with a question mark.
88      *
89      * @exception IOException if an error occurs
90      */

91     public void testQuestionmark()
92     throws IOException JavaDoc
93     {
94         RAMDirectory indexStore = getIndexStore("body", new String JavaDoc[]
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 JavaDoc field, String JavaDoc[] contents)
112     throws IOException JavaDoc
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 JavaDoc
129     {
130     Hits result = searcher.search(q);
131     assertEquals(expectedMatches, result.length());
132     }
133 }
134
Popular Tags