| /** |
| * |
| */ |
| package org.eclipse.smila.solr.update; |
| |
| import java.util.Map.Entry; |
| |
| import org.apache.solr.common.SolrInputDocument; |
| import org.eclipse.smila.datamodel.Any; |
| import org.eclipse.smila.datamodel.AnyMap; |
| import org.eclipse.smila.datamodel.AnySeq; |
| import org.eclipse.smila.datamodel.Record; |
| import org.eclipse.smila.datamodel.Value; |
| import org.eclipse.smila.datamodel.util.AnyUtil; |
| |
| import com.google.common.base.Strings; |
| |
| /** |
| * @author pwissel |
| * |
| */ |
| public class AdvancedSolrDocumentConverter { |
| |
| private final AnyMap _mapping; |
| |
| private final String _idField; |
| |
| public AdvancedSolrDocumentConverter() { |
| this(null); |
| } |
| |
| public AdvancedSolrDocumentConverter(final AnyMap mapping) { |
| this(mapping, Record.RECORD_ID); |
| } |
| |
| public AdvancedSolrDocumentConverter(final AnyMap mapping, final String idField) { |
| _mapping = mapping; |
| _idField = idField; |
| } |
| |
| public SolrInputDocument convert(final Record record) { |
| if (_mapping != null) { |
| return createDocumentWithMapping(record); |
| } |
| return null; |
| } |
| |
| private SolrInputDocument createDocumentWithMapping(final Record record) { |
| final String id = record.getMetadata().getStringValue(_idField); |
| final SolrInputDocument document = newDocument(id); |
| for (final Entry<String, Any> mapping : _mapping.entrySet()) { |
| final String attributeOrAttachmentName = mapping.getKey(); |
| final Any any = mapping.getValue(); |
| final Object fieldName = getFieldName(attributeOrAttachmentName, any); |
| addFieldFromAttribute(document, attributeOrAttachmentName, (String) fieldName, record); |
| } |
| return document; |
| } |
| |
| private SolrInputDocument newDocument(final String id) { |
| final SolrInputDocument document = new SolrInputDocument(); |
| document.addField(_idField, id); |
| return document; |
| } |
| |
| private Object getFieldName(final String attributeOrAttachmentName, final Any any) { |
| if (any.isValue()) { |
| // AnyValue |
| final Value value = any.asValue(); |
| if (value != null) { |
| String fieldName = Strings.emptyToNull(value.asString()); |
| return fieldName != null ? fieldName : attributeOrAttachmentName; |
| } |
| } else if (any.isSeq()) { |
| // AnySeq |
| final AnySeq seq = any.asSeq(); |
| if (seq != null && !seq.isEmpty()) { |
| return seq.asStrings().toArray(new String[seq.size()]); |
| } |
| } else if (any.isMap()) { |
| final AnyMap map = any.asMap(); |
| if (map != null && !map.isEmpty()) { |
| String fieldName = Strings.emptyToNull(map.getStringValue("fieldName")); |
| return fieldName != null ? fieldName : attributeOrAttachmentName; |
| } |
| } |
| return null; |
| } |
| |
| private void addFieldFromAttribute(final SolrInputDocument document, final String attributeName, |
| final String fieldName, final Record record) { |
| final Any any = record.getMetadata().get(attributeName); |
| if (any != null) { |
| final Object value = AnyUtil.anyToNative(any); |
| Float boost = null; |
| if (any.isMap()) { |
| final Any boostValue = any.asMap().remove("fieldBoost"); |
| if (boostValue != null) { |
| boost = boostValue.asValue().asDouble().floatValue(); |
| } |
| } |
| if (boost != null) { |
| document.addField(fieldName, value, boost); |
| } else { |
| document.addField(fieldName, value); |
| } |
| } |
| } |
| |
| } |