Tests for indexing a PHP AST with Lucene search engine;
[phpeclipse.git] / net.sourceforge.phpeclipse.tests / src / net / sourceforge / phpdt / core / tests / lucene / PHPSearcher.java
1 package net.sourceforge.phpdt.core.tests.lucene;
2
3 import java.io.IOException;
4
5 import org.apache.lucene.analysis.standard.StandardAnalyzer;
6 import org.apache.lucene.document.Document;
7 import org.apache.lucene.index.Term;
8 import org.apache.lucene.queryParser.ParseException;
9 import org.apache.lucene.queryParser.QueryParser;
10 import org.apache.lucene.search.Hits;
11 import org.apache.lucene.search.IndexSearcher;
12 import org.apache.lucene.search.Query;
13 import org.apache.lucene.search.TermQuery;
14
15 public class PHPSearcher {
16         private IndexSearcher fSearcher;
17         private StandardAnalyzer analyzer;
18
19         public PHPSearcher(String indexPath) {
20                 try {
21                         fSearcher = new IndexSearcher(indexPath);
22                         analyzer = new StandardAnalyzer();
23                 } catch (IOException e) {
24                         e.printStackTrace();
25                 }
26         }
27
28         public Hits getClassInfo(String ident) {
29                 Hits hits = null;
30                 try {
31       Query query = new TermQuery(new Term("c", ident));
32                         hits = fSearcher.search(query);
33                         int hitCount = hits.length();
34                         Document doc;
35                         for (int i = 0; (i < hitCount && i < 10); i++) {
36                                 doc = hits.doc(i);
37                                 for (int j = 0; j < doc.getValues("c").length; j++) {
38                                         System.out.println(doc.getValues("c")[j]);
39                                 }
40                         }
41                 } catch (IOException e) {
42                         e.printStackTrace();
43                 }
44                 return hits;
45         }
46
47         public Hits getIncludeInfo(String ident) {
48                 Hits hits = null;
49                 try {
50 //                      Query query = QueryParser.parse(ident, "f", new StandardAnalyzer());
51                         Query query = new TermQuery(new Term("i", ident));
52                         hits = fSearcher.search(query);
53                         int hitCount = hits.length();
54                         Document doc;
55                         for (int i = 0; (i < hitCount && i < 10); i++) {
56                                 doc = hits.doc(i);
57                                 for (int j = 0; j < doc.getValues("i").length; j++) {
58                                         System.out.println(doc.getValues("i")[j]);
59                                 }
60                         }
61                 } catch (IOException e) {
62                         e.printStackTrace();
63                 }
64                 return hits;
65         }
66
67         public Hits getFunctionInfo(String ident) {
68                 Hits hits = null;
69                 try {
70 //                      Query query = QueryParser.parse(ident, "f", new StandardAnalyzer());
71                         Query query = new TermQuery(new Term("f", ident));
72                         hits = fSearcher.search(query);
73                         int hitCount = hits.length();
74                         Document doc;
75                         for (int i = 0; (i < hitCount && i < 10); i++) {
76                                 doc = hits.doc(i);
77                                 for (int j = 0; j < doc.getValues("f").length; j++) {
78                                         System.out.println(doc.getValues("f")[j]);
79                                 }
80                         }
81                 } catch (IOException e) {
82                         e.printStackTrace();
83                 }
84                 return hits;
85         }
86
87         public Hits getMethodInfo(String ident) {
88                 Hits hits = null;
89                 try {
90 //                      Query query = QueryParser.parse(ident, "m", new StandardAnalyzer());
91                         Query query = new TermQuery(new Term("m", ident));
92                         hits = fSearcher.search(query);
93                         int hitCount = hits.length();
94                         Document doc;
95                         for (int i = 0; (i < hitCount && i < 10); i++) {
96                                 doc = hits.doc(i);
97                                 for (int j = 0; j < doc.getValues("m").length; j++) {
98                                         System.out.println(doc.getValues("m")[j]);
99                                 }
100                         }
101                 } catch (IOException e) {
102                         e.printStackTrace();
103                 }
104                 return hits;
105         }
106         public Hits getAttributeInfo(String ident) {
107                 Hits hits = null;
108                 try {
109 //                      Query query = QueryParser.parse(ident, "m", new StandardAnalyzer());
110                         Query query = new TermQuery(new Term("a", ident));
111                         hits = fSearcher.search(query);
112                         int hitCount = hits.length();
113                         Document doc;
114                         for (int i = 0; (i < hitCount && i < 10); i++) {
115                                 doc = hits.doc(i);
116                                 for (int j = 0; j < doc.getValues("a").length; j++) {
117                                         System.out.println(doc.getValues("a")[j]);
118                                 }
119                         }
120                 } catch (IOException e) {
121                         e.printStackTrace();
122                 }
123                 return hits;
124         }
125 }