KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > TestSearch


1 package org.apache.lucene;
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 java.util.GregorianCalendar JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import java.io.StringWriter JavaDoc;
22
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25 import junit.textui.TestRunner;
26
27 import org.apache.lucene.store.*;
28 import org.apache.lucene.document.*;
29 import org.apache.lucene.analysis.*;
30 import org.apache.lucene.index.*;
31 import org.apache.lucene.search.*;
32 import org.apache.lucene.queryParser.*;
33
34 /** JUnit adaptation of an older test case SearchTest.
35  * @author dmitrys@earthlink.net
36  * @version $Id: TestSearch.java,v 1.3 2004/03/29 22:48:05 cutting Exp $
37  */

38 public class TestSearch extends TestCase {
39
40     /** Main for running test case by itself. */
41     public static void main(String JavaDoc args[]) {
42         TestRunner.run (new TestSuite(TestSearch.class));
43     }
44
45     /** This test performs a number of searches. It also compares output
46      * of searches using multi-file index segments with single-file
47      * index segments.
48      *
49      * TODO: someone should check that the results of the searches are
50      * still correct by adding assert statements. Right now, the test
51      * passes if the results are the same between multi-file and
52      * single-file formats, even if the results are wrong.
53      */

54     public void testSearch() throws Exception JavaDoc {
55       StringWriter JavaDoc sw = new StringWriter JavaDoc();
56       PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw, true);
57       doTestSearch(pw, false);
58       pw.close();
59       sw.close();
60       String JavaDoc multiFileOutput = sw.getBuffer().toString();
61       //System.out.println(multiFileOutput);
62

63       sw = new StringWriter JavaDoc();
64       pw = new PrintWriter JavaDoc(sw, true);
65       doTestSearch(pw, true);
66       pw.close();
67       sw.close();
68       String JavaDoc singleFileOutput = sw.getBuffer().toString();
69
70       assertEquals(multiFileOutput, singleFileOutput);
71     }
72
73
74     private void doTestSearch(PrintWriter JavaDoc out, boolean useCompoundFile)
75     throws Exception JavaDoc
76     {
77       Directory directory = new RAMDirectory();
78       Analyzer analyzer = new SimpleAnalyzer();
79       IndexWriter writer = new IndexWriter(directory, analyzer, true);
80
81       writer.setUseCompoundFile(useCompoundFile);
82
83       String JavaDoc[] docs = {
84         "a b c d e",
85         "a b c d e a b c d e",
86         "a b c d e f g h i j",
87         "a c e",
88         "e c a",
89         "a c e a c e",
90         "a c e a b c"
91       };
92       for (int j = 0; j < docs.length; j++) {
93         Document d = new Document();
94         d.add(Field.Text("contents", docs[j]));
95         writer.addDocument(d);
96       }
97       writer.close();
98
99       Searcher searcher = new IndexSearcher(directory);
100
101       String JavaDoc[] queries = {
102         "a b",
103         "\"a b\"",
104         "\"a b c\"",
105         "a c",
106         "\"a c\"",
107         "\"a c e\"",
108       };
109       Hits hits = null;
110
111       QueryParser parser = new QueryParser("contents", analyzer);
112       parser.setPhraseSlop(4);
113       for (int j = 0; j < queries.length; j++) {
114         Query query = parser.parse(queries[j]);
115         out.println("Query: " + query.toString("contents"));
116
117       //DateFilter filter =
118
// new DateFilter("modified", Time(1997,0,1), Time(1998,0,1));
119
//DateFilter filter = DateFilter.Before("modified", Time(1997,00,01));
120
//System.out.println(filter);
121

122         hits = searcher.search(query);
123
124         out.println(hits.length() + " total results");
125         for (int i = 0 ; i < hits.length() && i < 10; i++) {
126           Document d = hits.doc(i);
127           out.println(i + " " + hits.score(i)
128 // + " " + DateField.stringToDate(d.get("modified"))
129
+ " " + d.get("contents"));
130         }
131       }
132       searcher.close();
133   }
134
135   static long Time(int year, int month, int day) {
136     GregorianCalendar JavaDoc calendar = new GregorianCalendar JavaDoc();
137     calendar.set(year, month, day);
138     return calendar.getTime().getTime();
139   }
140 }
141
Popular Tags