Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
SpanWeight.cs
Go to the documentation of this file.
1 /*
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 
18 using System;
19 using System.Collections.Generic;
20 using Lucene.Net.Index;
21 using IndexReader = Lucene.Net.Index.IndexReader;
22 using Lucene.Net.Search;
23 using IDFExplanation = Lucene.Net.Search.Explanation.IDFExplanation;
24 
25 namespace Lucene.Net.Search.Spans
26 {
27 
28  /// <summary> Expert-only. Public for use by other weight implementations</summary>
29  [Serializable]
30  public class SpanWeight:Weight
31  {
32  protected internal Similarity similarity;
33  protected internal float value_Renamed;
34  protected internal float idf;
35  protected internal float queryNorm;
36  protected internal float queryWeight;
37 
38  protected internal ISet<Term> terms;
39  protected internal SpanQuery internalQuery;
40  private IDFExplanation idfExp;
41 
42  public SpanWeight(SpanQuery query, Searcher searcher)
43  {
44  this.similarity = query.GetSimilarity(searcher);
45  this.internalQuery = query;
46 
47  terms = Lucene.Net.Support.Compatibility.SetFactory.CreateHashSet<Term>();
48  query.ExtractTerms(terms);
49 
50  idfExp = similarity.IdfExplain(terms, searcher);
51  idf = idfExp.Idf;
52  }
53 
54  public override Query Query
55  {
56  get { return internalQuery; }
57  }
58 
59  public override float Value
60  {
61  get { return value_Renamed; }
62  }
63 
64  public override float GetSumOfSquaredWeights()
65  {
66  queryWeight = idf*internalQuery.Boost; // compute query weight
67  return queryWeight*queryWeight; // square it
68  }
69 
70  public override void Normalize(float queryNorm)
71  {
72  this.queryNorm = queryNorm;
73  queryWeight *= queryNorm; // normalize query weight
74  value_Renamed = queryWeight * idf; // idf for document
75  }
76 
77  public override Scorer Scorer(IndexReader reader, bool scoreDocsInOrder, bool topScorer)
78  {
79  return new SpanScorer(internalQuery.GetSpans(reader), this, similarity, reader.Norms(internalQuery.Field));
80  }
81 
82  public override Explanation Explain(IndexReader reader, int doc)
83  {
84 
86  result.Description = "weight(" + Query + " in " + doc + "), product of:";
87  System.String field = ((SpanQuery) Query).Field;
88 
89  Explanation idfExpl = new Explanation(idf, "idf(" + field + ": " + idfExp.Explain() + ")");
90 
91  // explain query weight
92  Explanation queryExpl = new Explanation();
93  queryExpl.Description = "queryWeight(" + Query + "), product of:";
94 
95  Explanation boostExpl = new Explanation(Query.Boost, "boost");
96  if (Query.Boost != 1.0f)
97  queryExpl.AddDetail(boostExpl);
98  queryExpl.AddDetail(idfExpl);
99 
100  Explanation queryNormExpl = new Explanation(queryNorm, "queryNorm");
101  queryExpl.AddDetail(queryNormExpl);
102 
103  queryExpl.Value = boostExpl.Value * idfExpl.Value * queryNormExpl.Value;
104 
105  result.AddDetail(queryExpl);
106 
107  // explain field weight
108  ComplexExplanation fieldExpl = new ComplexExplanation();
109  fieldExpl.Description = "fieldWeight(" + field + ":" + internalQuery.ToString(field) + " in " + doc + "), product of:";
110 
111  Explanation tfExpl = ((SpanScorer)Scorer(reader, true, false)).Explain(doc);
112  fieldExpl.AddDetail(tfExpl);
113  fieldExpl.AddDetail(idfExpl);
114 
115  Explanation fieldNormExpl = new Explanation();
116  byte[] fieldNorms = reader.Norms(field);
117  float fieldNorm = fieldNorms != null?Similarity.DecodeNorm(fieldNorms[doc]):1.0f;
118  fieldNormExpl.Value = fieldNorm;
119  fieldNormExpl.Description = "fieldNorm(field=" + field + ", doc=" + doc + ")";
120  fieldExpl.AddDetail(fieldNormExpl);
121 
122  fieldExpl.Match = tfExpl.IsMatch;
123  fieldExpl.Value = tfExpl.Value * idfExpl.Value * fieldNormExpl.Value;
124 
125  result.AddDetail(fieldExpl);
126  System.Boolean? tempAux = fieldExpl.Match;
127  result.Match = tempAux;
128 
129  // combine them
130  result.Value = queryExpl.Value * fieldExpl.Value;
131 
132  if (queryExpl.Value == 1.0f)
133  return fieldExpl;
134 
135  return result;
136  }
137  }
138 }