1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 package com.isenshi.util.extlib;
42
43 import java.util.Collection;
44
45 import org.apache.lucene.analysis.Analyzer;
46 import org.apache.lucene.analysis.cjk.CJKAnalyzer;
47 import org.apache.lucene.document.Document;
48 import org.apache.lucene.queryParser.QueryParser;
49 import org.apache.lucene.search.Hits;
50 import org.apache.lucene.search.IndexSearcher;
51 import org.apache.lucene.search.Query;
52 import org.apache.lucene.search.Searcher;
53 import org.apache.slide.common.SlideException;
54 import org.apache.slide.search.BadQueryException;
55 import org.apache.slide.search.RequestedResource;
56 import org.apache.slide.search.SearchException;
57 import org.apache.slide.search.basic.BasicResultSetImpl;
58 import org.apache.slide.search.basic.ComparableResourceImpl;
59 import org.apache.slide.search.basic.IBasicExpression;
60 import org.apache.slide.search.basic.IBasicExpressionFactory;
61 import org.apache.slide.search.basic.IBasicQuery;
62 import org.apache.slide.search.basic.IBasicResultSet;
63 import org.apache.slide.structure.ObjectNode;
64 import org.apache.slide.structure.SubjectNode;
65
66 /***
67 * A very basic sample for a store specific Expression. Depending on the
68 * complexity of the concrete store specific implementation, iut might be
69 * a good idea to have an Expression class for each DAV: expression
70 * (SQLEqExpression, SQLOrExpression, ...)
71 *
72 * @version $Revision: 1.3 $
73 */
74 public class BasicExpressionTxtContainsCJK implements IBasicExpression
75 {
76 /*** an example for an executable command */
77 String searchedText;
78
79 String indexPath;
80
81 /*** backptr to the factory */
82 IBasicExpressionFactory factory;
83
84 /***
85 * constructor for a compare expression like gt, eq, ...
86 * For your concrete implementation you are free, which parameters have to
87 * be passed, let the factory give you everything you need.
88 */
89 BasicExpressionTxtContainsCJK (String searchedText, String rootPath)
90 {
91 this.searchedText = searchedText;
92 this.indexPath = rootPath;
93 }
94
95 /***
96 * constructor for a merge expression
97 */
98 BasicExpressionTxtContainsCJK (String mergeOperator,
99 Collection children,
100 IBasicExpressionFactory factory)
101 throws BadQueryException
102 {
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 }
118
119 /***
120 * fake executer. The executable command is printed and a fake result is created.
121 *
122 * @return an IBasicResultSet
123 *
124 * @throws SearchException
125 *
126 */
127 public IBasicResultSet execute() throws SearchException
128 {
129 IBasicResultSet result = new BasicResultSetImpl (false);
130
131 try
132 {
133
134 Searcher searcher = new IndexSearcher(indexPath);
135
136
137 Analyzer analyzer = new CJKAnalyzer();
138
139 Query query = QueryParser.parse(searchedText, "contents", analyzer);
140 Hits hits = searcher.search (query);
141 int noOfHits = hits.length();
142
143 for (int i = 0; i < noOfHits; i++)
144 {
145 Document doc = hits.doc(i);
146 String uri = doc.get("documentId");
147 RequestedResource resource = createResource(uri);
148 result.add (resource);
149 }
150 }
151 catch (Exception e)
152 {
153 throw new SearchException (e);
154 }
155
156 return result;
157 }
158
159 private RequestedResource createResource(String uri) throws SearchException
160 {
161 ObjectNode node = new SubjectNode(uri);
162 RequestedResource resource = null;
163 IBasicQuery query = factory.getQuery();
164
165 try
166 {
167 resource = new ComparableResourceImpl
168 (node, query.getSearchToken(), query.getScope(),
169 factory.getPropertyProvider());
170 }
171 catch (SlideException e)
172 {
173 throw new SearchException (e);
174 }
175 return resource;
176 }
177
178 public void setFactory (IBasicExpressionFactory factory)
179 {
180 this.factory = factory;
181 }
182
183 public IBasicExpressionFactory getFactory()
184 {
185 return this.factory;
186 }
187 }
188