2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
;
26 private float weightValue
;
29 private int[] docs
= new int[32]; // buffered doc numbers
30 private int[] freqs
= new int[32]; // buffered term freqs
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
)
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()
53 public override bool Next()
56 if (pointer
>= pointerMax
)
58 pointerMax
= termDocs
.Read(docs
, freqs
); // refill buffer
65 termDocs
.Close(); // close stream
66 doc
= System
.Int32
.MaxValue
; // set to sentinel value
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
)
94 // not found in cache, seek underlying stream
95 bool result
= termDocs
.SkipTo(target
);
100 docs
[pointer
] = doc
= termDocs
.Doc();
101 freqs
[pointer
] = termDocs
.Freq();
105 doc
= System
.Int32
.MaxValue
;
110 public override Explanation
Explain(int doc
)
112 TermQuery query
= (TermQuery
) weight
.Query
;
113 Explanation tfExplanation
= new Explanation();
115 while (pointer
< pointerMax
)
117 if (docs
[pointer
] == doc
)
123 while (termDocs
.Next())
125 if (termDocs
.Doc() == doc
)
127 tf
= termDocs
.Freq();
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
+ ")";