Spring Data Solr Tutorial: Configuration

Spring Data Solr Tutorial: Configuration post image

In the previous part of my Spring Data Solr tutorial, we learned that Solr provides a REST-like HTTP API which can be used to add information to Solr index and execute queries against indexed data. The problem is that running a separate Solr instance in a development environment is a bit cumbersome.

However, not all hope is lost because Solr provides two alternative server implementations which we can use in our applications. These implementations are described in the following:

  • The embedded Solr server connects directly to Solr core. We can use this server for development purposes but we must also remember that using it in production environment is not recommended. However, using the embedded Solr server is still a viable option in the development environment.
  • The HTTP Solr server connects to an external Solr server by using HTTP. This is the recommended way of using the Solr search server and that is why we should always use it in the production environment.

This blog entry describes how we can get the required dependencies with Maven. We also learn to configure the Spring Data Solr to use the embedded Solr server in the development environment and the HTTP Solr server in the production environment.

Note: These blog entries provides additional information which helps us to understand the concepts described in this blog entry:

Let’s get started.

Getting the Required Dependencies with Maven

We can get the required dependencies with Maven by following these steps:

  1. Add the Spring Milestone Maven repository to the POM file.
  2. Add the required dependencies to the pom.xml file.

Both of these steps are described with more details in the following.

Adding the Spring Milestone Maven Repository to the POM File

We can add the Spring milestone Maven repository to our POM file by adding the following XML to the pom.xml file:

<repositories>
    <repository>
        <id>spring-milestone</id>
        <name>Spring Milestone Maven Repository</name>
        <url>http://repo.springsource.org/libs-milestone</url>
    </repository>
</repositories>

Adding the Required Dependencies to the POM File

We can add the required dependencies to the POM file by following these steps:

  1. Add the Spring Data Solr dependency (version 1.0.0.RC1) to the dependencies section of our POM file.
  2. Add the Solr core dependency (version 4.1.0) to the dependencies section of our POM file and exclude the SLF4J JDK14 binding. Because Solr core is required by the embedded Solr server, we can skip this step if we are not using the embedded Solr server.

We can complete these steps by adding the following XML to the dependencies section of the POM file:

<!-- Spring Data Solr -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-solr</artifactId>
    <version>1.0.0.RC1</version>
</dependency>

<!-- Required by embedded solr server -->
<dependency>
    <groupId>org.apache.solr</groupId>
    <artifactId>solr-core</artifactId>
    <version>4.1.0</version>
    <exclusions>
        <exclusion>
            <artifactId>slf4j-jdk14</artifactId>
            <groupId>org.slf4j</groupId>
        </exclusion>
    </exclusions>
</dependency>

Configuring Spring Data Solr

This section describes how we can configure Spring Data Solr to use different Solr servers in the development and production environment. We will use the embedded Solr server in the development environment and the HTTP Solr server in the production environment.

We can configure Spring Data Solr by following these steps:

  1. Create a properties file.
  2. Configure the embedded Solr server.
  3. Configure the HTTP Solr server.
  4. Set the active bean definition profile.

These steps are described with more details in the following subsections.

Creating the Properties File

The name of our properties file is application.properties and we will use it to configure two properties which are described in the following:

  • The solr.server.url property specifies the url of the used Solr server. The value of this property is used to configure the HTTP Solr server which is used in the production environment.
  • The solr.solr.home configures the home directory of Solr. The value of this property is used to configure the home directory of the embedded Solr server which is used in the development environment.

The content of the application.properties file looks as follows:

solr.server.url=http://localhost:8983/solr/
solr.solr.home=

Configuring the Embedded Solr Server

This subsection describes how we can configure Spring Data Solr to use the embedded Solr server in the development environment.

Java Configuration

We can create a configuration class which configures the embedded Solr server by following these steps:

  1. Create a class called EmbeddedSolrContext and annotate that class with the @Configuration annotation.
  2. Enable Spring Data Solr repositories by annotating that class with the @EnableSolrRepositories annotation and configuring the root package of our Solr repositories.
  3. Annotate the created class with the @Profile annotation and set its value to ‘dev’. This means that this configuration class is bypassed unless the ‘dev’ profile have been activated.
  4. Annotate the class with the @PropertySource annotation and set its value to ‘classpath:application.properties’. This configures the location of our property file and adds a PropertySource to Spring’s Environment.
  5. Add an Environment field to the class and annotate that field with the @Resource annotation. The injected Environment is used to access the properties which we added to our properties file.
  6. Create a method called solrServerFactoryBean() and annotate this method with the @Bean annotation. The implementation of this method creates a new EmbeddedSolrServerFactoryBean object, sets the value of the Solr home and returns the created object.
  7. Create a method called solrTemplate() and annotate this method with the @Bean annotation. The implementation of this method creates a new SolrTemplate object and passes the used SolrServer implementation as a constructor argument.

