1 package org.apache.lucene.demo;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.BufferedReader;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.util.Date;
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;
39 /** Simple command-line based search demo. */
40 public class SearchFiles {
42 private SearchFiles() {}
44 /** Simple command-line based search demo. */
45 public static void main(String[] args) throws Exception {
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);
53 String index = "index";
54 String field = "contents";
55 String queries = null;
58 String queryString = null;
61 for(int i = 0;i < args.length;i++) {
62 if ("-index".equals(args[i])) {
65 } else if ("-field".equals(args[i])) {
68 } else if ("-queries".equals(args[i])) {
71 } else if ("-query".equals(args[i])) {
72 queryString = args[i+1];
74 } else if ("-repeat".equals(args[i])) {
75 repeat = Integer.parseInt(args[i+1]);
77 } else if ("-raw".equals(args[i])) {
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.");
89 IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)));
90 IndexSearcher searcher = new IndexSearcher(reader);
91 Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);
93 BufferedReader in = null;
94 if (queries != null) {
95 in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), "UTF-8"));
97 in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
99 QueryParser parser = new QueryParser(Version.LUCENE_31, field, analyzer);
101 if (queries == null && queryString == null) { // prompt the user
102 System.out.println("Enter query: ");
105 String line = queryString != null ? queryString : in.readLine();
107 if (line == null || line.length() == -1) {
112 if (line.length() == 0) {
116 Query query = parser.parse(line);
117 System.out.println("Searching for: " + query.toString(field));
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);
124 Date end = new Date();
125 System.out.println("Time: "+(end.getTime()-start.getTime())+"ms");
128 doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null);
130 if (queryString != null) {
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
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.
148 public static void doPagingSearch(BufferedReader in, IndexSearcher searcher, Query query,
149 int hitsPerPage, boolean raw, boolean interactive) throws IOException {
151 // Collect enough docs to show 5 pages
152 TopDocs results = searcher.search(query, 5 * hitsPerPage);
153 ScoreDoc[] hits = results.scoreDocs;
155 int numTotalHits = results.totalHits;
156 System.out.println(numTotalHits + " total matching documents");
159 int end = Math.min(numTotalHits, hitsPerPage);
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') {
170 hits = searcher.search(query, numTotalHits).scoreDocs;
173 end = Math.min(hits.length, start + hitsPerPage);
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);
181 Document doc = searcher.doc(hits[i].doc);
182 String path = doc.get("path");
184 System.out.println((i+1) + ". " + path);
185 String title = doc.get("title");
187 System.out.println(" Title: " + doc.get("title"));
190 System.out.println((i+1) + ". " + "No path for this document");
195 if (!interactive || end == 0) {
199 if (numTotalHits >= end) {
200 boolean quit = false;
202 System.out.print("Press ");
203 if (start - hitsPerPage >= 0) {
204 System.out.print("(p)revious page, ");
206 if (start + hitsPerPage < numTotalHits) {
207 System.out.print("(n)ext page, ");
209 System.out.println("(q)uit or enter number to jump to a page.");
211 String line = in.readLine();
212 if (line.length() == 0 || line.charAt(0)=='q') {
216 if (line.charAt(0) == 'p') {
217 start = Math.max(0, start - hitsPerPage);
219 } else if (line.charAt(0) == 'n') {
220 if (start + hitsPerPage < numTotalHits) {
225 int page = Integer.parseInt(line);
226 if ((page - 1) * hitsPerPage < numTotalHits) {
227 start = (page - 1) * hitsPerPage;
230 System.out.println("No such page");
235 end = Math.min(numTotalHits, start + hitsPerPage);