Spring Data Solr Example
In this example, we will demonstrate how to integrate Spring data with Apache Solr.
Solr
is a search engine built on top of Apache Lucene library. It can be communicated with a REST like HTTP API because of which it can be easily be consumed as a REST-ful web-service irrespective of the underlying programming language used in the application which is calling the Solr Server. However, for the sake of this example we will be using Java as the programming language and Spring Data as the framework.
The Spring Data Solr is the module of Spring Data that provides support for Solr. As with the other examples in this series, this module supports both for derived queries (based on the method name) and the annotated query.
1. Implementation
Download the Apache Solr from here. The version at the time of publishing this blog was 5.2.1
. Unzip the downloaded file, change directory to location where Solr bin
is unzipped and run the following commands :
solr start -p 8983 solr create -c jcg
The first command start the solr server, while the second command creates a core, an index. Verify if the server is up by hitting the URL : http://localhost:8983/solr. In version 5.2
of Solr, the solrconfig.xml
uses the ManagedIndexSchemaFactory
as the schemaFactory
. However, we will be using the ClassicIndexSchemaFactory
for this example. Make the following changes to do so:
- Go to $(Solr_Home)/server/solr/$(core_name)/conf and rename the managed-schema to
schema.xml
. - Comment out the existing
schameFactory
tag and add the following line :<schemaFactory class="ClassicIndexSchemaFactory"/>
- Reload the core at the URL mentioned above in the
Cores
tab
Now that the core is setup, we need to add our fields to the schema.xml
.
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="title" type="string" indexed="true" stored="true" required="false" multiValued="false" /> <field name="description" type="string" indexed="true" stored="true" required="false" multiValued="false" />
That’s all on the Solr side. The core is now up and ready to use. Let’s start coding on the Application side.
We need to have following JAR Files to connect to Solr Server:
- aopalliance-1.0
- commons-io-1.3.2
- commons-lang3-3.4
- commons-logging-1.1.3
- httpclient-4.3.6
- httpcore-4.4.1
- httpmime-4.3.6
- noggit-0.7
- slf4j-api-1.7.5
- solr-solrj-4.10.3
- spring-aop-4.1.4.RELEASE
- spring-beans-4.1.4.RELEASE
- spring-core-4.1.4.RELEASE
- spring-context-4.1.4.RELEASE
- spring-data-commons-1.10.2.RELEASE
- spring-data-solr-1.4.2.RELEASE
- spring-expression-4.2.0.RELEASE
- spring-tx-3.1.1.RELEASE
Create a project in eclipse or any IDE and add the JAR files downloaded above. Now that the project is setup, we start with the coding phase :
First, we create an Entity that is to be persisted in the Solr
for searching later.
Book.java
package com.jcg.examples.entity; import java.io.Serializable; import org.apache.solr.client.solrj.beans.Field; import org.springframework.data.annotation.Id; public class Book implements Serializable { private static final long serialVersionUID = -8243145429438016231L; @Id @Field private String id; @Field private String title; @Field private String description; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((description == null) ? 0 : description.hashCode()); result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + ((title == null) ? 0 : title.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Book other = (Book) obj; if (description == null) { if (other.description != null) return false; } else if (!description.equals(other.description)) return false; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; if (title == null) { if (other.title != null) return false; } else if (!title.equals(other.title)) return false; return true; } @Override public String toString() { return "Book [id=" + id + ", title=" + title + ", description=" + description + "]"; } }
The id
field is the unique/Primary field defined in the schema.xml
and the same is annotated with @Id
. The @Field
is used to mark the other fields in the schema. In case the name of the field is different in the schema.xml
, we pass the name of the field in the value attribute of the @Field
annotation.
Next, we configure the repository which will help us in persisting the Book
Entity to the Solr Server:
BookRepo.java
package com.jcg.examples.repo; import org.springframework.data.repository.CrudRepository; import org.springframework.data.solr.repository.Query; import com.jcg.examples.entity.Book; public interface BookRepo extends CrudRepository<Book, Long> { @Query("title:?0") public Book findByBookTitle(String name); }
The Spring Data provides a number of inbuilt method for manipulating the Data. We need not write the queries for basic data manipulation and reading. It is achieved by extending the CrudRepository and declaring the proper Generics as per the Entity, which in our case is the <Book, Long>.
In case the Developer is not satisfied with the existing method, he can create his own method by specifying the Query using the @Query
annotation. In the BookRepo
class, the findByBookTitle
method searches for the argument passed in the title
field of the jcg
Solr Core.
The Spring IoC Container creates an instance of this Repository and makes it available to be used as a Factory Bean.
The last and the most important part is to configure the Spring Container using the spring-config.xml:
spring-config.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:solr="http://www.springframework.org/schema/data/solr" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/data/solr http://www.springframework.org/schema/data/solr/spring-solr.xsd"> <solr:repositories base-package="com.jcg.examples.repo"/> <!-- Define HTTP Solr server --> <solr:solr-server id="solrServer" url="http://localhost:8983/solr/jcg"/> <!-- Define Solr template --> <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate"> <constructor-arg index="0" ref="solrServer"/> </bean> </beans>
- Line 10: Scan the packages for initializing Cassandra Repositories.
- Line 13: Provide the port ,host and core for instance of Solr Server we wish to connect with the Application.
- Line 16: reate an instance of SolrTemplate which will communicate with the Solr Server to execute the queries.
Now that all is set, let’s run the application and test out the code! Here’s Application class that loads the XML file to instantiate the Spring Container and execute a few queries.
Application.java
package com.jcg.examples.test; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import com.jcg.examples.entity.Book; import com.jcg.examples.repo.BookRepo; public class Application { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new ClassPathResource("resources/spring-config.xml").getPath()); BookRepo bookRepo = context.getBean(BookRepo.class); Book rrs = new Book(); rrs.setId("1"); rrs.setTitle("Red Storm Rising"); rrs.setDescription("World War III"); bookRepo.save(rrs); Book hobbit = new Book(); hobbit.setId("3"); hobbit.setTitle("Hobbit"); hobbit.setDescription("Prelude to LOTR"); bookRepo.save(hobbit); System.out.println(bookRepo.findOne(1l)); System.out.println(bookRepo.findByBookTitle("Hobbit")); context.close(); } }
In the Application
class we created two instances of Book class and persisted them to the Solr Server. We then fetch the record from the core by the unique key. Next we fetch the data by executing the explicit query in the BookRepo
class.
Here’s the sample output of the program:
Aug 17, 2015 12:56:56 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@28fa1b85: startup date [Mon Aug 17 00:56:56 IST 2015]; root of context hierarchy Aug 17, 2015 12:56:56 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [resources/spring-config.xml] SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Book [id=1, title=Red Storm Rising, description=World War III] Book [id=3, title=Hobbit, description=Prelude to LOTR] Aug 17, 2015 12:56:57 AM org.springframework.context.support.ClassPathXmlApplicationContext doClose INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@28fa1b85: startup date [Mon Aug 17 00:56:56 IST 2015]; root of context hierarchy
2. Download the Source Code
Here we demonstrated how to configure and manage a Apache Solr Search Engine using Spring Data.
You can download the source code of this example here: SpringDataSolrExample.zip
Hi Chandan, Thnx for the post! When I try to run the main method I run into an error as
java.lang.IllegalArgumentException: [Assertion failed] – this argument is required; it must not be null
org.springframework.util.Assert.notNull(Assert.java:112)
org.springframework.util.Assert.notNull(Assert.java:123)
org.springframework.data.solr.core.convert.MappingSolrConverter$SolrPropertyValueProvider.readValue(MappingSolrConverter.java:321)
org.springframework.data.solr.core.convert.MappingSolrConverter$SolrPropertyValueProvider.readCollection(MappingSolrConverter.java:439)
org.springframework.data.solr.core.convert.MappingSolrConverter$SolrPropertyValueProvider.readValue(MappingSolrConverter.java:335)
any idea why?