Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Search / TermScorer.cs
blob91e40c7f1bcf4c778652c5c4d0b112dc57ad147f
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 using System;
17 using TermDocs = Lucene.Net.Index.TermDocs;
18 namespace Lucene.Net.Search
21 sealed class TermScorer:Scorer
23 private Weight weight;
24 private TermDocs termDocs;
25 private byte[] norms;
26 private float weightValue;
27 private int doc;
29 private int[] docs = new int[32]; // buffered doc numbers
30 private int[] freqs = new int[32]; // buffered term freqs
31 private int pointer;
32 private int pointerMax;
34 private const int SCORE_CACHE_SIZE = 32;
35 private float[] scoreCache = new float[SCORE_CACHE_SIZE];
37 internal TermScorer(Weight weight, TermDocs td, Similarity similarity, byte[] norms) : base(similarity)
39 this.weight = weight;
40 this.termDocs = td;
41 this.norms = norms;
42 this.weightValue = weight.Value;
44 for (int i = 0; i < SCORE_CACHE_SIZE; i++)
45 scoreCache[i] = GetSimilarity().Tf(i) * weightValue;
48 public override int Doc()
50 return doc;
53 public override bool Next()
55 pointer++;
56 if (pointer >= pointerMax)
58 pointerMax = termDocs.Read(docs, freqs); // refill buffer
59 if (pointerMax != 0)
61 pointer = 0;
63 else
65 termDocs.Close(); // close stream
66 doc = System.Int32.MaxValue; // set to sentinel value
67 return false;
70 doc = docs[pointer];
71 return true;
74 public override float Score()
76 int f = freqs[pointer];
77 float raw = f < SCORE_CACHE_SIZE ? scoreCache[f] : GetSimilarity().Tf(f) * weightValue; // cache miss
79 return raw * Similarity.DecodeNorm(norms[doc]); // normalize for Field
82 public override bool SkipTo(int target)
84 // first scan in cache
85 for (pointer++; pointer < pointerMax; pointer++)
87 if (docs[pointer] >= target)
89 doc = docs[pointer];
90 return true;
94 // not found in cache, seek underlying stream
95 bool result = termDocs.SkipTo(target);
96 if (result)
98 pointerMax = 1;
99 pointer = 0;
100 docs[pointer] = doc = termDocs.Doc();
101 freqs[pointer] = termDocs.Freq();
103 else
105 doc = System.Int32.MaxValue;
107 return result;
110 public override Explanation Explain(int doc)
112 TermQuery query = (TermQuery) weight.Query;
113 Explanation tfExplanation = new Explanation();
114 int tf = 0;
115 while (pointer < pointerMax)
117 if (docs[pointer] == doc)
118 tf = freqs[pointer];
119 pointer++;
121 if (tf == 0)
123 while (termDocs.Next())
125 if (termDocs.Doc() == doc)
127 tf = termDocs.Freq();
131 termDocs.Close();
132 tfExplanation.SetValue(GetSimilarity().Tf(tf));
133 tfExplanation.SetDescription("tf(termFreq(" + query.GetTerm() + ")=" + tf + ")");
135 return tfExplanation;
138 public override System.String ToString()
140 return "scorer(" + weight + ")";