* ChangeLog: Added/Started.
[mono.git] / mcs / class / monodoc / Monodoc / SearchableIndex.cs
1 //
2 //
3 // SearchableIndex.cs: Index that uses Lucene to search through the docs 
4 //
5 // Author: Mario Sopena
6 //
7
8 using System;
9 using System.IO;
10 using System.Collections;
11 // Lucene imports
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;
18
19 namespace Monodoc
20 {
21
22 //TODO: where do I call searcher.close()
23 public class SearchableIndex 
24 {
25         IndexSearcher searcher;
26     string dir;
27         public string Dir {
28                 get { 
29                         if (dir == null) dir = "search_index";
30                         return dir;
31                 }
32                 set { dir = value; }
33         }
34         public ArrayList Results;
35         
36         public static SearchableIndex Load (string dir) { 
37                 SearchableIndex s = new SearchableIndex ();
38                 s.dir = dir;
39                 s.Results = new ArrayList (20);
40                 try {
41                         s.searcher = new IndexSearcher (dir);
42                 } catch (IOException) {
43                         Console.WriteLine ("Index nonexistent or in bad format");
44                         return null;
45                 }
46                 return s;
47         }
48                 
49         //
50         // Search the index with term
51         //
52         public Result Search (string term) {
53                 try {
54                         Query q1 = QueryParser.Parse (term, "hottext", new StandardAnalyzer ());
55                         Query q2 = QueryParser.Parse (term, "text", new StandardAnalyzer ());
56                         q2.SetBoost (0.7f);
57                         Query q3 = QueryParser.Parse (term, "examples", new StandardAnalyzer ());
58                         q3.SetBoost (0.5f);
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);
65                         Results.Add (r);
66                         return r;
67                 } catch (IOException) {
68                         Console.WriteLine ("No index in {0}", dir);
69                         return null;
70                 }
71         }
72         
73 }
74 //
75 // An object representing the search term with the results
76 // 
77 public class Result {
78         string term;
79         public string Term {
80                 get { return term;}
81         }
82         public Hits hits;
83
84         public int Count {
85                 get { return hits.Length(); }
86         }
87         public Document this [int i] {
88                 get { return hits.Doc (i); }
89         }
90         
91         public string GetTitle (int i) 
92         {
93                 Document d = hits.Doc (i);
94                 if (d == null)
95                         return "";
96                 else
97                         return d.Get ("title");
98         }
99         public string GetUrl (int i)
100         {
101                 Document d = hits.Doc (i);
102                 if (d == null)
103                         return "";
104                 else
105                         return d.Get ("url");
106                 
107         }
108         public float Score (int i)
109         {
110                 return hits.Score (i);
111         }
112         public Result (string Term, Hits hits) 
113         {
114                 this.term = Term;
115                 this.hits = hits;
116         }
117 }
118 }
119