The source code of the EmbeddedSolrContext class looks as follows:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.repository.config.EnableSolrRepositories;
import org.springframework.data.solr.server.support.EmbeddedSolrServerFactoryBean;

import javax.annotation.Resource;

@Configuration
@EnableSolrRepositories("net.petrikainulainen.spring.datasolr.todo.repository.solr")
@Profile("dev")
@PropertySource("classpath:application.properties")
public class EmbeddedSolrContext {

    @Resource
    private Environment environment;

    @Bean
    public EmbeddedSolrServerFactoryBean solrServerFactoryBean() {
        EmbeddedSolrServerFactoryBean factory = new EmbeddedSolrServerFactoryBean();

        factory.setSolrHome(environment.getRequiredProperty("solr.solr.home"));

        return factory;
    }

    @Bean
    public SolrTemplate solrTemplate() throws Exception {
        return new SolrTemplate(solrServerFactoryBean().getObject());
    }
}

XML Configuration

We can create an XML configuration file for the embedded Solr server by following these steps:

  1. Configure the used properties file by using the property-placeholder element of the context namespace.
  2. Enable Solr repositories and configure the base package of our Solr repositories by using the repositories element of the solr namespace.
  3. Create a bean configuration for the development profile.
  4. Configure the embedded Solr server bean by using the embedded-solr-server element of the solr namespace. Set the value of the Solr home.
  5. Configure the Solr template bean. Set the configured embedded Solr server bean as constructor argument.

The contents of the exampleApplicationContext-solr.xml file looks as follows:

<?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">


    <context:property-placeholder location="classpath:application.properties"/>

    <!-- Enable Solr repositories and configure repository base package -->
    <solr:repositories base-package="net.petrikainulainen.spring.datasolr.todo.repository.solr"/>

    <!-- Bean definitions for the dev profile -->
    <beans profile="dev">
        <!-- Configures embedded Solr server -->
        <solr:embedded-solr-server id="solrServer" solrHome="${solr.solr.home}"/>

        <!-- Configures Solr template -->
        <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
            <constructor-arg index="0" ref="solrServer"/>
        </bean>
    </beans>

    <!-- Bean definitions for the prod profile are omitted -->
</beans>

Configuring the Http Solr Server

This subsection describes how we can configure Spring Data Solr to use the HTTP Solr server in the production environment.

Java Configuration

We can create a configuration class which configures the HTTP Solr server by following these steps:

  1. Create a class called HttpSolrContext and annotate that class with the @Configuration annotation.
  2. Enable Spring Data Solr repositories by annotating that class with the @EnableSolrRepositories annotation and configuring the root package of our Solr repositories.
  3. Annotate the created class with a @Profile annotation and set its value to ‘prod’. This means that this configuration class is bypassed unless the ‘prod’ profile have been activated.
  4. Annotate the class with the @PropertySource annotation and set its value to ‘classpath:application.properties’. This configures the location of our property file and adds a PropertySource to Spring’s Environment.
  5. Add an Environment field to the class and annotate that field with the @Resource annotation. The injected Environment is used to access the properties which we added to our properties file.
  6. Create a method called solrServerFactoryBean() and annotate this method with the @Bean annotation. The implementation of this method create a new HttpSolrServerFactoryBean object, sets the value of the Solr server url and returns the created object.
  7. Create a method called solrTemplate() and annotate this method with the @Bean annotation. The implementation of this method creates a new SolrTemplate object and passes the used SolrServer implementation as a constructor argument.

The source code of the HttpSolrContext class looks as follows:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.repository.config.EnableSolrRepositories;
import org.springframework.data.solr.server.support.HttpSolrServerFactoryBean;

import javax.annotation.Resource;

@Configuration
@EnableSolrRepositories("net.petrikainulainen.spring.datasolr.todo.repository.solr")
@Profile("prod")
@PropertySource("classpath:application.properties")
public class HttpSolrContext {

    @Resource
    private Environment environment;

