Apache Solr – How to start Spring Data Solr with SpringBoot

Solr powers the search with highly reliable, centralized configuration, scalablility and more for heavily-trafficked websites and applications. So in the tutorial, JavaSampleApproach will show how to create a Spring Data Solr application with SpringBoot.

Related posts:
How to start SpringBoot ElasticSearch using Spring Data

I. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.6RELEASE
– Solr 6.6.0

II. SpringBoot & Solr

spring data solr - springboot - architecture

SpringBoot provides spring-boot-starter-data-solr Starter to support connecting and abstractions on top of Spring Data Solr.

1. Solr connection

In application.properties file, We use spring.data.solr.* to configure Solr connection.
Here is the details of sourcecode – SolrProperties:

By default the instance will attempt to connect to a server using http://localhost:8983/solr. And we can add @Bean with SolrClient type to override it.

2. Spring Data Solr repositories

We use @SolrDocument to create Solr document:

Using SolrCrudRepository to create Solr repositories:

Details about interface SolrCrudRepository:

III. Practice

In the tutorial, we create a Spring Data Solr project with SpringBoot:

spring data solr - springboot - project structure

Step to do:
– Create SpringBoot project
– Create Solr document model
– Create Solr repository
– Run and check results

1. Create SpringBoot project

Using Spring Tool Suite, create a SpringBoot project. Then open pom.xml file, add spring-data-solr dependency:

2. Create Solr document model

Using @SolrDocument annotation, create a Solr document model:

3. Create Solr repository

Using interface SolrCrudRepository to create a CustomerRepository:

findByNameEndsWith is used to fetch all Customer documents that have name ending with specified argument.

Open application.properties file, configure solr.host:

4. Run and check results
4.1 Start Solr engine

Start Solr by commandline: solr-6.6.0\bin>solr start

spring data solr - springboot - start solr

spring data solr - springboot - admin at start time

Create a Solr core customer by commandline: solr create -c customer

spring data solr - springboot - create core

spring data solr - springboot - admin has customer core

4.2 Run SpringData Solr application

Build and Run the SpringBoot project with commandlines: {mvn clean install, mvn spring-boot:run}.

Logs:

spring data solr - springboot - admin excute select query

More practice:
– Delete Solr core customer by commandline: solr delete -c customer

spring data solr - springboot - delete core

– Stop Apache Solr by commandline: solr stop -p 8983

spring data solr - springboot - stop solr

IV. Sourcecode

SpringBootApacheSolr

By JavaSampleApproach | August 16, 2017.


Related Posts


2 thoughts on “Apache Solr – How to start Spring Data Solr with SpringBoot”

  1. Hello,

    Thanks for your tutorial. I am getting below error while running Spring boot application.

    ; nested exception is org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://localhost:8983/solr/customer: Expected mime type application/octet-stream but got text/html.

    Error 404 Not Found

    HTTP ERROR 404
    Problem accessing /solr/customer/customer/update. Reason:

    at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:224) ~[spring-data-solr-2.1.8.RELEASE.jar:na]
    at org.springframework.data.solr.core.SolrTemplate.saveBeans(SolrTemplate.java:376) ~[spring-data-solr-2.1.8.RELEASE.jar:na]
    at org.springframework.data.solr.core.SolrTemplate.saveBeans(SolrTemplate.java:365) ~[spring-data-solr-2.1.8.RELEASE.jar:na]
    at org.springframework.data.solr.core.SolrTemplate.saveBeans(SolrTemplate.java:347) ~[spring-data-solr-2.1.8.RELEASE.jar:na]
    at org.springframework.data.solr.repository.support.SimpleSolrRepository.save(SimpleSolrRepository.java:163) ~[spring-data-solr-2.1.8.RELEASE.jar:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_151]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_151]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_151]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:504) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:489) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at com.sun.proxy.$Proxy42.save(Unknown Source) ~[na:na]
    at com.example.solr.SolrConfig.run(SolrConfig.java:44) ~[classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    … 6 common frames omitted
    Caused by: org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://localhost:8983/solr/customer: Expected mime type application/octet-stream but got text/html.

    Error 404 Not Found

    HTTP ERROR 404
    Problem accessing /solr/customer/customer/update. Reason:

    1. Hello Kumar,

      We had double check, everthing works wells!
      Please do the right steps in tutorial:

      – Start Solr by commandline: solr-6.6.0\bin>solr start
      – Then create a Solr core customer by commandline: solr create -c customer

      Then run SpringData Solr application

      I see your problems is:

      -> mean your forgot creating Solr core customer.

      Regards,
      JSA

Got Something To Say:

Your email address will not be published. Required fields are marked *

*