public SolrDocument getDoc(Document doc) { SolrDocument solrDoc = new SolrDocument(); for (Fieldable f : (List<Fieldable>) doc.getFields()) { String fieldName = f.name(); if (returnFields != null && !returnFields.contains(fieldName)) continue; FieldType ft = schema.getFieldTypeNoEx(fieldName); Object val; if (ft == null) { // handle fields not in the schema if (f.isBinary()) val = f.binaryValue(); else val = f.stringValue(); } else { try { if (useFieldObjects && KNOWN_TYPES.contains(ft.getClass())) { val = ft.toObject(f); } else { val = ft.toExternal(f); } } catch (Exception e) { // There is a chance of the underlying field not really matching the // actual field type . So ,it can throw exception LOG.warn("Error reading a field from document : " + solrDoc, e); //if it happens log it and continue continue; } } solrDoc.addField(fieldName, val); } return solrDoc; }
} else { ftype.readableToIndexed(fieldValue, termval); pivot.add( VALUE, ftype.toObject(field, termval.get()) );
/** * Converts a lucene document to a solr document. * * @param schema the index schema * @param luceneDoc the lucene document * @return solr document */ public SolrDocument toSolrDoc(IndexSchema schema, Document luceneDoc) { SolrDocument solrDoc = new SolrDocument(); luceneDoc.forEach(it -> solrDoc.addField(it.name(), schema.getField(it.name()).getType().toObject(it))); for (String field : solrDoc.getFieldNames()) { Object value = solrDoc.getFieldValue(field); if (value instanceof ByteBuffer) { solrDoc.setField(field, ((ByteBuffer) value).array()); } } return solrDoc; }
/** * Add fields from the solr document * * TODO: /!\ NOTE /!\ This semantics of this function are still in flux. * Something somewhere needs to be able to fill up a SolrDocument from * a lucene document - this is one place that may happen. It may also be * moved to an independent function * * @since solr 1.3 */ public SolrDocument loadStoredFields( SolrDocument doc, Document luceneDoc ) { for( Object f : luceneDoc.getFields() ) { Fieldable field = (Fieldable)f; if( field.isStored() ) { SchemaField sf = schema.getField( field.name() ); if( !schema.isCopyFieldTarget( sf ) ) { doc.addField( field.name(), sf.getType().toObject( field ) ); } } } return doc; } }
if (null == fields || fields.contains(field.name())) { SchemaField sf = schema.getField( field.name() ); doc.addField( field.name(), sf.getType().toObject( field ) );
val = ft.toObject(field);