1 package org.apache.lucene.search.spans;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
22 import org.apache.lucene.search.Explanation;
23 import org.apache.lucene.search.Weight;
24 import org.apache.lucene.search.Scorer;
25 import org.apache.lucene.search.Similarity;
28 * Public for extension only.
30 public class SpanScorer extends Scorer {
31 protected Spans spans;
32 protected byte[] norms;
33 protected float value;
35 protected boolean more = true;
40 protected SpanScorer(Spans spans, Weight weight, Similarity similarity, byte[] norms)
42 super(similarity, weight);
45 this.value = weight.getValue();
46 if (this.spans.next()) {
55 public int nextDoc() throws IOException {
56 if (!setFreqCurrentDoc()) {
63 public int advance(int target) throws IOException {
65 return doc = NO_MORE_DOCS;
67 if (spans.doc() < target) { // setFreqCurrentDoc() leaves spans.doc() ahead
68 more = spans.skipTo(target);
70 if (!setFreqCurrentDoc()) {
76 protected boolean setFreqCurrentDoc() throws IOException {
83 int matchLength = spans.end() - spans.start();
84 freq += getSimilarity().sloppyFreq(matchLength);
86 } while (more && (doc == spans.doc()));
91 public int docID() { return doc; }
94 public float score() throws IOException {
95 float raw = getSimilarity().tf(freq) * value; // raw score
96 return norms == null? raw : raw * getSimilarity().decodeNormValue(norms[doc]); // normalize
100 public float freq() throws IOException {
104 /** This method is no longer an official member of {@link Scorer},
105 * but it is needed by SpanWeight to build an explanation. */
106 protected Explanation explain(final int doc) throws IOException {
107 Explanation tfExplanation = new Explanation();
109 int expDoc = advance(doc);
111 float phraseFreq = (expDoc == doc) ? freq : 0.0f;
112 tfExplanation.setValue(getSimilarity().tf(phraseFreq));
113 tfExplanation.setDescription("tf(phraseFreq=" + phraseFreq + ")");
115 return tfExplanation;