001package org.apache.lucene.demo;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements.  See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020import java.io.BufferedReader;
021import java.io.File;
022import java.io.FileInputStream;
023import java.io.IOException;
024import java.io.InputStreamReader;
025import java.util.Date;
026
027import org.apache.lucene.analysis.Analyzer;
028import org.apache.lucene.analysis.standard.StandardAnalyzer;
029import org.apache.lucene.document.Document;
030import org.apache.lucene.index.DirectoryReader;
031import org.apache.lucene.index.IndexReader;
032import org.apache.lucene.queryparser.classic.QueryParser;
033import org.apache.lucene.search.IndexSearcher;
034import org.apache.lucene.search.Query;
035import org.apache.lucene.search.ScoreDoc;
036import org.apache.lucene.search.TopDocs;
037import org.apache.lucene.store.FSDirectory;
038import org.apache.lucene.util.Version;
039
040/** Simple command-line based search demo. */
041public class SearchFiles {
042
043  private SearchFiles() {}
044
045  /** Simple command-line based search demo. */
046  public static void main(String[] args) throws Exception {
047    String usage =
048      "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/core/4_1_0/demo/ for details.";
049    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
050      System.out.println(usage);
051      System.exit(0);
052    }
053
054    String index = "index";
055    String field = "contents";
056    String queries = null;
057    int repeat = 0;
058    boolean raw = false;
059    String queryString = null;
060    int hitsPerPage = 10;
061    
062    for(int i = 0;i < args.length;i++) {
063      if ("-index".equals(args[i])) {
064        index = args[i+1];
065        i++;
066      } else if ("-field".equals(args[i])) {
067        field = args[i+1];
068        i++;
069      } else if ("-queries".equals(args[i])) {
070        queries = args[i+1];
071        i++;
072      } else if ("-query".equals(args[i])) {
073        queryString = args[i+1];
074        i++;
075      } else if ("-repeat".equals(args[i])) {
076        repeat = Integer.parseInt(args[i+1]);
077        i++;
078      } else if ("-raw".equals(args[i])) {
079        raw = true;
080      } else if ("-paging".equals(args[i])) {
081        hitsPerPage = Integer.parseInt(args[i+1]);
082        if (hitsPerPage <= 0) {
083          System.err.println("There must be at least 1 hit per page.");
084          System.exit(1);
085        }
086        i++;
087      }
088    }
089    
090    IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index)));
091    IndexSearcher searcher = new IndexSearcher(reader);
092    // :Post-Release-Update-Version.LUCENE_XY:
093    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47);
094
095    BufferedReader in = null;
096    if (queries != null) {
097      in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), "UTF-8"));
098    } else {
099      in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
100    }
101    // :Post-Release-Update-Version.LUCENE_XY:
102    QueryParser parser = new QueryParser(Version.LUCENE_47, field, analyzer);
103    while (true) {
104      if (queries == null && queryString == null) {                        // prompt the user
105        System.out.println("Enter query: ");
106      }
107
108      String line = queryString != null ? queryString : in.readLine();
109
110      if (line == null || line.length() == -1) {
111        break;
112      }
113
114      line = line.trim();
115      if (line.length() == 0) {
116        break;
117      }
118      
119      Query query = parser.parse(line);
120      System.out.println("Searching for: " + query.toString(field));
121            
122      if (repeat > 0) {                           // repeat & time as benchmark
123        Date start = new Date();
124        for (int i = 0; i < repeat; i++) {
125          searcher.search(query, null, 100);
126        }
127        Date end = new Date();
128        System.out.println("Time: "+(end.getTime()-start.getTime())+"ms");
129      }
130
131      doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null);
132
133      if (queryString != null) {
134        break;
135      }
136    }
137    reader.close();
138  }
139
140  /**
141   * This demonstrates a typical paging search scenario, where the search engine presents 
142   * pages of size n to the user. The user can then go to the next page if interested in
143   * the next hits.
144   * 
145   * When the query is executed for the first time, then only enough results are collected
146   * to fill 5 result pages. If the user wants to page beyond this limit, then the query
147   * is executed another time and all hits are collected.
148   * 
149   */
150  public static void doPagingSearch(BufferedReader in, IndexSearcher searcher, Query query, 
151                                     int hitsPerPage, boolean raw, boolean interactive) throws IOException {
152 
153    // Collect enough docs to show 5 pages
154    TopDocs results = searcher.search(query, 5 * hitsPerPage);
155    ScoreDoc[] hits = results.scoreDocs;
156    
157    int numTotalHits = results.totalHits;
158    System.out.println(numTotalHits + " total matching documents");
159
160    int start = 0;
161    int end = Math.min(numTotalHits, hitsPerPage);
162        
163    while (true) {
164      if (end > hits.length) {
165        System.out.println("Only results 1 - " + hits.length +" of " + numTotalHits + " total matching documents collected.");
166        System.out.println("Collect more (y/n) ?");
167        String line = in.readLine();
168        if (line.length() == 0 || line.charAt(0) == 'n') {
169          break;
170        }
171
172        hits = searcher.search(query, numTotalHits).scoreDocs;
173      }
174      
175      end = Math.min(hits.length, start + hitsPerPage);
176      
177      for (int i = start; i < end; i++) {
178        if (raw) {                              // output raw format
179          System.out.println("doc="+hits[i].doc+" score="+hits[i].score);
180          continue;
181        }
182
183        Document doc = searcher.doc(hits[i].doc);
184        String path = doc.get("path");
185        if (path != null) {
186          System.out.println((i+1) + ". " + path);
187          String title = doc.get("title");
188          if (title != null) {
189            System.out.println("   Title: " + doc.get("title"));
190          }
191        } else {
192          System.out.println((i+1) + ". " + "No path for this document");
193        }
194                  
195      }
196
197      if (!interactive || end == 0) {
198        break;
199      }
200
201      if (numTotalHits >= end) {
202        boolean quit = false;
203        while (true) {
204          System.out.print("Press ");
205          if (start - hitsPerPage >= 0) {
206            System.out.print("(p)revious page, ");  
207          }
208          if (start + hitsPerPage < numTotalHits) {
209            System.out.print("(n)ext page, ");
210          }
211          System.out.println("(q)uit or enter number to jump to a page.");
212          
213          String line = in.readLine();
214          if (line.length() == 0 || line.charAt(0)=='q') {
215            quit = true;
216            break;
217          }
218          if (line.charAt(0) == 'p') {
219            start = Math.max(0, start - hitsPerPage);
220            break;
221          } else if (line.charAt(0) == 'n') {
222            if (start + hitsPerPage < numTotalHits) {
223              start+=hitsPerPage;
224            }
225            break;
226          } else {
227            int page = Integer.parseInt(line);
228            if ((page - 1) * hitsPerPage < numTotalHits) {
229              start = (page - 1) * hitsPerPage;
230              break;
231            } else {
232              System.out.println("No such page");
233            }
234          }
235        }
236        if (quit) break;
237        end = Math.min(numTotalHits, start + hitsPerPage);
238      }
239    }
240  }
241}