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 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
29 class SpanWeight
: Weight
31 virtual public Query Query
39 virtual public float Value
47 private Searcher searcher
;
48 private float value_Renamed
;
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
;
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();
93 Term term
= (Term
) i
.Current
;
94 docFreqs
.Append(term
.Text());
96 docFreqs
.Append(searcher
.DocFreq(term
));
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
);
142 result
.SetValue(queryExpl
.GetValue() * fieldExpl
.GetValue());
144 if (queryExpl
.GetValue() == 1.0f
)