我有一个具有以下要求的字段:
至少在另外一个领域,我确实想要字段长度标准化,所以我不怀疑在Searcher上广泛应用自定义相似性是合适的。
如何在索引时提升单个字段,但禁用字段长度规范化的效果?
答案 0 :(得分:1)
您可以使用PerFieldSimilarityWrapper为每个字段使用不同的相似性实现:
public class MySimilarity extends PerFieldSimilarityWrapper {
Similarity standardSim = new ClassicSimilarity();
Similarity nolengthSim = new SimilarityWithoutLengthNorm();
@Override
public Similarity get(String fieldName) {
if (fieldName.equals("someField")) {
return nolengthSim;
}
else {
return standardSim;
}
}
//These two methods must be implemented here, as their
//calculation is not field specific
@Override
public float queryNorm (float valueForNormalization) {
return standardSim.queryNorm(valueForNormalization);
}
@Override
public float coord (int overlap, int maxOverlap) {
return standardSim.coord(overlap, maxOverlap);
}
}
SimilarityWithoutLengthNorm
看起来像:
public class SimilarityWithoutLengthNorm extends ClassicSimilarity{
@Override
public float lengthNorm(FieldInvertState state) {
return 1;
}
}