3. Document Mapping
Though there is already support for Entity Mapping within SolrJ, Spring Data Solr ships with its own mapping mechanism shown in the following section. NOTE: DocumentObjectBinder has superior performance. Therefore usage is recommended if there is not need for custom type mapping. You can switch to DocumentObjectBinder by registering SolrJConverter within SolrTemplate.
3.1. Mapping Solr Converter
MappingSolrConverter
allows you to register custom converters for your SolrDocument
and SolrInputDocument
as well as for other types nested within your beans. The Converter is not 100% compartible with DocumentObjectBinder
and @Indexed
has to be added with readonly=true
to ignore fields from beeing written to solr.
public class Product {
@Field
private String simpleProperty;
@Field("somePropertyName")
private String namedPropery;
@Field
private List<String> listOfValues;
@Indexed(readonly = true)
@Field("property_*")
private List<String> ignoredFromWriting;
@Field("mappedField_*")
private Map<String, List<String>> mappedFieldValues;
@Dynamic
@Field("dynamicMappedField_*")
private Map<String, String> dynamicMappedFieldValues;
@Field
private GeoLocation location;
}
Taking a look as the above MappingSolrConverter
will do as follows:
Property | Write Mapping |
---|---|
simpleProperty |
|
namedPropery |
|
listOfValues |
|
ignoredFromWriting |
|
mappedFieldValues |
|
dynamicMappedFieldValues |
|
location |
|
To register a custom converter one must add CustomConversions
to SolrTemplate
initializing it with own Converter
implementation.
<bean id="solrConverter" class="org.springframework.data.solr.core.convert.MappingSolrConverter">
<constructor-arg>
<bean class="org.springframework.data.solr.core.mapping.SimpleSolrMappingContext" />
</constructor-arg>
<property name="customConversions" ref="customConversions" />
</bean>
<bean id="customConversions" class="org.springframework.data.solr.core.convert.SolrCustomConversions">
<constructor-arg>
<list>
<bean class="com.acme.MyBeanToSolrInputDocumentConverter" />
</list>
</constructor-arg>
</bean>
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrClient" />
<property name="solrConverter" ref="solrConverter" />
</bean>