Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
../../../../img/srcFileCovDistChart0.png 86% of files have more coverage
80   172   31   20
44   124   0.39   2
4     7.75  
2    
 
  SearchFiles       Line # 37 77 29 0% 0.0
  SearchFiles.OneNormsReader       Line # 44 3 2 0% 0.0
 
No Tests
 
1    package org.apache.lucene.demo;
2   
3    /**
4    * Copyright 2004 The Apache Software Foundation
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10    * http://www.apache.org/licenses/LICENSE-2.0
11    *
12    * Unless required by applicable law or agreed to in writing, software
13    * distributed under the License is distributed on an "AS IS" BASIS,
14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    * See the License for the specific language governing permissions and
16    * limitations under the License.
17    */
18   
19    import org.apache.lucene.analysis.Analyzer;
20    import org.apache.lucene.analysis.standard.StandardAnalyzer;
21    import org.apache.lucene.document.Document;
22    import org.apache.lucene.index.FilterIndexReader;
23    import org.apache.lucene.index.IndexReader;
24    import org.apache.lucene.queryParser.QueryParser;
25    import org.apache.lucene.search.Hits;
26    import org.apache.lucene.search.IndexSearcher;
27    import org.apache.lucene.search.Query;
28    import org.apache.lucene.search.Searcher;
29   
30    import java.io.BufferedReader;
31    import java.io.FileReader;
32    import java.io.IOException;
33    import java.io.InputStreamReader;
34    import java.util.Date;
35   
36    /** Simple command-line based search demo. */
 
37    public class SearchFiles {
38   
39    /** Use the norms from one field for all fields. Norms are read into memory,
40    * using a byte of memory per document per searched field. This can cause
41    * search of large collections with a large number of fields to run out of
42    * memory. If all of the fields contain only a single token, then the norms
43    * are all identical, then single norm vector may be shared. */
 
44    private static class OneNormsReader extends FilterIndexReader {
45    private String field;
46   
 
47  0 toggle public OneNormsReader(IndexReader in, String field) {
48  0 super(in);
49  0 this.field = field;
50    }
51   
 
52  0 toggle public byte[] norms(String field) throws IOException {
53  0 return in.norms(this.field);
54    }
55    }
56   
 
57  0 toggle private SearchFiles() {}
58   
59    /** Simple command-line based search demo. */
 
60  0 toggle public static void main(String[] args) throws Exception {
61  0 String usage =
62    "Usage: java org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-raw] [-norms field]";
63  0 if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
64  0 System.out.println(usage);
65  0 System.exit(0);
66    }
67   
68  0 String index = "index";
69  0 String field = "contents";
70  0 String queries = null;
71  0 int repeat = 0;
72  0 boolean raw = false;
73  0 String normsField = null;
74   
75  0 for (int i = 0; i < args.length; i++) {
76  0 if ("-index".equals(args[i])) {
77  0 index = args[i+1];
78  0 i++;
79  0 } else if ("-field".equals(args[i])) {
80  0 field = args[i+1];
81  0 i++;
82  0 } else if ("-queries".equals(args[i])) {
83  0 queries = args[i+1];
84  0 i++;
85  0 } else if ("-repeat".equals(args[i])) {
86  0 repeat = Integer.parseInt(args[i+1]);
87  0 i++;
88  0 } else if ("-raw".equals(args[i])) {
89  0 raw = true;
90  0 } else if ("-norms".equals(args[i])) {
91  0 normsField = args[i+1];
92  0 i++;
93    }
94    }
95   
96  0 IndexReader reader = IndexReader.open(index);
97   
98  0 if (normsField != null)
99  0 reader = new OneNormsReader(reader, normsField);
100   
101  0 Searcher searcher = new IndexSearcher(reader);
102  0 Analyzer analyzer = new StandardAnalyzer();
103   
104  0 BufferedReader in = null;
105  0 if (queries != null) {
106  0 in = new BufferedReader(new FileReader(queries));
107    } else {
108  0 in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
109    }
110  0 QueryParser parser = new QueryParser(field, analyzer);
111  0 while (true) {
112  0 if (queries == null) // prompt the user
113  0 System.out.print("Query: ");
114   
115  0 String line = in.readLine();
116   
117  0 if (line == null || line.length() == -1)
118  0 break;
119   
120  0 Query query = parser.parse(line);
121  0 System.out.println("Searching for: " + query.toString(field));
122   
123  0 Hits hits = searcher.search(query);
124   
125  0 if (repeat > 0) { // repeat & time as benchmark
126  0 Date start = new Date();
127  0 for (int i = 0; i < repeat; i++) {
128  0 hits = searcher.search(query);
129    }
130  0 Date end = new Date();
131  0 System.out.println("Time: "+(end.getTime()-start.getTime())+"ms");
132    }
133   
134  0 System.out.println(hits.length() + " total matching documents");
135   
136  0 final int HITS_PER_PAGE = 10;
137  0 for (int start = 0; start < hits.length(); start += HITS_PER_PAGE) {
138  0 int end = Math.min(hits.length(), start + HITS_PER_PAGE);
139  0 for (int i = start; i < end; i++) {
140   
141  0 if (raw) { // output raw format
142  0 System.out.println("doc="+hits.id(i)+" score="+hits.score(i));
143  0 continue;
144    }
145   
146  0 Document doc = hits.doc(i);
147  0 String path = doc.get("path");
148  0 if (path != null) {
149  0 System.out.println((i+1) + ". " + path);
150  0 String title = doc.get("title");
151  0 if (title != null) {
152  0 System.out.println(" Title: " + doc.get("title"));
153    }
154    } else {
155  0 System.out.println((i+1) + ". " + "No path for this document");
156    }
157    }
158   
159  0 if (queries != null) // non-interactive
160  0 break;
161   
162  0 if (hits.length() > end) {
163  0 System.out.print("more (y/n) ? ");
164  0 line = in.readLine();
165  0 if (line.length() == 0 || line.charAt(0) == 'n')
166  0 break;
167    }
168    }
169    }
170  0 reader.close();
171    }
172    }