Example usage for org.apache.solr.schema FieldType toObject

List of usage examples for org.apache.solr.schema FieldType toObject

Introduction

In this page you can find the example usage for org.apache.solr.schema FieldType toObject.

Prototype

public Object toObject(IndexableField f) 

Source Link

Document

Convert the stored-field format to an external object.

Usage

From source file:net.yacy.cora.federate.solr.connector.EmbeddedSolrConnector.java

License:Open Source License

public SolrDocument doc2SolrDoc(Document doc) {
    SolrDocument solrDoc = new SolrDocument();
    for (IndexableField field : doc) {
        String fieldName = field.name();
        SchemaField sf = getSchemaField(fieldName); // hack-patch of this.core.getLatestSchema().getFieldOrNull(fieldName); makes it a lot faster!!
        Object val = null;
        try {//  w w w  .ja v  a  2  s.  com
            FieldType ft = null;
            if (sf != null)
                ft = sf.getType();
            if (ft == null) {
                BytesRef bytesRef = field.binaryValue();
                if (bytesRef != null) {
                    if (bytesRef.offset == 0 && bytesRef.length == bytesRef.bytes.length) {
                        val = bytesRef.bytes;
                    } else {
                        final byte[] bytes = new byte[bytesRef.length];
                        System.arraycopy(bytesRef.bytes, bytesRef.offset, bytes, 0, bytesRef.length);
                        val = bytes;
                    }
                } else {
                    val = field.stringValue();
                }
            } else {
                val = ft.toObject(field);
            }
        } catch (Throwable e) {
            continue;
        }

        if (sf != null && sf.multiValued() && !solrDoc.containsKey(fieldName)) {
            ArrayList<Object> l = new ArrayList<Object>();
            l.add(val);
            solrDoc.addField(fieldName, l);
        } else {
            solrDoc.addField(fieldName, val);
        }
    }
    return solrDoc;
}