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.
18 using IndexReader
= Lucene
.Net
.Index
.IndexReader
;
19 using Term
= Lucene
.Net
.Index
.Term
;
20 using Explanation
= Lucene
.Net
.Search
.Explanation
;
21 using Query
= Lucene
.Net
.Search
.Query
;
22 using Scorer
= Lucene
.Net
.Search
.Scorer
;
23 using Searcher
= Lucene
.Net
.Search
.Searcher
;
24 using Similarity
= Lucene
.Net
.Search
.Similarity
;
25 using Weight
= Lucene
.Net
.Search
.Weight
;
27 namespace Lucene
.Net
.Search
.Spans
31 class SpanWeight
: Weight
33 private Similarity similarity
;
34 private float value_Renamed
;
36 private float queryNorm
;
37 private float queryWeight
;
39 private System
.Collections
.ICollection terms
;
40 private SpanQuery query
;
42 public SpanWeight(SpanQuery query
, Searcher searcher
)
44 this.similarity
= query
.GetSimilarity(searcher
);
46 this.terms
= query
.GetTerms();
48 idf
= this.query
.GetSimilarity(searcher
).Idf(terms
, searcher
);
51 public virtual Query
GetQuery()
55 public virtual float GetValue()
60 public virtual float SumOfSquaredWeights()
62 queryWeight
= idf
* query
.GetBoost(); // compute query weight
63 return queryWeight
* queryWeight
; // square it
66 public virtual void Normalize(float queryNorm
)
68 this.queryNorm
= queryNorm
;
69 queryWeight
*= queryNorm
; // normalize query weight
70 value_Renamed
= queryWeight
* idf
; // idf for document
73 public virtual Scorer
Scorer(IndexReader reader
)
75 return new SpanScorer(query
.GetSpans(reader
), this, similarity
, reader
.Norms(query
.GetField()));
78 public virtual Explanation
Explain(IndexReader reader
, int doc
)
81 Explanation result
= new Explanation();
82 result
.SetDescription("weight(" + GetQuery() + " in " + doc
+ "), product of:");
83 System
.String field
= ((SpanQuery
) GetQuery()).GetField();
85 System
.Text
.StringBuilder docFreqs
= new System
.Text
.StringBuilder();
86 System
.Collections
.IEnumerator i
= terms
.GetEnumerator();
89 Term term
= (Term
) i
.Current
;
90 docFreqs
.Append(term
.Text());
92 docFreqs
.Append(reader
.DocFreq(term
));
100 Explanation idfExpl
= new Explanation(idf
, "idf(" + field
+ ": " + docFreqs
+ ")");
102 // explain query weight
103 Explanation queryExpl
= new Explanation();
104 queryExpl
.SetDescription("queryWeight(" + GetQuery() + "), product of:");
106 Explanation boostExpl
= new Explanation(GetQuery().GetBoost(), "boost");
107 if (GetQuery().GetBoost() != 1.0f
)
108 queryExpl
.AddDetail(boostExpl
);
109 queryExpl
.AddDetail(idfExpl
);
111 Explanation queryNormExpl
= new Explanation(queryNorm
, "queryNorm");
112 queryExpl
.AddDetail(queryNormExpl
);
114 queryExpl
.SetValue(boostExpl
.GetValue() * idfExpl
.GetValue() * queryNormExpl
.GetValue());
116 result
.AddDetail(queryExpl
);
118 // explain field weight
119 Explanation fieldExpl
= new Explanation();
120 fieldExpl
.SetDescription("fieldWeight(" + field
+ ":" + query
.ToString(field
) + " in " + doc
+ "), product of:");
122 Explanation tfExpl
= Scorer(reader
).Explain(doc
);
123 fieldExpl
.AddDetail(tfExpl
);
124 fieldExpl
.AddDetail(idfExpl
);
126 Explanation fieldNormExpl
= new Explanation();
127 byte[] fieldNorms
= reader
.Norms(field
);
128 float fieldNorm
= fieldNorms
!= null ? Similarity
.DecodeNorm(fieldNorms
[doc
]) : 0.0f
;
129 fieldNormExpl
.SetValue(fieldNorm
);
130 fieldNormExpl
.SetDescription("fieldNorm(field=" + field
+ ", doc=" + doc
+ ")");
131 fieldExpl
.AddDetail(fieldNormExpl
);
133 fieldExpl
.SetValue(tfExpl
.GetValue() * idfExpl
.GetValue() * fieldNormExpl
.GetValue());
135 result
.AddDetail(fieldExpl
);
138 result
.SetValue(queryExpl
.GetValue() * fieldExpl
.GetValue());
140 if (queryExpl
.GetValue() == 1.0f
)