📄 inaccuratebooleanweight2.java
字号:
package org.apache.lucene.search;
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.BooleanClause.Occur;
/**
* <p>
* Rewritten by caocao (http://www.caocao.name)
*
* <p>
* Most of source code in InaccurateBooleanWeight2 came from BooleanWeight2 and
* BooleanWeight. It can't extends BooleanWeight2 because BooleanWeight2 is
* invisiable.
*/
public class InaccurateBooleanWeight2 implements Weight {
private static final long serialVersionUID = 2340597771717234040L;
protected Searcher searcher;
protected float boost;
protected Similarity similarity;
protected List clauses = new ArrayList();
protected Vector weights = new Vector();
protected Query query;
@SuppressWarnings("unchecked")
public InaccurateBooleanWeight2(Searcher searcher, Query query)
throws IOException {
this.searcher = searcher;
this.query = query;
this.boost = query.getBoost();
this.similarity = query.getSimilarity(searcher);
if (query instanceof BooleanQuery) {
this.clauses = ((BooleanQuery) query).clauses();
}
for (int i = 0; i < clauses.size(); i++) {
BooleanClause c = (BooleanClause) clauses.get(i);
weights.add(c.getQuery().createWeight(searcher));
}
}
public Query getQuery() {
return query;
}
public float getValue() {
return boost;
}
public float sumOfSquaredWeights() throws IOException {
float sum = 0.0f;
for (int i = 0; i < weights.size(); i++) {
BooleanClause c = (BooleanClause) clauses.get(i);
Weight w = (Weight) weights.elementAt(i);
// call sumOfSquaredWeights for all clauses in case of side effects
float s = w.sumOfSquaredWeights(); // sum sub weights
if (!c.isProhibited())
// only add to sum for non-prohibited clauses
sum += s;
}
sum *= boost * boost; // boost each sub-weight
return sum;
}
public void normalize(float norm) {
norm *= boost; // incorporate boost
for (int i = 0; i < weights.size(); i++) {
@SuppressWarnings("unused")
BooleanClause c = (BooleanClause) clauses.get(i);
Weight w = (Weight) weights.elementAt(i);
// normalize all clauses, (even if prohibited in case of side
// affects)
w.normalize(norm);
}
}
/**
* @return An alternative Scorer that uses and provides skipTo(), and scores
* documents in document number order.
*/
public Scorer scorer(IndexReader reader) throws IOException {
return getInaccurateBooleanScorer2(reader, Integer.MAX_VALUE);
}
public InaccurateBooleanScorer2 getInaccurateBooleanScorer2(
IndexReader reader, int maxNumberOfDocs) throws IOException {
InaccurateBooleanScorer2 result = new InaccurateBooleanScorer2(
similarity, 0, maxNumberOfDocs);
for (int i = 0; i < weights.size(); i++) {
BooleanClause c = (BooleanClause) clauses.get(i);
Weight w = (Weight) weights.elementAt(i);
Scorer subScorer = w.scorer(reader);
if (subScorer != null)
result.add(subScorer, c.isRequired(), c.isProhibited());
else if (c.isRequired())
return null;
}
return result;
}
public Explanation explain(IndexReader reader, int doc) throws IOException {
final int minShouldMatch = 0;
ComplexExplanation sumExpl = new ComplexExplanation();
sumExpl.setDescription("sum of:");
int coord = 0;
int maxCoord = 0;
float sum = 0.0f;
boolean fail = false;
int shouldMatchCount = 0;
for (int i = 0; i < weights.size(); i++) {
BooleanClause c = (BooleanClause) clauses.get(i);
Weight w = (Weight) weights.elementAt(i);
Explanation e = w.explain(reader, doc);
if (!c.isProhibited())
maxCoord++;
if (e.isMatch()) {
if (!c.isProhibited()) {
sumExpl.addDetail(e);
sum += e.getValue();
coord++;
} else {
Explanation r = new Explanation(0.0f,
"match on prohibited clause ("
+ c.getQuery().toString() + ")");
r.addDetail(e);
sumExpl.addDetail(r);
fail = true;
}
if (c.getOccur().equals(Occur.SHOULD))
shouldMatchCount++;
} else if (c.isRequired()) {
Explanation r = new Explanation(0.0f,
"no match on required clause ("
+ c.getQuery().toString() + ")");
r.addDetail(e);
sumExpl.addDetail(r);
fail = true;
}
}
if (fail) {
sumExpl.setMatch(Boolean.FALSE);
sumExpl.setValue(0.0f);
sumExpl
.setDescription("Failure to meet condition(s) of required/prohibited clause(s)");
return sumExpl;
} else if (shouldMatchCount < minShouldMatch) {
sumExpl.setMatch(Boolean.FALSE);
sumExpl.setValue(0.0f);
sumExpl.setDescription("Failure to match minimum number "
+ "of optional clauses: " + minShouldMatch);
return sumExpl;
}
sumExpl.setMatch(0 < coord ? Boolean.TRUE : Boolean.FALSE);
sumExpl.setValue(sum);
float coordFactor = similarity.coord(coord, maxCoord);
if (coordFactor == 1.0f) // coord is no-op
return sumExpl; // eliminate wrapper
else {
ComplexExplanation result = new ComplexExplanation(sumExpl
.isMatch(), sum * coordFactor, "product of:");
result.addDetail(sumExpl);
result.addDetail(new Explanation(coordFactor, "coord(" + coord
+ "/" + maxCoord + ")"));
return result;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -