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}.


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


Related Posts

Got Something To Say:

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