Tom Lokhorst
Tom Lokhorst

Reputation: 13768

Using Lucene API to show float field from Solr index

I've inherited some code which uses the Lucene API to query a Solr index.

The code does a lot of searches, and at the end converts all found lucene documents to solr documents:

// doc:Document

val sdoc = new SolrDocument

for (f:Fieldable <- doc.getFields if f.isStored) {
  sdoc.addField(f.name(),f.stringValue())
}

This works fine, except for when the field value isn't a string, e.g. floats or booleans. On float fields, stringValue() returns some weird characters (e.g. ¿£൱), presumably the string representation of a float.

How do I properly get the float value from a Lucene document?

Upvotes: 3

Views: 584

Answers (1)

mailboat
mailboat

Reputation: 1077

For Numeric stored as binary value, you need to get by doc.getBinaryValue(fieldName) you will get byte[] as return value which you will have to convert it into your appropriate numeric value. This is what you could do:

if(!field.isBinary()){
    sdoc.addField(fieldName, doc.get(fieldName));
} else{
    ByteBuffer buff = ByteBuffer.wrap(doc.getBinaryValue(fieldName));
    sdoc.addField(fieldName, buff.getFloat());
}

Here's a SO Quetion that provides help with the conversion.

Upvotes: 3

Related Questions