2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 using IndexReader = Mono.Lucene.Net.Index.IndexReader;
21 using Mono.Lucene.Net.Search;
22 using IDFExplanation = Mono.Lucene.Net.Search.Explanation.IDFExplanation;
24 namespace Mono.Lucene.Net.Search.Spans
27 /// <summary> Expert-only. Public for use by other weight implementations</summary>
29 public class SpanWeight:Weight
31 protected internal Similarity similarity;
32 protected internal float value_Renamed;
33 protected internal float idf;
34 protected internal float queryNorm;
35 protected internal float queryWeight;
37 protected internal System.Collections.Hashtable terms;
38 protected internal SpanQuery query;
39 private IDFExplanation idfExp;
41 public SpanWeight(SpanQuery query, Searcher searcher)
43 this.similarity = query.GetSimilarity(searcher);
45 terms = new System.Collections.Hashtable();
46 query.ExtractTerms(terms);
47 idfExp = similarity.idfExplain(new System.Collections.ArrayList(terms.Values), searcher);
48 idf = idfExp.GetIdf();
51 public override Query GetQuery()
55 public override float GetValue()
60 public override float SumOfSquaredWeights()
62 queryWeight = idf * query.GetBoost(); // compute query weight
63 return queryWeight * queryWeight; // square it
66 public override void Normalize(float queryNorm)
68 this.queryNorm = queryNorm;
69 queryWeight *= queryNorm; // normalize query weight
70 value_Renamed = queryWeight * idf; // idf for document
73 public override Scorer Scorer(IndexReader reader, bool scoreDocsInOrder, bool topScorer)
75 return new SpanScorer(query.GetSpans(reader), this, similarity, reader.Norms(query.GetField()));
78 public override Explanation Explain(IndexReader reader, int doc)
81 ComplexExplanation result = new ComplexExplanation();
82 result.SetDescription("weight(" + GetQuery() + " in " + doc + "), product of:");
83 System.String field = ((SpanQuery) GetQuery()).GetField();
85 Explanation idfExpl = new Explanation(idf, "idf(" + field + ": " + idfExp.Explain() + ")");
87 // explain query weight
88 Explanation queryExpl = new Explanation();
89 queryExpl.SetDescription("queryWeight(" + GetQuery() + "), product of:");
91 Explanation boostExpl = new Explanation(GetQuery().GetBoost(), "boost");
92 if (GetQuery().GetBoost() != 1.0f)
93 queryExpl.AddDetail(boostExpl);
94 queryExpl.AddDetail(idfExpl);
96 Explanation queryNormExpl = new Explanation(queryNorm, "queryNorm");
97 queryExpl.AddDetail(queryNormExpl);
99 queryExpl.SetValue(boostExpl.GetValue() * idfExpl.GetValue() * queryNormExpl.GetValue());
101 result.AddDetail(queryExpl);
103 // explain field weight
104 ComplexExplanation fieldExpl = new ComplexExplanation();
105 fieldExpl.SetDescription("fieldWeight(" + field + ":" + query.ToString(field) + " in " + doc + "), product of:");
107 Explanation tfExpl = Scorer(reader, true, false).Explain(doc);
108 fieldExpl.AddDetail(tfExpl);
109 fieldExpl.AddDetail(idfExpl);
111 Explanation fieldNormExpl = new Explanation();
112 byte[] fieldNorms = reader.Norms(field);
113 float fieldNorm = fieldNorms != null?Similarity.DecodeNorm(fieldNorms[doc]):1.0f;
114 fieldNormExpl.SetValue(fieldNorm);
115 fieldNormExpl.SetDescription("fieldNorm(field=" + field + ", doc=" + doc + ")");
116 fieldExpl.AddDetail(fieldNormExpl);
118 fieldExpl.SetMatch(tfExpl.IsMatch());
119 fieldExpl.SetValue(tfExpl.GetValue() * idfExpl.GetValue() * fieldNormExpl.GetValue());
121 result.AddDetail(fieldExpl);
122 System.Boolean? tempAux = fieldExpl.GetMatch();
123 result.SetMatch(tempAux);
126 result.SetValue(queryExpl.GetValue() * fieldExpl.GetValue());
128 if (queryExpl.GetValue() == 1.0f)