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