在构建文档时,给字段 setBoost 爆异常:
java.lang.IllegalArgumentException: You cannot set an index-time boost on an unindexed field, or one that omits norms
代码为:
Document doc = new Document();
Field f = new FloatField("x", 10.0, Field.Store.YES);
f.setBoost(10.0);
doc.add(f);
Lucene 版本 4.6.0, 求指教!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
FieldType fieldType = new FieldType(StringField.TYPE_STORED);
fieldType.setOmitNorms(false);
Field countryField = new Field("country", country, getNormStringField());
countryField.setBoost("China".equals(country) ? 5f : 1f);
FloatField是不支持setBoost的。需要创建自定义的FieldType
总算搞清楚点了,不能在 omitNorms 的字段上 setBoost,而 Lucene 4.x 的 StringField, StoredField, IntField 等都是 omitNorms 的,我测试发现只有 TextField 是非 omitNorms 的,故可以在 TextFields 上 setBoost。
现在有个新的问题,给其中一个字段设置了 boost,但如果条件里没有这个字段,顺序就没任何变化。而 4.x 的 Document 上并没有 setBoost 方法。
刚实践了一个好办法,添加了一个 TextField 且所有索引记录的这个字段的值都是一个相同的字,然后在这个字段上 setBoost ,在查询条件上默认的加上这个字段的查询,关系是 SHOULD,就能实现按打分+相关度共同排序了。