pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / demo / src / java / org / apache / lucene / demo / SearchFiles.java
1 package org.apache.lucene.demo;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.util.Date;
26
27 import org.apache.lucene.analysis.Analyzer;
28 import org.apache.lucene.analysis.standard.StandardAnalyzer;
29 import org.apache.lucene.document.Document;
30 import org.apache.lucene.index.IndexReader;
31 import org.apache.lucene.queryParser.QueryParser;
32 import org.apache.lucene.search.IndexSearcher;
33 import org.apache.lucene.search.Query;
34 import org.apache.lucene.search.ScoreDoc;
35 import org.apache.lucene.search.TopDocs;
36 import org.apache.lucene.store.FSDirectory;
37 import org.apache.lucene.util.Version;
38
39 /** Simple command-line based search demo. */
40 public class SearchFiles {
41
42   private SearchFiles() {}
43
44   /** Simple command-line based search demo. */
45   public static void main(String[] args) throws Exception {
46     String usage =
47       "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/java/4_0/demo.html for details.";
48     if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
49       System.out.println(usage);
50       System.exit(0);
51     }
52
53     String index = "index";
54     String field = "contents";
55     String queries = null;
56     int repeat = 0;
57     boolean raw = false;
58     String queryString = null;
59     int hitsPerPage = 10;
60     
61     for(int i = 0;i < args.length;i++) {
62       if ("-index".equals(args[i])) {
63         index = args[i+1];
64         i++;
65       } else if ("-field".equals(args[i])) {
66         field = args[i+1];
67         i++;
68       } else if ("-queries".equals(args[i])) {
69         queries = args[i+1];
70         i++;
71       } else if ("-query".equals(args[i])) {
72         queryString = args[i+1];
73         i++;
74       } else if ("-repeat".equals(args[i])) {
75         repeat = Integer.parseInt(args[i+1]);
76         i++;
77       } else if ("-raw".equals(args[i])) {
78         raw = true;
79       } else if ("-paging".equals(args[i])) {
80         hitsPerPage = Integer.parseInt(args[i+1]);
81         if (hitsPerPage <= 0) {
82           System.err.println("There must be at least 1 hit per page.");
83           System.exit(1);
84         }
85         i++;
86       }
87     }
88     
89     IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)));
90     IndexSearcher searcher = new IndexSearcher(reader);
91     Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);
92
93     BufferedReader in = null;
94     if (queries != null) {
95       in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), "UTF-8"));
96     } else {
97       in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
98     }
99     QueryParser parser = new QueryParser(Version.LUCENE_31, field, analyzer);
100     while (true) {
101       if (queries == null && queryString == null) {                        // prompt the user
102         System.out.println("Enter query: ");
103       }
104
105       String line = queryString != null ? queryString : in.readLine();
106
107       if (line == null || line.length() == -1) {
108         break;
109       }
110
111       line = line.trim();
112       if (line.length() == 0) {
113         break;
114       }
115       
116       Query query = parser.parse(line);
117       System.out.println("Searching for: " + query.toString(field));
118             
119       if (repeat > 0) {                           // repeat & time as benchmark
120         Date start = new Date();
121         for (int i = 0; i < repeat; i++) {
122           searcher.search(query, null, 100);
123         }
124         Date end = new Date();
125         System.out.println("Time: "+(end.getTime()-start.getTime())+"ms");
126       }
127
128       doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null);
129
130       if (queryString != null) {
131         break;
132       }
133     }
134     searcher.close();
135     reader.close();
136   }
137
138   /**
139    * This demonstrates a typical paging search scenario, where the search engine presents 
140    * pages of size n to the user. The user can then go to the next page if interested in
141    * the next hits.
142    * 
143    * When the query is executed for the first time, then only enough results are collected
144    * to fill 5 result pages. If the user wants to page beyond this limit, then the query
145    * is executed another time and all hits are collected.
146    * 
147    */
148   public static void doPagingSearch(BufferedReader in, IndexSearcher searcher, Query query, 
149                                      int hitsPerPage, boolean raw, boolean interactive) throws IOException {
150  
151     // Collect enough docs to show 5 pages
152     TopDocs results = searcher.search(query, 5 * hitsPerPage);
153     ScoreDoc[] hits = results.scoreDocs;
154     
155     int numTotalHits = results.totalHits;
156     System.out.println(numTotalHits + " total matching documents");
157
158     int start = 0;
159     int end = Math.min(numTotalHits, hitsPerPage);
160         
161     while (true) {
162       if (end > hits.length) {
163         System.out.println("Only results 1 - " + hits.length +" of " + numTotalHits + " total matching documents collected.");
164         System.out.println("Collect more (y/n) ?");
165         String line = in.readLine();
166         if (line.length() == 0 || line.charAt(0) == 'n') {
167           break;
168         }
169
170         hits = searcher.search(query, numTotalHits).scoreDocs;
171       }
172       
173       end = Math.min(hits.length, start + hitsPerPage);
174       
175       for (int i = start; i < end; i++) {
176         if (raw) {                              // output raw format
177           System.out.println("doc="+hits[i].doc+" score="+hits[i].score);
178           continue;
179         }
180
181         Document doc = searcher.doc(hits[i].doc);
182         String path = doc.get("path");
183         if (path != null) {
184           System.out.println((i+1) + ". " + path);
185           String title = doc.get("title");
186           if (title != null) {
187             System.out.println("   Title: " + doc.get("title"));
188           }
189         } else {
190           System.out.println((i+1) + ". " + "No path for this document");
191         }
192                   
193       }
194
195       if (!interactive || end == 0) {
196         break;
197       }
198
199       if (numTotalHits >= end) {
200         boolean quit = false;
201         while (true) {
202           System.out.print("Press ");
203           if (start - hitsPerPage >= 0) {
204             System.out.print("(p)revious page, ");  
205           }
206           if (start + hitsPerPage < numTotalHits) {
207             System.out.print("(n)ext page, ");
208           }
209           System.out.println("(q)uit or enter number to jump to a page.");
210           
211           String line = in.readLine();
212           if (line.length() == 0 || line.charAt(0)=='q') {
213             quit = true;
214             break;
215           }
216           if (line.charAt(0) == 'p') {
217             start = Math.max(0, start - hitsPerPage);
218             break;
219           } else if (line.charAt(0) == 'n') {
220             if (start + hitsPerPage < numTotalHits) {
221               start+=hitsPerPage;
222             }
223             break;
224           } else {
225             int page = Integer.parseInt(line);
226             if ((page - 1) * hitsPerPage < numTotalHits) {
227               start = (page - 1) * hitsPerPage;
228               break;
229             } else {
230               System.out.println("No such page");
231             }
232           }
233         }
234         if (quit) break;
235         end = Math.min(numTotalHits, start + hitsPerPage);
236       }
237     }
238   }
239 }