21 using Lucene.Net.Analysis;
22 using Lucene.Net.Analysis.Standard;
23 using Lucene.Net.Documents;
24 using Lucene.Net.QueryParsers;
25 using Lucene.Net.Index;
26 using Lucene.Net.Search;
30 namespace Lucene.Net.Demo
34 public static class SearchFiles
36 private class AnonymousClassCollector :
Collector
42 public override void Collect(
int doc)
44 Console.Out.WriteLine(
"doc=" + doc + docBase +
" score=" + scorer.Score());
47 public override bool AcceptsDocsOutOfOrder
52 public override void SetNextReader(
IndexReader reader,
int docBase)
54 this.docBase = docBase;
57 public override void SetScorer(
Scorer scorer)
72 private readonly String field;
74 public OneNormsReader(
IndexReader in_Renamed, String field):base(in_Renamed)
79 public override byte[] Norms(String field)
81 return in_Renamed.
Norms(this.field);
87 public static void Main(String[] args)
89 String usage =
"Usage:\t" + typeof(SearchFiles) +
"[-index dir] [-field f] [-repeat n] [-queries file] [-raw] [-norms field] [-paging hitsPerPage]";
90 usage +=
"\n\tSpecify 'false' for hitsPerPage to use streaming instead of paging search.";
91 if (args.Length > 0 && (
"-h".Equals(args[0]) ||
"-help".Equals(args[0])))
93 Console.Out.WriteLine(usage);
97 String index =
"index";
98 String field =
"contents";
99 String queries = null;
102 String normsField = null;
104 int hitsPerPage = 10;
106 for (
int i = 0; i < args.Length; i++)
108 if (
"-index".Equals(args[i]))
113 else if (
"-field".Equals(args[i]))
118 else if (
"-queries".Equals(args[i]))
120 queries = args[i + 1];
123 else if (
"-repeat".Equals(args[i]))
125 repeat = Int32.Parse(args[i + 1]);
128 else if (
"-raw".Equals(args[i]))
132 else if (
"-norms".Equals(args[i]))
134 normsField = args[i + 1];
137 else if (
"-paging".Equals(args[i]))
139 if (args[i + 1].Equals(
"false"))
145 hitsPerPage = Int32.Parse(args[i + 1]);
146 if (hitsPerPage == 0)
161 if (normsField != null)
162 indexReader =
new OneNormsReader(indexReader, normsField);
167 StreamReader queryReader;
170 queryReader =
new StreamReader(
new StreamReader(queries, Encoding.Default).BaseStream,
new StreamReader(queries, Encoding.Default).CurrentEncoding);
174 queryReader =
new StreamReader(
new StreamReader(Console.OpenStandardInput(), Encoding.UTF8).BaseStream,
new StreamReader(Console.OpenStandardInput(), Encoding.UTF8).CurrentEncoding);
182 Console.Out.WriteLine(
"Enter query: ");
184 String line = queryReader.ReadLine();
186 if (line == null || line.Length == - 1)
190 if (line.Length == 0)
193 Query query = parser.Parse(line);
194 Console.Out.WriteLine(
"Searching for: " + query.
ToString(field));
199 DateTime start = DateTime.Now;
200 for (
int i = 0; i < repeat; i++)
202 searcher.
Search(query, null, 100);
204 DateTime end = DateTime.Now;
205 Console.Out.WriteLine(
"Time: " + (end.Millisecond - start.Millisecond) +
"ms");
210 DoPagingSearch(queryReader, searcher, query, hitsPerPage, raw, queries == null);
214 DoStreamingSearch(searcher, query);
221 if (indexReader != null)
235 public static void DoStreamingSearch(
Searcher searcher,
Query query)
237 Collector streamingHitCollector =
new AnonymousClassCollector();
238 searcher.
Search(query, streamingHitCollector);
250 public static void DoPagingSearch(StreamReader input,
Searcher searcher,
Query query,
int hitsPerPage,
bool raw,
bool interactive)
255 searcher.
Search(query, collector);
256 var hits = collector.TopDocs().ScoreDocs;
258 int numTotalHits = collector.TotalHits;
259 Console.Out.WriteLine(numTotalHits +
" total matching documents");
262 int end = Math.Min(numTotalHits, hitsPerPage);
266 if (end > hits.Length)
268 Console.Out.WriteLine(
"Only results 1 - " + hits.Length +
" of " + numTotalHits +
" total matching documents collected.");
269 Console.Out.WriteLine(
"Collect more (y/n) ?");
270 String line = input.ReadLine();
271 if (String.IsNullOrEmpty(line) || line[0] ==
'n')
277 searcher.
Search(query, collector);
278 hits = collector.TopDocs().ScoreDocs;
281 end = Math.Min(hits.Length, start + hitsPerPage);
283 for (
int i = start; i < end; i++)
288 Console.Out.WriteLine(
"doc=" + hits[i].Doc +
" score=" + hits[i].Score);
293 String path = doc.
Get(
"path");
296 Console.Out.WriteLine((i + 1) +
". " + path);
297 String title = doc.
Get(
"title");
300 Console.Out.WriteLine(
" Title: " + doc.
Get(
"title"));
305 Console.Out.WriteLine((i + 1) +
". " +
"No path for this document");
314 if (numTotalHits >= end)
319 Console.Out.Write(
"Press ");
320 if (start - hitsPerPage >= 0)
322 Console.Out.Write(
"(p)revious page, ");
324 if (start + hitsPerPage < numTotalHits)
326 Console.Out.Write(
"(n)ext page, ");
328 Console.Out.WriteLine(
"(q)uit or enter number to jump to a page.");
330 String line = input.ReadLine();
331 if (String.IsNullOrEmpty(line) || line[0] ==
'q')
338 start = Math.Max(0, start - hitsPerPage);
341 else if (line[0] ==
'n')
343 if (start + hitsPerPage < numTotalHits)
345 start += hitsPerPage;
351 int page = Int32.Parse(line);
352 if ((page - 1) * hitsPerPage < numTotalHits)
354 start = (page - 1) * hitsPerPage;
359 Console.Out.WriteLine(
"No such page");
365 end = Math.Min(numTotalHits, start + hitsPerPage);