下面列出了org.apache.lucene.index.LeafReader#getNormValues ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testSimilarities() throws IOException {
MemoryIndex mi = new MemoryIndex();
mi.addField("f1", "a long text field that contains many many terms", analyzer);
IndexSearcher searcher = mi.createSearcher();
LeafReader reader = (LeafReader) searcher.getIndexReader();
NumericDocValues norms = reader.getNormValues("f1");
assertEquals(0, norms.nextDoc());
float n1 = norms.longValue();
// Norms are re-computed when we change the Similarity
mi.setSimilarity(new Similarity() {
@Override
public long computeNorm(FieldInvertState state) {
return 74;
}
@Override
public SimScorer scorer(float boost, CollectionStatistics collectionStats, TermStatistics... termStats) {
throw new UnsupportedOperationException();
}
});
norms = reader.getNormValues("f1");
assertEquals(0, norms.nextDoc());
float n2 = norms.longValue();
assertTrue(n1 != n2);
TestUtil.checkReader(reader);
}
@Test
public void testOmitNorms() throws IOException {
MemoryIndex mi = new MemoryIndex();
FieldType ft = new FieldType();
ft.setTokenized(true);
ft.setIndexOptions(IndexOptions.DOCS_AND_FREQS);
ft.setOmitNorms(true);
mi.addField(new Field("f1", "some text in here", ft), analyzer);
mi.freeze();
LeafReader leader = (LeafReader) mi.createSearcher().getIndexReader();
NumericDocValues norms = leader.getNormValues("f1");
assertNull(norms);
}
/**
* Sole constructor: Score documents of {@code reader} with {@code scorer}.
*
*/
MultiNormsLeafSimScorer(SimScorer scorer, LeafReader reader, Collection<FieldAndWeight> normFields, boolean needsScores) throws IOException {
this.scorer = Objects.requireNonNull(scorer);
if (needsScores) {
final List<NumericDocValues> normsList = new ArrayList<>();
final List<Float> weightList = new ArrayList<>();
for (FieldAndWeight field : normFields) {
NumericDocValues norms = reader.getNormValues(field.field);
if (norms != null) {
normsList.add(norms);
weightList.add(field.weight);
}
}
if (normsList.isEmpty()) {
norms = null;
} else if (normsList.size() == 1) {
norms = normsList.get(0);
} else {
final NumericDocValues[] normsArr = normsList.toArray(new NumericDocValues[0]);
final float[] weightArr = new float[normsList.size()];
for (int i = 0; i < weightList.size(); i++) {
weightArr[i] = weightList.get(i);
}
norms = new MultiFieldNormValues(normsArr, weightArr);
}
} else {
norms = null;
}
}
private static void checkReaderSanity(LeafReader reader) throws IOException {
for (FieldInfo info : reader.getFieldInfos()) {
// reader shouldn't return normValues if the field does not have them
if (!info.hasNorms()) {
if (reader.getNormValues(info.name) != null) {
throw new RuntimeException("field: " + info.name + " should omit norms but has them!");
}
}
// reader shouldn't return docValues if the field does not have them
// reader shouldn't return multiple docvalues types for the same field.
switch(info.getDocValuesType()) {
case NONE:
if (reader.getBinaryDocValues(info.name) != null ||
reader.getNumericDocValues(info.name) != null ||
reader.getSortedDocValues(info.name) != null ||
reader.getSortedSetDocValues(info.name) != null) {
throw new RuntimeException("field: " + info.name + " has docvalues but should omit them!");
}
break;
case SORTED:
if (reader.getBinaryDocValues(info.name) != null ||
reader.getNumericDocValues(info.name) != null ||
reader.getSortedNumericDocValues(info.name) != null ||
reader.getSortedSetDocValues(info.name) != null) {
throw new RuntimeException(info.name + " returns multiple docvalues types!");
}
break;
case SORTED_NUMERIC:
if (reader.getBinaryDocValues(info.name) != null ||
reader.getNumericDocValues(info.name) != null ||
reader.getSortedSetDocValues(info.name) != null ||
reader.getSortedDocValues(info.name) != null) {
throw new RuntimeException(info.name + " returns multiple docvalues types!");
}
break;
case SORTED_SET:
if (reader.getBinaryDocValues(info.name) != null ||
reader.getNumericDocValues(info.name) != null ||
reader.getSortedNumericDocValues(info.name) != null ||
reader.getSortedDocValues(info.name) != null) {
throw new RuntimeException(info.name + " returns multiple docvalues types!");
}
break;
case BINARY:
if (reader.getNumericDocValues(info.name) != null ||
reader.getSortedDocValues(info.name) != null ||
reader.getSortedNumericDocValues(info.name) != null ||
reader.getSortedSetDocValues(info.name) != null) {
throw new RuntimeException(info.name + " returns multiple docvalues types!");
}
break;
case NUMERIC:
if (reader.getBinaryDocValues(info.name) != null ||
reader.getSortedDocValues(info.name) != null ||
reader.getSortedNumericDocValues(info.name) != null ||
reader.getSortedSetDocValues(info.name) != null) {
throw new RuntimeException(info.name + " returns multiple docvalues types!");
}
break;
default:
throw new AssertionError();
}
}
}
/**
* Sole constructor: Score documents of {@code reader} with {@code scorer}.
*/
public LeafSimScorer(SimScorer scorer, LeafReader reader, String field, boolean needsScores) throws IOException {
this.scorer = Objects.requireNonNull(scorer);
norms = needsScores ? reader.getNormValues(field) : null;
}