3 // SearchableIndex.cs: Index that uses Lucene to search through the docs
5 // Author: Mario Sopena
10 using System.Collections;
12 using Monodoc.Lucene.Net.Index;
13 using Monodoc.Lucene.Net.Documents;
14 using Monodoc.Lucene.Net.Analysis;
15 using Monodoc.Lucene.Net.Analysis.Standard;
16 using Monodoc.Lucene.Net.Search;
17 using Monodoc.Lucene.Net.QueryParsers;
22 //TODO: where do I call searcher.close()
23 public class SearchableIndex
25 IndexSearcher searcher;
29 if (dir == null) dir = "search_index";
34 public ArrayList Results;
36 public static SearchableIndex Load (string dir) {
37 SearchableIndex s = new SearchableIndex ();
39 s.Results = new ArrayList (20);
41 s.searcher = new IndexSearcher (dir);
42 } catch (IOException) {
43 Console.WriteLine ("Index nonexistent or in bad format");
50 // Search the index with term
52 public Result Search (string term) {
54 Query q1 = QueryParser.Parse (term, "hottext", new StandardAnalyzer ());
55 Query q2 = QueryParser.Parse (term, "text", new StandardAnalyzer ());
57 Query q3 = QueryParser.Parse (term, "examples", new StandardAnalyzer ());
59 BooleanQuery q = new BooleanQuery();
60 q.Add (q1, false, false);
61 q.Add (q2, false, false);
62 q.Add (q3, false, false);
63 Hits hits = searcher.Search(q);
64 Result r = new Result (term, hits);
67 } catch (IOException) {
68 Console.WriteLine ("No index in {0}", dir);
75 // An object representing the search term with the results
85 get { return hits.Length(); }
87 public Document this [int i] {
88 get { return hits.Doc (i); }
91 public string GetTitle (int i)
93 Document d = hits.Doc (i);
97 return d.Get ("title");
99 public string GetUrl (int i)
101 Document d = hits.Doc (i);
105 return d.Get ("url");
108 public float Score (int i)
110 return hits.Score (i);
112 public Result (string Term, Hits hits)