KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > ConjunctionScorer


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.Comparator JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24
25 /** Scorer for conjunctions, sets of queries, all of which are required. */
26 class ConjunctionScorer extends Scorer {
27   private LinkedList JavaDoc scorers = new LinkedList JavaDoc();
28   private boolean firstTime = true;
29   private boolean more = true;
30   private float coord;
31
32   public ConjunctionScorer(Similarity similarity) {
33     super(similarity);
34   }
35
36   final void add(Scorer scorer) {
37     scorers.addLast(scorer);
38   }
39
40   private Scorer first() { return (Scorer)scorers.getFirst(); }
41   private Scorer last() { return (Scorer)scorers.getLast(); }
42
43   public int doc() { return first().doc(); }
44
45   public boolean next() throws IOException JavaDoc {
46     if (firstTime) {
47       init(true);
48     } else if (more) {
49       more = last().next(); // trigger further scanning
50
}
51     return doNext();
52   }
53   
54   private boolean doNext() throws IOException JavaDoc {
55     while (more && first().doc() < last().doc()) { // find doc w/ all clauses
56
more = first().skipTo(last().doc()); // skip first upto last
57
scorers.addLast(scorers.removeFirst()); // move first to last
58
}
59     return more; // found a doc with all clauses
60
}
61
62   public boolean skipTo(int target) throws IOException JavaDoc {
63     if(firstTime) {
64       init(false);
65     }
66     
67     Iterator JavaDoc i = scorers.iterator();
68     while (more && i.hasNext()) {
69       more = ((Scorer)i.next()).skipTo(target);
70     }
71     
72     if (more)
73       sortScorers(); // re-sort scorers
74

75     return doNext();
76   }
77
78   public float score() throws IOException JavaDoc {
79     float score = 0.0f; // sum scores
80
Iterator JavaDoc i = scorers.iterator();
81     while (i.hasNext())
82       score += ((Scorer)i.next()).score();
83     score *= coord;
84     return score;
85   }
86   
87   private void init(boolean initScorers) throws IOException JavaDoc {
88     // compute coord factor
89
coord = getSimilarity().coord(scorers.size(), scorers.size());
90    
91     more = scorers.size() > 0;
92
93     if(initScorers){
94       // move each scorer to its first entry
95
Iterator JavaDoc i = scorers.iterator();
96       while (more && i.hasNext()) {
97         more = ((Scorer)i.next()).next();
98       }
99       if (more)
100         sortScorers(); // initial sort of list
101
}
102
103     firstTime = false;
104   }
105
106   private void sortScorers() {
107     // move scorers to an array
108
Scorer[] array = (Scorer[])scorers.toArray(new Scorer[scorers.size()]);
109     scorers.clear(); // empty the list
110

111     // note that this comparator is not consistent with equals!
112
Arrays.sort(array, new Comparator JavaDoc() { // sort the array
113
public int compare(Object JavaDoc o1, Object JavaDoc o2) {
114           return ((Scorer)o1).doc() - ((Scorer)o2).doc();
115         }
116       });
117     
118     for (int i = 0; i < array.length; i++) {
119       scorers.addLast(array[i]); // re-build list, now sorted
120
}
121   }
122
123   public Explanation explain(int doc) {
124     throw new UnsupportedOperationException JavaDoc();
125   }
126
127 }
128
Popular Tags