gpt4 book ai didi

java - lucene 文档字段 setBoost 错误

转载 作者:行者123 更新时间:2023-12-02 05:02:36 24 4
gpt4 key购买 nike

正如 lucene 迁移指南提到的,要设置文档级别提升,我们应该将所有字段提升乘以提升值。这是我的代码:

    StringField nameField = new StringField("name", name, Field.Store.YES) ;
StringField linkField = new StringField("link", link, Field.Store.YES);
Field descField;
TextField reviewsField = new TextField("reviews", reviews_str, Field.Store.YES);
TextField authorsField = new TextField("authors", authors_str, Field.Store.YES);
FloatField scoreField = new FloatField("score", origScore,Field.Store.YES);
if (desc != null) {
descField = new TextField("desc", desc, Field.Store.YES);
} else {
descField = new TextField("desc", "", Field.Store.YES);
}


doc.add(nameField);
doc.add(linkField);
doc.add(descField);
doc.add(reviewsField);
doc.add(authorsField);
doc.add(scoreField);


nameField.setBoost(score);
linkField.setBoost(score);
descField.setBoost(score);
reviewsField.setBoost(score);
authorsField.setBoost(score);
scoreField.setBoost(score);

但是我在运行代码时遇到了这个异常:

Exception in thread "main" java.lang.IllegalArgumentException: You cannot set an index-time boost on an unindexed field, or one that omits norms

我用谷歌搜索过。但我没有答案。你能帮我一下吗?

最佳答案

索引时间提升存储在字段的范数中,并且 StringField 和 FloatField 默认情况下都会忽略范数。因此,您需要在设置增强之前将它们打开。

要启用规范,您需要定义自己的字段类型:

//Start with a copy of the standard field type
FieldType myStringType = new FieldType(StringField.TYPE_STORED);
myStringType.setOmitNorms(false);
//StringField doesn't do anything special except have a customized fieldtype, so just use Field.
Field nameField = new Field("name", name, myStringType);

FieldType myFloatType = new FieldType(FloatField.TYPE_STORED);
myFloatType.setOmitNorms(false);
//For FloatField, use the appropriate FloatField ctor, instead of Field (similar for other numerics)
Field scoreField = new FloatField("score", origScore, myFloatType);

关于java - lucene 文档字段 setBoost 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28109237/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com