KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > lucene > SearchFiles


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

17
18 /* $Id: SearchFiles.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.lucene;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25
26 import org.apache.lucene.analysis.Analyzer;
27 import org.apache.lucene.analysis.standard.StandardAnalyzer;
28 import org.apache.lucene.document.Document;
29 import org.apache.lucene.queryParser.QueryParser;
30 import org.apache.lucene.search.Hits;
31 import org.apache.lucene.search.IndexSearcher;
32 import org.apache.lucene.search.Query;
33 import org.apache.lucene.search.Searcher;
34
35 /**
36  * Command Line Interface
37  */

38 class SearchFiles {
39
40     /**
41      * main method
42      *
43      * @param args Directory of the index
44      */

45     public static void main(String JavaDoc[] args) {
46         if (args.length == 0) {
47             System.err.println("Usage: org.apache.lenya.lucene.SearchFiles \"directory_where_index_is_located\" <word>");
48             return;
49         }
50
51         File JavaDoc index_directory = new File JavaDoc(args[0]);
52
53         if (!index_directory.exists()) {
54             System.err.println("Exception: No such directory: " +
55                 index_directory.getAbsolutePath());
56
57             return;
58         }
59
60
61         try {
62             if (args.length > 1) {
63                 Hits hits = new SearchFiles().search(args[1], index_directory);
64                 return;
65             }
66
67             BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
68
69             while (true) {
70                 System.out.print("Search: ");
71
72                 String JavaDoc line = in.readLine();
73
74                 if (line.length() == -1) {
75                     break;
76                 }
77
78         Hits hits = new SearchFiles().search(line, index_directory);
79
80                     System.out.print("\nAnother Search (y/n) ? ");
81                     line = in.readLine();
82
83                     if ((line.length() == 0) || (line.charAt(0) == 'n')) {
84                          break;
85                     }
86             }
87
88         } catch (Exception JavaDoc e) {
89             System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
90         }
91     }
92
93     /**
94      *
95      */

96     public Hits search(String JavaDoc line, File JavaDoc index_directory) throws Exception JavaDoc {
97         Searcher searcher = new IndexSearcher(index_directory.getAbsolutePath());
98         Analyzer analyzer = new StandardAnalyzer();
99
100         Query query = QueryParser.parse(line, "contents", analyzer);
101         System.out.println("Searching for: " + query.toString("contents"));
102
103                 Hits hits = searcher.search(query);
104                 System.out.println("Total matching documents: " + hits.length());
105
106                 final int HITS_PER_PAGE = 10;
107
108                 for (int start = 0; start < hits.length(); start += HITS_PER_PAGE) {
109                     int end = Math.min(hits.length(), start + HITS_PER_PAGE);
110
111                     for (int i = start; i < end; i++) {
112                         Document doc = hits.doc(i);
113                         String JavaDoc path = doc.get("path");
114
115                         if (path != null) {
116                             System.out.println(i + ". " + path);
117                         } else {
118                             String JavaDoc url = doc.get("url");
119
120                             if (url != null) {
121                                 System.out.println(i + ". " + url);
122                                 System.out.println(" - " + doc.get("title"));
123                             } else {
124                                 System.out.println(i + ". " + "No path nor URL for this document");
125                             }
126                         }
127                     }
128
129                 }
130                 searcher.close();
131         return hits;
132     }
133 }
134
Popular Tags