文档字段setBoost中的lucene错误

时间:2015-01-23 11:47:13

标签: java lucene

正如提到的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

我搜索了谷歌。但我没有答案。你能帮帮我吗?

1 个答案:

答案 0 :(得分:2)

索引时间提升存储在字段的范数中,默认情况下,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);