1
package org.apache.lucene.search.spans;
4
* Licensed to the Apache Software Foundation (ASF) under one or more
5
* contributor license agreements. See the NOTICE file distributed with
6
* this work for additional information regarding copyright ownership.
7
* The ASF licenses this file to You under the Apache License, Version 2.0
8
* (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
20
import org.apache.lucene.index.IndexReader;
21
import org.apache.lucene.index.Term;
22
import org.apache.lucene.search.*;
23
import org.apache.lucene.search.Explanation.IDFExplanation;
25
import java.io.IOException;
26
import java.util.HashSet;
30
* Expert-only. Public for use by other weight implementations
32
public class SpanWeight extends Weight {
33
protected Similarity similarity;
34
protected float value;
36
protected float queryNorm;
37
protected float queryWeight;
39
protected Set<Term> terms;
40
protected SpanQuery query;
41
private IDFExplanation idfExp;
43
public SpanWeight(SpanQuery query, Searcher searcher)
45
this.similarity = query.getSimilarity(searcher);
48
terms=new HashSet<Term>();
49
query.extractTerms(terms);
51
idfExp = similarity.idfExplain(terms, searcher);
52
idf = idfExp.getIdf();
56
public Query getQuery() { return query; }
59
public float getValue() { return value; }
62
public float sumOfSquaredWeights() throws IOException {
63
queryWeight = idf * query.getBoost(); // compute query weight
64
return queryWeight * queryWeight; // square it
68
public void normalize(float queryNorm) {
69
this.queryNorm = queryNorm;
70
queryWeight *= queryNorm; // normalize query weight
71
value = queryWeight * idf; // idf for document
75
public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException {
76
return new SpanScorer(query.getSpans(reader), this, similarity, reader
77
.norms(query.getField()));
81
public Explanation explain(IndexReader reader, int doc)
84
ComplexExplanation result = new ComplexExplanation();
85
result.setDescription("weight("+getQuery()+" in "+doc+"), product of:");
86
String field = ((SpanQuery)getQuery()).getField();
89
new Explanation(idf, "idf(" + field + ": " + idfExp.explain() + ")");
91
// explain query weight
92
Explanation queryExpl = new Explanation();
93
queryExpl.setDescription("queryWeight(" + getQuery() + "), product of:");
95
Explanation boostExpl = new Explanation(getQuery().getBoost(), "boost");
96
if (getQuery().getBoost() != 1.0f)
97
queryExpl.addDetail(boostExpl);
98
queryExpl.addDetail(idfExpl);
100
Explanation queryNormExpl = new Explanation(queryNorm,"queryNorm");
101
queryExpl.addDetail(queryNormExpl);
103
queryExpl.setValue(boostExpl.getValue() *
105
queryNormExpl.getValue());
107
result.addDetail(queryExpl);
109
// explain field weight
110
ComplexExplanation fieldExpl = new ComplexExplanation();
111
fieldExpl.setDescription("fieldWeight("+field+":"+query.toString(field)+
112
" in "+doc+"), product of:");
114
Explanation tfExpl = ((SpanScorer)scorer(reader, true, false)).explain(doc);
115
fieldExpl.addDetail(tfExpl);
116
fieldExpl.addDetail(idfExpl);
118
Explanation fieldNormExpl = new Explanation();
119
byte[] fieldNorms = reader.norms(field);
121
fieldNorms!=null ? similarity.decodeNormValue(fieldNorms[doc]) : 1.0f;
122
fieldNormExpl.setValue(fieldNorm);
123
fieldNormExpl.setDescription("fieldNorm(field="+field+", doc="+doc+")");
124
fieldExpl.addDetail(fieldNormExpl);
126
fieldExpl.setMatch(Boolean.valueOf(tfExpl.isMatch()));
127
fieldExpl.setValue(tfExpl.getValue() *
129
fieldNormExpl.getValue());
131
result.addDetail(fieldExpl);
132
result.setMatch(fieldExpl.getMatch());
135
result.setValue(queryExpl.getValue() * fieldExpl.getValue());
137
if (queryExpl.getValue() == 1.0f)