    @Bean
    public HttpSolrServerFactoryBean solrServerFactoryBean() {
        HttpSolrServerFactoryBean factory = new HttpSolrServerFactoryBean();

        factory.setUrl(environment.getRequiredProperty("solr.server.url"));

        return factory;
    }

    @Bean
    public SolrTemplate solrTemplate() throws Exception {
        return new SolrTemplate(solrServerFactoryBean().getObject());
    }
}

XML Configuration

We can create an XML configuration file for the HTTP Solr server by following these steps:

  1. Configure the used properties file by using the property-placeholder element of the context namespace.
  2. Enable Solr repositories and configure the base package of our Solr repositories by using the repositories element of the solr namespace.
  3. Create a bean configuration for the production profile.
  4. Configure the HTTP Solr server bean by using the solr-server element of the solr namespace. Set the url of the Solr server.
  5. Configure the Solr template bean. Set the configured HTTP Solr server bean as a constructor argument.

The content of the exampleApplicationContext-solr.xml file looks as follows:

<?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">


    <context:property-placeholder location="classpath:application.properties"/>

    <!-- Enable Solr repositories and configure repository base package -->
    <solr:repositories base-package="net.petrikainulainen.spring.datasolr.todo.repository.solr"/>

    <!-- Bean definitions for the dev profile are omitted -->

    <!-- Bean definitions for the prod profile -->
    <beans profile="prod">
        <!-- Configures HTTP Solr server -->
        <solr:solr-server id="solrServer" url="${solr.server.url}"/>

        <!-- Configures Solr template -->
        <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
            <constructor-arg index="0" ref="solrServer"/>
        </bean>
    </beans>
</beans>

Setting the Active Bean Definition Profile

We can select the active bean definition profile by setting the value of the spring.profiles.active system variable. The allowed values of this system variable (in the context of our example application) are described in the following:

  • We can configure our application to run in the development profile by setting the value of the spring.profiles.active system variable to ‘dev’.
  • When we want configure our application to run in the production profile, we have to set the of the spring.profiles.active system variable to ‘prod’.

We can configure our example application to support both profiles by following these steps:

  1. Add required profiles to the POM file.
  2. Create the profile specific properties files for system properties.
  3. Configure the Jetty Maven plugin.

These steps are described with more details in the following.

Adding the Required Profiles to the POM File

We can add the required profiles to our Maven build by following these steps:

  1. Create a profile for development environment. Set the id of this profile to ‘dev’ and set the value of the build.profile.id property to ‘dev’.
  2. Create a profile for the production environment. Set the id of this profile to ‘prod’ and set the value of the build.profile.id property to ‘prod’.

The configuration of our Maven profiles looks as follows:

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <build.profile.id>dev</build.profile.id>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <build.profile.id>prod</build.profile.id>
        </properties>
    </profile>
</profiles>

Creating the Profile Specific Properties Files for System Properties

The profile specific properties files are located in the sub directories of the profiles directory. The name of each sub directory matches with the values of the build.profile.id properties configured in the pom.xml file.

We can create the profile specific properties files for system properties by following these steps:

  1. Create a properties file called system.properties to the profiles/dev directory. This properties file contains the system properties of the development profile.
  2. Create a properties file called system.properties to the profiles/prod directory. This properties file contains the system properties of the production profile.

The content of the properties file used to configure the system properties of the development profile looks as follows:

spring.profiles.active=dev

The content of the properties file used to configure the system properties of the production profile looks as follows:

spring.profiles.active=prod

Configuring the Jetty Maven Plugin

We can configure the Jetty Maven plugin by following these steps:

  1. Add the plugin declaration of the Jetty Maven plugin to the plugins section of our Pom file.
  2. Configure the stopKey and stopPort of the Jetty Maven plugin.
  3. Configure the location of the properties file containing the used system properties.

The configuration of the Jetty Maven plugin looks as follows:

<plugin>
     <groupId>org.mortbay.jetty</groupId>
     <artifactId>jetty-maven-plugin</artifactId>
     <version>8.1.5.v20120716</version>
     <configuration>
         <stopKey>todostop</stopKey>
         <stopPort>9999</stopPort>
         <systemPropertiesFile>${project.basedir}/profiles/${build.profile.id}/system.properties</systemPropertiesFile>
     </configuration>
 </plugin>

Summary

We have now successfully obtained the required dependencies with Maven and configured Spring Data Solr. This blog entry has taught us four things:

  • We learned to get the required dependencies with Maven.
  • We know that we should use the embedded Solr server only in the development environment and learned how we can configure Spring Data Solr to use it.
  • We learned that we should always use the HTTP Solr server in the production environment and know how we can configure Spring Data Solr to use it.
  • We know how we can use the bean definition profiles of Spring Framework for creating different configurations for development and production environment.

