简体   繁体   中英

lucene error in document field setBoost

As the lucene migration guide mentioned, to set document level boost we should multiply all fields boost by boosting value. here is my code :

    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);

but I've got this exception when running code :

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

I've searched google. but I've got no answers. would you please help me?

Index-time boosts are stored in the field's norm, and both StringField and FloatField omit norms by default. So, you'll need to turn them on before you set the boosts.

To turn norms on, you'll need to define your own field types:

//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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM