Example usage for org.springframework.data.solr.core.mapping SolrPersistentProperty getFieldName

List of usage examples for org.springframework.data.solr.core.mapping SolrPersistentProperty getFieldName

Introduction

In this page you can find the example usage for org.springframework.data.solr.core.mapping SolrPersistentProperty getFieldName.

Prototype

String getFieldName();

Source Link

Document

Get name of field under attention to org.apache.solr.client.solrj.beans.Field annotation

Usage

From source file:org.springframework.data.solr.core.convert.MappingSolrConverter.java

@SuppressWarnings("rawtypes")
protected void write(Object source, final Map target, SolrPersistentEntity<?> entity) {

    final PersistentPropertyAccessor accessor = new ConvertingPropertyAccessor(
            entity.getPropertyAccessor(source), getConversionService());

    entity.doWithProperties(new PropertyHandler<SolrPersistentProperty>() {

        @SuppressWarnings({ "unchecked" })
        @Override//from  w  w  w .  j  a  va2s. c om
        public void doWithPersistentProperty(SolrPersistentProperty persistentProperty) {

            Object value = accessor.getProperty(persistentProperty);
            if (value == null || persistentProperty.isReadonly()) {
                return;
            }

            if (persistentProperty.containsWildcard() && !persistentProperty.isMap()) {
                throw new IllegalArgumentException("Field '" + persistentProperty.getFieldName()
                        + "' must not contain wildcards. Consider excluding Field from beeing indexed.");
            }

            Object fieldValue = value;
            if (persistentProperty.isMap() && persistentProperty.containsWildcard()) {
                TypeInformation<?> mapTypeInformation = persistentProperty.getTypeInformation()
                        .getMapValueType();
                Class<?> rawMapType = mapTypeInformation.getType();

                Map<?, ?> map = (Map<?, ?>) fieldValue;
                for (Map.Entry<?, ?> entry : map.entrySet()) {
                    String mappedFieldName = entry.getKey().toString();
                    SolrInputField field = new SolrInputField(mappedFieldName);
                    if (entry.getValue() instanceof Iterable) {
                        for (Object o : (Iterable<?>) entry.getValue()) {
                            field.addValue(convertToSolrType(rawMapType, o), 1f);
                        }
                    } else {
                        if (rawMapType.isArray()) {
                            for (Object o : (Object[]) entry.getValue()) {
                                field.addValue(convertToSolrType(rawMapType, o), 1f);
                            }
                        } else {
                            field.addValue(convertToSolrType(rawMapType, entry.getValue()), 1f);
                        }
                    }
                    target.put(mappedFieldName, field);
                }
                return;
            }

            SolrInputField field = new SolrInputField(persistentProperty.getFieldName());
            if (persistentProperty.isCollectionLike()) {
                Collection<?> collection = asCollection(fieldValue);
                for (Object o : collection) {
                    if (o != null) {
                        field.addValue(convertToSolrType(persistentProperty.getType(), o), 1f);
                    }
                }
            } else {
                field.setValue(convertToSolrType(persistentProperty.getType(), fieldValue), 1f);
            }
            target.put(persistentProperty.getFieldName(), field);

            if (persistentProperty.isBoosted()) {
                field.setBoost(persistentProperty.getBoost());
            }

        }
    });

    if (entity.isBoosted() && target instanceof SolrInputDocument) {
        ((SolrInputDocument) target).setDocumentBoost(entity.getBoost());
    }

}