The next part of my Spring Data Solr tutorial describes how we can add new document to Solr index, update the information of an existing documents and delete documents from the Solr index.

PS. The example application of this blog entry is available at Github.

You might also like:

About the Author

Petri Kainulainen is passionate about software development and continuous improvement. He is specialized in software development with the Spring Framework and is the author of Spring Data book.

About Petri Kainulainen →

{ 39 comments… add one }

  • Kumar Chandran February 21, 2013 at 3:54 am

    HI,

    The spring data solr tutorial that you have posted is one of the best I have found on the internet. It is very detailed and comprehensive.

    In your spring data solr tutorial you have mentioned “The next part of my Spring Data Solr tutorial describes how we can add new document to Solr index, update the information of an existing documents and delete documents from the Solr index” but I was unable to find continuation to the last part of this tutorial. Please let me know if there is a link to the post that describes how to add new document to solr index…

    Would appreciate your response…

    Regards,

    Kumar

    Reply
    • Petri February 21, 2013 at 10:13 am

      Thank you for the compliment. It is always nice to get positive feedback.

      I have not written the next part of this tutorial yet but the example application already has this functionality.

      I am planning to write the next part during the weekend and publish it before Monday.

      Reply
  • Ssm April 1, 2013 at 8:20 am

    How to integrate it with Tomcat7?

    Reply
  • Tom April 11, 2013 at 5:43 am

    Hi, I can use this search English content,but not Chinese, I use Lucke and I can see all the content has been indexed properly. I want to know whether is my problem of solr configuration or my spring data solr search. My repository just like this:
    @Query(“name:*?0* or description:*?0* or type:*?0* or mac_address:*?0* or uri:*?0* or attrs:*?0*”)
    Page find(String keyword,Pageable page);
    And I can search my Chinese content just use “*”,but can’t get anything by using specific Chinese word.

    I will be very appreciative if you can give me a hint.Thanks.

    Reply
    • Petri April 11, 2013 at 9:41 am

      Hi,

      The query seems to be correct. If the Chinese text is indexed property as you said, the problem can be that

      • Your application messes up special characters and that is why the search does not work.
      • The servlet container of your application is not configured to support UTF-8.
      • The used Solr instance is not configured to support UTF-8.

      In order to determine the root cause of problem, it would be important that you would answer to the following questions:

      • How the used search term is send to the application (GET with request parameter or POST and request body)?
      • Which servlet container are you using?
      • What version of Solr are you using? If you are using a version smaller than 4.1, have you configured the URI encoding (details here)?

      As a side note, indexing Chinese seems to be a bit trickier than I originally thought. I found a blog post called Indexing Chinese in Solr which has a brief overview about this. However, that blog post is not very detailed and it would be interesting to find a more comprehensive tutorial about this. If you know any, let me know. :)

      Reply
  • amit singh May 10, 2013 at 3:54 pm

    I am getting folowwing error

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘repositoryTodoService’: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘repositoryTodoIndexService’: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [net.petrikainulainen.spring.datasolr.todo.repository.solr.TodoDocumentRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency

    While i am able to run the test cases. Please help me on this. I found this very useful but it is not working for me.

    Reply
  • amit singh May 10, 2013 at 4:50 pm

    I am getting the error at starting up the jetty. more that there is no web.xml so that i can test the same on tomcat. if i do mvn install it gives no error and runs the tes cases.

    Reply
    • Petri May 10, 2013 at 5:06 pm

      The root cause is the same (The correct Spring profile is not set).

      If you start Jetty by using a command: mvn clean jetty:run, the active profile is set by Maven (I just tested this by running the example application myself).

      If you are using the standalone Jetty, you have to configure the correct profile manually. In other words, you have to add one of the following lines to the Jetty configuration:

      #Add this if you want to use embedded Solr server
      -Dspring.profiles.active=dev
      #add this if you want to use HTTP Solr server
      -Dspring.profiles.active=prod

      Check out the chapter 16 of the Jetty user’s manual for more details about this.

      You can also deploy the created war file to Tomcat 7 because it supports the Servlet 3.0 specification. This comment explains how you can configure the active profile when you are using Tomcat 7.

      I hope that this answered to your question.

      Reply
  • amit singh May 10, 2013 at 4:53 pm

    I am not able to run the jetty server even on dev environment.

    Reply
  • amit singh May 10, 2013 at 6:09 pm

    I have solr running on tomcat at 9080. I am running this code through maven jetty. I pointed it to dev environment.

    Reply
    • Petri May 10, 2013 at 6:24 pm

      If you want to use the HTTP Solr server, you cannot use the ‘dev’ profile because it is configured to use the embedded Solr server. In this case, you have to use the ‘prod’ profile.

      Also, since Solr is not running in the default port, you have to set the value of the solr.server.url property. You can find this property from the profiles/prod/config.properties file. After this is done, you can run the example application by running the command mvn clean jetty:run -P prod at the command prompt (assuming that you are using the schema described in this blog post).

      If you want to use a standalone server, you have to set the active Spring profile manually. Check out my previous comment for more details about this.

      Reply
  • amit singh May 10, 2013 at 8:01 pm

    Thanx a bunch !!!! I got it.. do you have any default user id and password to access the application…

    Reply
  • amit singh May 10, 2013 at 9:30 pm

    More i am reading and it is getting more interesting to explore…. I got it working in tomcat but now getting error on tomcat….

    127.0.0.1 – - [10/May/2013:23:52:48 +0530] “GET /spring%2Ddata%2Dsolr%2Dcriteria/ HTTP/1.1″ 200 10580
    127.0.0.1 – - [10/May/2013:23:52:48 +0530] “GET /static/css/example.css HTTP/1.1″ 404 995
    127.0.0.1 – - [10/May/2013:23:52:48 +0530] “GET /static/js/vendor/bootstrap-transition.js HTTP/1.1″ 404 1031

    all js files are 404 response. while if run the application on jetty it is working

    Reply
    • Petri May 10, 2013 at 10:53 pm

      This is a limitation of the example application. The problem is that the static resources are loaded by using absolute paths. That is why they are found only if the web application is found from context path ‘/’. You have two options for solving this problem:

      1. Rename the war file to ROOT.war. This ensures that the application is found from context path ‘/’ when Tomcat is used. Obviously, this is not a very elegant solution.
      2. A better option is to to add the context path as a prefix to the path which is used to download the static resources. This StackOverflow question describes how you can do this.

      I hope that this solved your problem.

      Reply
      • amit singh May 10, 2013 at 11:09 pm

        You rock Petri !!!! i got every thing up and running in my application.

        Reply
        • Petri May 10, 2013 at 11:10 pm

          Great!

          I decided to update the example application by using option number two. Now the context path is taken into account when static resources are loaded. Thanks for pointing this problem out!

          Reply
  • amit singh May 13, 2013 at 2:20 pm

    I am using it in application with xmlwebapplication content. Where can I mention to load the HttpSolrContext.java. I am not able to load this java file and hence not any repository.

    Reply
  • amit singh May 13, 2013 at 3:29 pm

    Thanx Petri !! I successfully integrated it with my project.

    Reply
  • amit singh May 13, 2013 at 3:57 pm

    Hi Petri, one question, is it possible to index images… i have requirement to index the profile of users and profile comprises the image also…. any suggestion on this would be great help. Thanx in adnvance.

    Reply
  • PeriS May 16, 2013 at 4:32 am

    Petri,

    The repository link does not have the spring-data-solr jar so its not getting pulled down. I checked their site, and its missing. So any ideas?

    Reply
    • Petri May 16, 2013 at 8:39 am

      Which repository url are you using?

      I noticed yesterday that the url http://repo.springframework.org/milestone does not work. Then I checked the homepage of the Spring Data Solr project and noticed that the repository url has changed to http://repo.springsource.org/libs-milestone. After I updated the repository url to the POM files of my Spring Data Solr examples, I could download all the dependencies from Maven repositories.

      Reply
      • Peri May 16, 2013 at 8:48 pm

        Thanks, will give it a shot. Would you recommend this approach (spring based model), as I m not sure if Spring is going to be actively working on evolving this solr support.

        Reply
        • Petri May 16, 2013 at 11:08 pm

          I am using Spring Data Solr in a production system without any problems. However, it is true that since the Spring Data Solr is not an official Spring project, it might not have as much resources than the official projects. I don’t think that this is necessarily a problem but of course you have to make your own mind about this. I can only say that if I had decided to use SolrJ, the codebase of that project would have a lot of boilerplate code. Spring Data Solr makes the code cleaner, shorter and faster to write.

          Reply

Leave a Comment