Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Search / Spans / SpanWeight.cs
blob246d566d09f1a02c843a9606945a3e28d1a65b93
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 IndexReader = Lucene.Net.Index.IndexReader;
18 using Term = Lucene.Net.Index.Term;
19 using Explanation = Lucene.Net.Search.Explanation;
20 using Query = Lucene.Net.Search.Query;
21 using Scorer = Lucene.Net.Search.Scorer;
22 using Searcher = Lucene.Net.Search.Searcher;
23 using Similarity = Lucene.Net.Search.Similarity;
24 using Weight = Lucene.Net.Search.Weight;
25 namespace Lucene.Net.Search.Spans
28 [Serializable]
29 class SpanWeight : Weight
31 virtual public Query Query
33 get
35 return query;
39 virtual public float Value
41 get
43 return value_Renamed;
47 private Searcher searcher;
48 private float value_Renamed;
49 private float idf;
50 private float queryNorm;
51 private float queryWeight;
53 private System.Collections.ICollection terms;
54 private SpanQuery query;
56 public SpanWeight(SpanQuery query, Searcher searcher)
58 this.searcher = searcher;
59 this.query = query;
60 this.terms = query.GetTerms();
63 public virtual float SumOfSquaredWeights()
65 idf = this.query.GetSimilarity(searcher).Idf(terms, searcher);
66 queryWeight = idf * query.GetBoost(); // compute query weight
67 return queryWeight * queryWeight; // square it
70 public virtual void Normalize(float queryNorm)
72 this.queryNorm = queryNorm;
73 queryWeight *= queryNorm; // normalize query weight
74 value_Renamed = queryWeight * idf; // idf for document
77 public virtual Scorer Scorer(IndexReader reader)
79 return new SpanScorer(query.GetSpans(reader), this, query.GetSimilarity(searcher), reader.Norms(query.GetField()));
82 public virtual Explanation Explain(IndexReader reader, int doc)
85 Explanation result = new Explanation();
86 result.SetDescription("weight(" + Query + " in " + doc + "), product of:");
87 System.String field = ((SpanQuery) Query).GetField();
89 System.Text.StringBuilder docFreqs = new System.Text.StringBuilder();
90 System.Collections.IEnumerator i = terms.GetEnumerator();
91 while (i.MoveNext())
93 Term term = (Term) i.Current;
94 docFreqs.Append(term.Text());
95 docFreqs.Append("=");
96 docFreqs.Append(searcher.DocFreq(term));
98 if (i.MoveNext())
100 docFreqs.Append(" ");
104 Explanation idfExpl = new Explanation(idf, "idf(" + field + ": " + docFreqs + ")");
106 // explain query weight
107 Explanation queryExpl = new Explanation();
108 queryExpl.SetDescription("queryWeight(" + Query + "), product of:");
110 Explanation boostExpl = new Explanation(Query.GetBoost(), "boost");
111 if (Query.GetBoost() != 1.0f)
112 queryExpl.AddDetail(boostExpl);
113 queryExpl.AddDetail(idfExpl);
115 Explanation queryNormExpl = new Explanation(queryNorm, "queryNorm");
116 queryExpl.AddDetail(queryNormExpl);
118 queryExpl.SetValue(boostExpl.GetValue() * idfExpl.GetValue() * queryNormExpl.GetValue());
120 result.AddDetail(queryExpl);
122 // explain Field weight
123 Explanation fieldExpl = new Explanation();
124 fieldExpl.SetDescription("fieldWeight(" + field + ":" + query.ToString(field) + " in " + doc + "), product of:");
126 Explanation tfExpl = Scorer(reader).Explain(doc);
127 fieldExpl.AddDetail(tfExpl);
128 fieldExpl.AddDetail(idfExpl);
130 Explanation fieldNormExpl = new Explanation();
131 byte[] fieldNorms = reader.Norms(field);
132 float fieldNorm = fieldNorms != null ? Similarity.DecodeNorm(fieldNorms[doc]) : 0.0f;
133 fieldNormExpl.SetValue(fieldNorm);
134 fieldNormExpl.SetDescription("fieldNorm(Field=" + field + ", doc=" + doc + ")");
135 fieldExpl.AddDetail(fieldNormExpl);
137 fieldExpl.SetValue(tfExpl.GetValue() * idfExpl.GetValue() * fieldNormExpl.GetValue());
139 result.AddDetail(fieldExpl);
141 // combine them
142 result.SetValue(queryExpl.GetValue() * fieldExpl.GetValue());
144 if (queryExpl.GetValue() == 1.0f)
145 return fieldExpl;
147 return result;