KickJava   Java API By Example, From Geeks To Geeks.

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


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

17
18 import java.io.IOException JavaDoc;
19
20 /** A Scorer for queries with a required part and an optional part.
21  * Delays skipTo() on the optional part until a score() is needed.
22  * <br>
23  * This <code>Scorer</code> implements {@link Scorer#skipTo(int)}.
24  */

25 public class ReqOptSumScorer extends Scorer {
26   /** The scorers passed from the constructor.
27    * These are set to null as soon as their next() or skipTo() returns false.
28    */

29   private Scorer reqScorer;
30   private Scorer optScorer;
31
32   /** Construct a <code>ReqOptScorer</code>.
33    * @param reqScorer The required scorer. This must match.
34    * @param optScorer The optional scorer. This is used for scoring only.
35    */

36   public ReqOptSumScorer(
37       Scorer reqScorer,
38       Scorer optScorer)
39   {
40     super(null); // No similarity used.
41
this.reqScorer = reqScorer;
42     this.optScorer = optScorer;
43   }
44
45   private boolean firstTimeOptScorer = true;
46
47   public boolean next() throws IOException JavaDoc {
48     return reqScorer.next();
49   }
50
51   public boolean skipTo(int target) throws IOException JavaDoc {
52     return reqScorer.skipTo(target);
53   }
54
55   public int doc() {
56     return reqScorer.doc();
57   }
58
59   /** Returns the score of the current document matching the query.
60    * Initially invalid, until {@link #next()} is called the first time.
61    * @return The score of the required scorer, eventually increased by the score
62    * of the optional scorer when it also matches the current document.
63    */

64   public float score() throws IOException JavaDoc {
65     int curDoc = reqScorer.doc();
66     float reqScore = reqScorer.score();
67     if (firstTimeOptScorer) {
68       firstTimeOptScorer = false;
69       if (! optScorer.skipTo(curDoc)) {
70         optScorer = null;
71         return reqScore;
72       }
73     } else if (optScorer == null) {
74       return reqScore;
75     } else if ((optScorer.doc() < curDoc) && (! optScorer.skipTo(curDoc))) {
76       optScorer = null;
77       return reqScore;
78     }
79     // assert (optScorer != null) && (optScorer.doc() >= curDoc);
80
return (optScorer.doc() == curDoc)
81        ? reqScore + optScorer.score()
82        : reqScore;
83   }
84
85   /** Explain the score of a document.
86    * @todo Also show the total score.
87    * See BooleanScorer.explain() on how to do this.
88    */

89   public Explanation explain(int doc) throws IOException JavaDoc {
90     Explanation res = new Explanation();
91     res.setDescription("required, optional");
92     res.addDetail(reqScorer.explain(doc));
93     res.addDetail(optScorer.explain(doc));
94     return res;
95   }
96 }
97
98
Popular Tags