KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 import org.apache.lucene.store.*;
22 import org.apache.lucene.document.*;
23 import org.apache.lucene.analysis.*;
24 import org.apache.lucene.index.*;
25 import org.apache.lucene.search.*;
26 import org.apache.lucene.queryParser.*;
27
28 class SearchTest {
29   public static void main(String JavaDoc[] args) {
30     try {
31       Directory directory = new RAMDirectory();
32       Analyzer analyzer = new SimpleAnalyzer();
33       IndexWriter writer = new IndexWriter(directory, analyzer, true);
34
35       String JavaDoc[] docs = {
36         "a b c d e",
37         "a b c d e a b c d e",
38         "a b c d e f g h i j",
39         "a c e",
40         "e c a",
41         "a c e a c e",
42         "a c e a b c"
43       };
44       for (int j = 0; j < docs.length; j++) {
45         Document d = new Document();
46         d.add(Field.Text("contents", docs[j]));
47         writer.addDocument(d);
48       }
49       writer.close();
50
51       Searcher searcher = new IndexSearcher(directory);
52       
53       String JavaDoc[] queries = {
54 // "a b",
55
// "\"a b\"",
56
// "\"a b c\"",
57
// "a c",
58
// "\"a c\"",
59
"\"a c e\"",
60       };
61       Hits hits = null;
62
63       QueryParser parser = new QueryParser("contents", analyzer);
64       parser.setPhraseSlop(4);
65       for (int j = 0; j < queries.length; j++) {
66         Query query = parser.parse(queries[j]);
67         System.out.println("Query: " + query.toString("contents"));
68
69       //DateFilter filter =
70
// new DateFilter("modified", Time(1997,0,1), Time(1998,0,1));
71
//DateFilter filter = DateFilter.Before("modified", Time(1997,00,01));
72
//System.out.println(filter);
73

74         hits = searcher.search(query);
75
76         System.out.println(hits.length() + " total results");
77         for (int i = 0 ; i < hits.length() && i < 10; i++) {
78           Document d = hits.doc(i);
79           System.out.println(i + " " + hits.score(i)
80 // + " " + DateField.stringToDate(d.get("modified"))
81
+ " " + d.get("contents"));
82         }
83       }
84       searcher.close();
85       
86     } catch (Exception JavaDoc e) {
87       System.out.println(" caught a " + e.getClass() +
88              "\n with message: " + e.getMessage());
89     }
90   }
91
92   static long Time(int year, int month, int day) {
93     GregorianCalendar JavaDoc calendar = new GregorianCalendar JavaDoc();
94     calendar.set(year, month, day);
95     return calendar.getTime().getTime();
96   }
97 }
98
Popular Tags