KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
21
22 import org.alfresco.repo.search.impl.lucene.query.LeafScorer.Counter;
23 import org.alfresco.service.cmr.dictionary.DictionaryService;
24 import org.apache.lucene.document.Document;
25 import org.apache.lucene.document.Field;
26 import org.apache.lucene.index.IndexReader;
27 import org.apache.lucene.index.Term;
28 import org.apache.lucene.index.TermPositions;
29 import org.apache.lucene.search.Explanation;
30 import org.apache.lucene.search.Scorer;
31 import org.apache.lucene.search.Similarity;
32 import org.apache.lucene.search.Weight;
33
34 public class PathScorer extends Scorer
35 {
36     Scorer scorer;
37   
38     PathScorer(Similarity similarity, Scorer scorer)
39     {
40         super(similarity);
41         this.scorer = scorer;
42     }
43   
44
45     public static PathScorer createPathScorer(Similarity similarity, PathQuery pathQuery, IndexReader reader, Weight weight, DictionaryService dictionarySertvice, boolean repeat) throws IOException JavaDoc
46     {
47         Scorer selfScorer = null;
48         HashMap JavaDoc<String JavaDoc, Counter> selfIds = null;
49         
50         StructuredFieldPosition last = null;
51         if(pathQuery.getQNameStructuredFieldPositions().size() > 0)
52         {
53            last = pathQuery.getQNameStructuredFieldPositions().get(pathQuery.getQNameStructuredFieldPositions().size() - 1);
54         }
55         if ((last != null) && last.linkSelf())
56         {
57             PathQuery selfQuery = new PathQuery(dictionarySertvice);
58             selfQuery.setQuery(pathQuery.getPathStructuredFieldPositions(), pathQuery.getQNameStructuredFieldPositions());
59             selfQuery.removeDescendantAndSelf();
60             if (!selfQuery.isEmpty())
61             {
62                selfIds = new HashMap JavaDoc<String JavaDoc, Counter>();
63                selfScorer = PathScorer.createPathScorer(similarity, selfQuery, reader, weight, dictionarySertvice, repeat);
64                selfIds.clear();
65                while (selfScorer.next())
66                {
67                    int doc = selfScorer.doc();
68                    Document document = reader.document(doc);
69                    Field id = document.getField("ID");
70                    Counter counter = selfIds.get(id.stringValue());
71                    if (counter == null)
72                    {
73                        counter = new Counter();
74                        selfIds.put(id.stringValue(), counter);
75                    }
76                    counter.count++;
77                }
78             }
79         }
80         
81         
82         if ((pathQuery.getPathStructuredFieldPositions().size() + pathQuery.getQNameStructuredFieldPositions().size()) == 0) // optimize
83
// zero-term
84
// case
85
return null;
86
87         
88         for (StructuredFieldPosition sfp : pathQuery.getPathStructuredFieldPositions())
89         {
90             if (sfp.getTermText() != null)
91             {
92                 TermPositions p = reader.termPositions(new Term(pathQuery.getPathField(), sfp.getTermText()));
93                 if (p == null)
94                     return null;
95                 CachingTermPositions ctp = new CachingTermPositions(p);
96                 sfp.setCachingTermPositions(ctp);
97             }
98         }
99
100         for (StructuredFieldPosition sfp : pathQuery.getQNameStructuredFieldPositions())
101         {
102             if (sfp.getTermText() != null)
103             {
104                 TermPositions p = reader.termPositions(new Term(pathQuery.getQnameField(), sfp.getTermText()));
105                 if (p == null)
106                     return null;
107                 CachingTermPositions ctp = new CachingTermPositions(p);
108                 sfp.setCachingTermPositions(ctp);
109             }
110         }
111
112         TermPositions rootContainerPositions = null;
113         if (pathQuery.getPathRootTerm() != null)
114         {
115             rootContainerPositions = reader.termPositions(pathQuery.getPathRootTerm());
116         }
117         
118         TermPositions rootLeafPositions = null;
119         if (pathQuery.getQNameRootTerm() != null)
120         {
121             rootLeafPositions = reader.termPositions(pathQuery.getQNameRootTerm());
122         }
123
124
125         TermPositions tp = reader.termPositions();
126
127         ContainerScorer cs = null;
128
129         TermPositions level0 = null;
130
131         TermPositions nodePositions = reader.termPositions(new Term("ISNODE", "T"));
132
133         // StructuredFieldPosition[] test =
134
// (StructuredFieldPosition[])structuredFieldPositions.toArray(new
135
// StructuredFieldPosition[]{});
136
if (pathQuery.getPathStructuredFieldPositions().size() > 0)
137         {
138             TermPositions containerPositions = reader.termPositions(new Term("ISCONTAINER", "T"));
139             cs = new ContainerScorer(weight, rootContainerPositions, (StructuredFieldPosition[]) pathQuery.getPathStructuredFieldPositions().toArray(new StructuredFieldPosition[] {}),
140                     containerPositions, similarity, reader.norms(pathQuery.getPathField()));
141         }
142         else
143         {
144             level0 = reader.termPositions(new Term("ISROOT", "T"));
145         }
146
147         LeafScorer ls = new LeafScorer(weight, rootLeafPositions, level0, cs, (StructuredFieldPosition[]) pathQuery.getQNameStructuredFieldPositions().toArray(new StructuredFieldPosition[] {}), nodePositions,
148                 selfIds, reader, similarity, reader.norms(pathQuery.getQnameField()), dictionarySertvice, repeat, tp);
149
150         return new PathScorer(similarity, ls);
151     }
152
153     @Override JavaDoc
154     public boolean next() throws IOException JavaDoc
155     {
156         return scorer.next();
157     }
158
159     @Override JavaDoc
160     public int doc()
161     {
162         return scorer.doc();
163     }
164
165     @Override JavaDoc
166     public float score() throws IOException JavaDoc
167     {
168         return scorer.score();
169     }
170
171     @Override JavaDoc
172     public boolean skipTo(int position) throws IOException JavaDoc
173     {
174         return scorer.skipTo(position);
175     }
176
177     @Override JavaDoc
178     public Explanation explain(int position) throws IOException JavaDoc
179     {
180         return scorer.explain(position);
181     }
182
183 }
184
Popular Tags