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)
Assumption
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
?name=Boot_Solr_Basic
&groupId=com.kode12
&artifactId=bootsolrbasic
&version=0.0.1-SNAPSHOT
&description=Demo+project+for+Spring+Boot+and+Solr
&packageName=com.kode12
&type=maven-project
&packaging=jar
&javaVersion=1.8
&language=java
&bootVersion=1.4.2.RELEASE
&dependencies=data-solr
Code and Configuration
Basic project structure with necessary files looks like
Let’s look content of all required files one by one.
pom.xml
Path: classpath
Content:
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.kode12</groupId> <artifactId>bootsolrbasic</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Boot_Solr_Basic</name> <description>Demo project for Spring Boot and Solr</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-solr</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Detail:
Artifact spring-boot-starter-data-solr
will get all required dependency for Solr.
BootSolrBasicApplication.java
Path: com.kode12
Content:
package com.kode12; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class BootSolrBasicApplication { public static void main(String[] args) { SpringApplication.run(BootSolrBasicApplication.class, args); } }
Detail:
Automatically created and we are not using in our demo.
application.properties
Location: src/main/resources
Content:
spring.data.solr.host=http://127.0.0.1:8983/solr/test
Detail:
spring.data.solr.host
is a built-in property provided by boot only
http://127.0.0.1:8983/solr/test
provides host, port, and core name which is test
here.
Employee.java
Location: com.kode12.vo
Content:
package com.kode12.vo; import org.apache.solr.client.solrj.beans.Field; public class Employee { public Employee() { } public Employee(long id, String name) { super(); this.id = id; this.name = name; } private long id; @Field 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; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + "]"; } }
Detail:
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.
SolrEmployeeRepository.java
Location: com.kode12.repository
Content:
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); }
Detail:
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.
BootSolrBasicApplicationTests.java
Location: src/main/reaources
Content:
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; @RunWith(SpringRunner.class) @SpringBootTest public class BootSolrBasicApplicationTests { @Autowired SolrEmployeeRepository solrEmployeeRepository; @Test 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"); solrEmployeeRepository.save(employee1); solrEmployeeRepository.save(employee2); solrEmployeeRepository.save(employee3); solrEmployeeRepository.save(employee4); solrEmployeeRepository.save(employee5); 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
The post Apache Solr: Setup and Searching with Spring Boot appeared first on kode12.