KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > search > impl > lucene > query > PathQuery


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.search.impl.lucene.query;
18
19 import java.io.IOException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.alfresco.service.cmr.dictionary.DictionaryService;
24 import org.apache.lucene.index.IndexReader;
25 import org.apache.lucene.index.Term;
26 import org.apache.lucene.search.Explanation;
27 import org.apache.lucene.search.Query;
28 import org.apache.lucene.search.Scorer;
29 import org.apache.lucene.search.Searcher;
30 import org.apache.lucene.search.Weight;
31
32 /**
33  * An extension to the Lucene query set.
34  *
35  * This query supports structured queries against paths.
36  *
37  * The field must have been tokenised using the path tokeniser.
38  *
39  * This class manages linking together an ordered chain of absolute and relative
40  * positional queries.
41  *
42  * @author Andy Hind
43  */

44 public class PathQuery extends Query
45 {
46     /**
47      *
48      */

49     private static final long serialVersionUID = 3832904355660707892L;
50
51     private String JavaDoc pathField = "PATH";
52
53     private String JavaDoc qNameField = "QNAME";
54
55     private int unitSize = 2;
56
57     private List JavaDoc<StructuredFieldPosition> pathStructuredFieldPositions = new ArrayList JavaDoc<StructuredFieldPosition>();
58
59     private List JavaDoc<StructuredFieldPosition> qNameStructuredFieldPositions = new ArrayList JavaDoc<StructuredFieldPosition>();
60
61     private DictionaryService dictionarySertvice;
62     
63     private boolean repeats = false;
64
65     /**
66      * The base query
67      *
68      * @param query
69      */

70
71     public PathQuery(DictionaryService dictionarySertvice)
72     {
73         super();
74         this.dictionarySertvice = dictionarySertvice;
75     }
76
77     public void setQuery(List JavaDoc<StructuredFieldPosition> path, List JavaDoc<StructuredFieldPosition> qname)
78     {
79         qNameStructuredFieldPositions.clear();
80         pathStructuredFieldPositions.clear();
81         if (qname.size() != unitSize)
82         {
83             throw new UnsupportedOperationException JavaDoc();
84         }
85         if (path.size() % unitSize != 0)
86         {
87             throw new UnsupportedOperationException JavaDoc();
88         }
89         qNameStructuredFieldPositions.addAll(qname);
90         pathStructuredFieldPositions.addAll(path);
91     }
92     
93     public void appendQuery(List JavaDoc<StructuredFieldPosition> sfps)
94     {
95         if (sfps.size() != unitSize)
96         {
97             throw new UnsupportedOperationException JavaDoc();
98         }
99
100         StructuredFieldPosition last = null;
101         StructuredFieldPosition next = sfps.get(0);
102
103         if (qNameStructuredFieldPositions.size() > 0)
104         {
105             last = qNameStructuredFieldPositions.get(qNameStructuredFieldPositions.size() - 1);
106         }
107
108         if ((last != null) && next.linkParent() && !last.allowslinkingByParent())
109         {
110             return;
111         }
112
113         if ((last != null) && next.linkSelf() && !last.allowsLinkingBySelf())
114         {
115             return;
116         }
117
118         if (qNameStructuredFieldPositions.size() == unitSize)
119         {
120             pathStructuredFieldPositions.addAll(qNameStructuredFieldPositions);
121         }
122         qNameStructuredFieldPositions.clear();
123         qNameStructuredFieldPositions.addAll(sfps);
124     }
125
126     public String JavaDoc getPathField()
127     {
128         return pathField;
129     }
130
131     public void setPathField(String JavaDoc pathField)
132     {
133         this.pathField = pathField;
134     }
135
136     public String JavaDoc getQnameField()
137     {
138         return qNameField;
139     }
140
141     public void setQnameField(String JavaDoc qnameField)
142     {
143         this.qNameField = qnameField;
144     }
145
146     public Term getPathRootTerm()
147     {
148         return new Term(getPathField(), ";");
149     }
150     
151     public Term getQNameRootTerm()
152     {
153         return new Term(getQnameField(), ";");
154     }
155
156     /*
157      * @see org.apache.lucene.search.Query#createWeight(org.apache.lucene.search.Searcher)
158      */

159     protected Weight createWeight(Searcher searcher)
160     {
161         return new StructuredFieldWeight(searcher);
162     }
163
164     /*
165      * @see java.lang.Object#toString()
166      */

167     public String JavaDoc toString()
168     {
169         return "";
170     }
171
172     /*
173      * @see org.apache.lucene.search.Query#toString(java.lang.String)
174      */

175     public String JavaDoc toString(String JavaDoc field)
176     {
177         return "";
178     }
179
180     private class StructuredFieldWeight implements Weight
181     {
182
183         /**
184          *
185          */

186         private static final long serialVersionUID = 3257854259645985328L;
187
188         private Searcher searcher;
189
190         private float value;
191
192         private float idf;
193
194         private float queryNorm;
195
196         private float queryWeight;
197
198         public StructuredFieldWeight(Searcher searcher)
199         {
200             this.searcher = searcher;
201
202         }
203
204         /*
205          * @see org.apache.lucene.search.Weight#explain(org.apache.lucene.index.IndexReader,
206          * int)
207          */

208         public Explanation explain(IndexReader reader, int doc) throws IOException JavaDoc
209         {
210             throw new UnsupportedOperationException JavaDoc();
211         }
212
213         /*
214          * @see org.apache.lucene.search.Weight#getQuery()
215          */

216         public Query getQuery()
217         {
218             return PathQuery.this;
219         }
220
221         /*
222          * (non-Javadoc)
223          *
224          * @see org.apache.lucene.search.Weight#getValue()
225          */

226         public float getValue()
227         {
228             return value;
229         }
230
231         /*
232          * (non-Javadoc)
233          *
234          * @see org.apache.lucene.search.Weight#normalize(float)
235          */

236         public void normalize(float queryNorm)
237         {
238             this.queryNorm = queryNorm;
239             queryWeight *= queryNorm; // normalize query weight
240
value = queryWeight * idf; // idf for document
241
}
242
243         /*
244          * (non-Javadoc)
245          *
246          * @see org.apache.lucene.search.Weight#scorer(org.apache.lucene.index.IndexReader)
247          */

248         public Scorer scorer(IndexReader reader) throws IOException JavaDoc
249         {
250             return PathScorer.createPathScorer(getSimilarity(searcher), PathQuery.this, reader, this, dictionarySertvice, repeats);
251             
252         }
253
254         /*
255          * (non-Javadoc)
256          *
257          * @see org.apache.lucene.search.Weight#sumOfSquaredWeights()
258          */

259         public float sumOfSquaredWeights() throws IOException JavaDoc
260         {
261             idf = getSimilarity(searcher).idf(getTerms(), searcher); // compute
262
// idf
263
queryWeight = idf * getBoost(); // compute query weight
264
return queryWeight * queryWeight; // square it
265
}
266
267         private ArrayList JavaDoc<Term> getTerms()
268         {
269             ArrayList JavaDoc<Term> answer = new ArrayList JavaDoc<Term>(pathStructuredFieldPositions.size());
270             for (StructuredFieldPosition sfp : pathStructuredFieldPositions)
271             {
272                 if (sfp.getTermText() != null)
273                 {
274                     Term term = new Term(pathField, sfp.getTermText());
275                     answer.add(term);
276                 }
277             }
278             return answer;
279         }
280     }
281
282     public void removeDescendantAndSelf()
283     {
284         while ((getLast() != null) && getLast().linkSelf())
285         {
286             removeLast();
287             removeLast();
288         }
289     }
290
291     private StructuredFieldPosition getLast()
292
293     {
294         if (qNameStructuredFieldPositions.size() > 0)
295         {
296             return qNameStructuredFieldPositions.get(qNameStructuredFieldPositions.size() - 1);
297         }
298         else
299         {
300             return null;
301         }
302     }
303
304     private void removeLast()
305     {
306         qNameStructuredFieldPositions.clear();
307         for (int i = 0; i < unitSize; i++)
308         {
309             if (pathStructuredFieldPositions.size() > 0)
310             {
311                 qNameStructuredFieldPositions.add(0, pathStructuredFieldPositions.remove(pathStructuredFieldPositions.size() - 1));
312             }
313         }
314     }
315
316     public boolean isEmpty()
317     {
318         return qNameStructuredFieldPositions.size() == 0;
319     }
320
321     public List JavaDoc<StructuredFieldPosition> getPathStructuredFieldPositions()
322     {
323         return pathStructuredFieldPositions;
324     }
325     
326
327     public List JavaDoc<StructuredFieldPosition> getQNameStructuredFieldPositions()
328     {
329         return qNameStructuredFieldPositions;
330     }
331
332     public void setRepeats(boolean repeats)
333     {
334         this.repeats = repeats;
335     }
336     
337     
338
339     
340     
341 }
Popular Tags