Apache Solr: Setup and Searching with Spring Boot

In Previous tutorial we have gone through basic setup and query with Solr server.

In this post i’ll show you how to index your documents and perform search with Java, especially using Spring Boot here.

Prerequisites & Assumption

Required tools

  • STS (Good to have)
  • Maven/Gradle
  • Java (:) Definitely)


You should have Solr distribution and running, visit previous post if you don’t have.

Create Project

I am using STS here to create project, follow steps below to create project.

  • Create Spring starter Project (If you don’t know how to do it than visit my previous post).
  • Select Solr dependency from NoSQL group(shown in image below)
  • Click Finish.

You can download zip using url also: http://start.spring.io/starter.zip

Code and Configuration

Basic project structure with necessary files looks like

Let’s look content of all required files one by one.


Path: classpath

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


	<description>Demo project for Spring Boot and Solr</description>

		<relativePath/> <!-- lookup parent from repository -->






Artifact spring-boot-starter-data-solr will get all required dependency for Solr.


Path: com.kode12

package com.kode12;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class BootSolrBasicApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootSolrBasicApplication.class, args);

Automatically created and we are not using in our demo.


Location: src/main/resources


spring.data.solr.host is a built-in property provided by boot only provides host, port, and core name which is test here.


Location: com.kode12.vo

package com.kode12.vo;

import org.apache.solr.client.solrj.beans.Field;

public class Employee {

    public Employee() {


    public Employee(long id, String name) {
        this.id = id;
        this.name = name;

    private long id;
    private String name;

    public long getId() {
        return id;

    public void setId(long id) {
        this.id = id;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

    public String toString() {
        return "Employee [id=" + id + ", name=" + name + "]";


This is just a pojo class and used to map document and object.
@Field annotation is can be used with property or setter method, this denotes that attribute which is marked with @Field annotation is able to take part in Solr indexing.


Location: com.kode12.repository

package com.kode12.repository;

import org.springframework.data.solr.repository.SolrCrudRepository;

import com.kode12.vo.Employee;
import java.lang.String;
import java.util.List;

public interface SolrEmployeeRepository extends SolrCrudRepository<Employee, Long>{
    List<Employee> findByName(String name);


This is same as other Repository interface which we used to in hibernate. This interface extends SolrCrudRepository where Employee is a class and Long is a Serializable field name.

Test Cases

Now we have everything ready and it’s good to go.
I am using a JUnit Test case to test some methods.


Location: src/main/reaources

package com.kode12;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.kode12.repository.SolrEmployeeRepository;
import com.kode12.vo.Employee;

public class BootSolrBasicApplicationTests {
    SolrEmployeeRepository solrEmployeeRepository;

    public void contextLoads() {
        // Write your code snippet here.

Insert your code snippet to perform operation form below list.

Insert Test Data
Employee employee1 = new Employee(1, "Employee_1");
Employee employee2 = new Employee(2, "Employee_2");
Employee employee3 = new Employee(3, "Employee_3");
Employee employee4 = new Employee(4, "Employee_4");
Employee employee5 = new Employee(5, "Employee_5");
System.out.println("5 Employee records indexed.");
Get total no of documents in core
System.out.println("Total documents in core 'test' is " + solrEmployeeRepository.count());
Search By Name
List<Employee> employee = solrEmployeeRepository.findByName("Employee_3");
System.out.println("Documents: " + employee);
Search By Id
System.out.println(“Employee: ” + solrEmployeeRepository.findOne(1l));

As you all know we used SolrCrudRepository which provides many useful methods, you can invoke it without any custom implementation. Explore more about SolrCrudRepository here.

This is all about basic things which we can do using spring boot, will come up with some other useful Solr tutorial in upcoming tutorials.

Share current post by copy: https://goo.gl/HAXDN0
Happy Learning!

Thanks, Yogesh P