checklist
checklist

Reputation: 12940

Spring Data Solr with Dynamic Field not working

I am trying to index a document with dynamic fields and have it defined:

@SolrDocument(solrCoreName = "collection1")
public class SolrProduct {

    @Field
    String id;

    @Field
    String name;

    @Field("mappedField_*")
    Map<String, List<String>> mappedFieldValues;
}

And the following repo:

public interface SolrProductRepository extends SolrCrudRepository<SolrProduct, String> { }

My solr schema is as follows:

<field name="name" type="text_ws" indexed="true" stored="true"/>
<dynamicField name="mappedField_*" type="text_general" indexed="true" stored="true"/>

I am trying to save a new document where the value of the mappedFieldValues Map is:

{thermometer=[yes], camera=[yes], vibration=[Motion], brand=[Philips]}

Yet, I get the following exception:

org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException:

ERROR: [doc=5530cbd78b15a5f18dfe3d28] unknown field 'thermometer' at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:495) at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:199) at org.apache.solr.client.solrj.request.AbstractUpdateRequest.process(AbstractUpdateRequest.java:118) at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:116) at org.springframework.data.solr.core.SolrTemplate$4.doInSolr(SolrTemplate.java:178) at org.springframework.data.solr.core.SolrTemplate$4.doInSolr(SolrTemplate.java:175) at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:132) at org.springframework.data.solr.core.SolrTemplate.saveBean(SolrTemplate.java:175) at org.springframework.data.solr.core.SolrTemplate.saveBean(SolrTemplate.java:169) at org.springframework.data.solr.repository.support.SimpleSolrRepository.save(SimpleSolrRepository.java:149) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:416) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:401) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:373) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at $Proxy60.save(Unknown Source) at com.example.Indexer.indexAll(Indexer.java:29)

So it seems to me that the spring-data-solr does not properly call solr with the dynamic fields name (instead of mappedField_thermometer it simply calls thermometer).

Any ideas?

Upvotes: 3

Views: 1856

Answers (1)

Saeed Shahsavan
Saeed Shahsavan

Reputation: 45

Spring data solr supports dynamic field mapping for example in my project i have configured any field names to Object type

@org.springframework.data.solr.core.mapping.Dynamic
@org.apache.solr.client.solrj.beans.Field("*")
private ListOrderedMap<String, Object> fieldValueMap;

as you can see any field values maps to this map. but in your project in solr configuration you have said just map fields that starting with "mappedField_*" so if you change your field names to mappedField_yourField it will work correctly

Upvotes: 1

Related Questions