Do want to get a better understanding of Spring web application architecture? If so, get started right now!

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.

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.

If you want to learn more about Spring Data Solr, you should read my Spring Data Solr tutorial.

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 →

83 comments… add one

  • 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
    • 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
  • How to integrate it with Tomcat7?

    Reply
    • When the application is deployed to Tomcat, you can specify the active profile by adding the following line to the conf/catalina.properties file:

      spring.profiles.active=dev

      Remember that the value of the spring.profiles.active property depends from the profile which you want to use.

      Reply
  • 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
    • 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
  • 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
  • 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
    • 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
  • I am not able to run the jetty server even on dev environment.

    Reply
    • How are you trying to run it?

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

    Reply
    • 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
      • Thanx very much !! it worked as you said.. i needed to point to production.

        Reply
        • You are welcome! I am happy to hear that you could solve your problem.

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

    Reply
    • thanx got it working… thanx for your support and such a nice tutorial..

      Reply
      • I guess you figured out that the credentials are user / password :) Anyway, good to hear that I could help you out.

        Reply
  • 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
    • 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
      • You rock Petri !!!! i got every thing up and running in my application.

        Reply
        • 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
  • 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
    • The example application also has a working XML configuration for Spring application context. The embedded Solr server and the HTTP Solr server are configured in a separate XML file which is imported to the main configuration file by using the import namespace element.

      Read the section called Configuring Spring Data Solr for more details about this.

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

    Reply
    • You are welcome!

      Reply
  • 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
  • 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
    • 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
      • 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
        • 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
  • Hi,

    1)What is the value of solr.solr.home in :
    application.properties :
    solr.server.url=http://localhost:8983/solr/
    solr.solr.home=

    2)where the package in :

    @EnableSolrRepositories(“net.petrikainulainen.spring.datasolr.todo.repository.solr”)

    come from ?

    Or can it be anything ?

    thanks, lot.

    Reply
  • Hi Petri,

    I cannot get your sample code to run correctly.
    The error is

    org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [net.petrikainulainen.spring.datasolr.todo.repository.solr.TodoDocumentRepository] found for dependency

    I am not sure how to resolve this.
    Any pointers?

    Thank you.
    Adil

    Reply
    • Have you set the active Spring profile? The example application has two Spring profiles:

      • The dev profile which uses embedded Solr server.
      • The prod profile which uses HTTP Solr server.

      If either one of those profiles isn’t activated, you get that exception.

      If you aren’t running the example by using the Maven Jetty plugin (check README), you have to configure the active Spring profile in a such way that it is recognized by the used server.

      Which server are you using?

      Reply
      • Hi Petri,

        The dev profile is in use which is using Jetty. I am using IntelliJ IDE. I invoke the jetty:run goal from the IDE.

        The project in use is https://github.com/pkainulainen/spring-data-solr-examples/tree/master/criteria

        The only modifications i have done is

        1. Setup embedded solr per http://www.petrikainulainen.net/programming/maven/running-solr-with-maven/. I don’t think i reach a point where Solr is even started
        2. Changed solr version to 4.4.0 in pom.xml
        3. Change @Service to @Service(“repositoryTodoIndexService”) on RepositoryTodoIndexService as i was having similar problem of Spring not resolving the bean
        3. Change @Service to @Service(“repositoryTodoService”) on RepositoryTodoService to resolve the bean for spring

        I think is not auto resolving the repositorybean.

        Adil

        Reply
        • Hi Petri,

          I restarted from scratch and setup the project again. This time i used the command line to run the project instead of the intellij

          I think the correct command line is
          mvn jetty:run -Dspring.profiles.active=”dev”

          That did work for me and i was able to bootup and navigate to the http url.
          Also, Solr 4.4.0 is incompatible. probably changes to the embeddedsolrserver has something to do with this.

          The other problem i have is probably a issue in the project.
          When i navigate to the home page http://localhost:8983/solr/ ( default url set in the properties file), i am prompted to login. username/password combination is what is specified in your security xml file and when i use that, i see that the login post request is being sent to http://localhost:8983/api/login instead of http://localhost:8983/solr/api/login. Therefore a 404 is always received and 404 view rendered.

          i did try changing the security url patterns from /api/login and /api/logout to api/login and api/logout but spring security does not like it

          Any ideas on how to resolve that ?
          Adil

          Reply
          • It is also possible to specify the active Spring profile at command prompt like you did.

            However, my example application uses another method. The Maven Jetty plugin used in the example application reads the system properties from a properties file. You can do this by adding the path of the properties file as the value of the systemPropertiesFile configuration option like this (add this to the plugin configuration section):

            
            <systemPropertiesFile>
                ${project.basedir}/profiles/${build.profile.id}/system.properties
            </systemPropertiesFile>
            
            

            About your second problem, the frontend application assumes that the backend is found from the context root. You can fix this adding fixing the urls found from the Javascript files (Seach for the string “/api/” from the directory src/main/webapp/static/js/app).

          • Hi Petri,

            Thank you so much for the help. I was finally able to get it working.

            I had to:
            – add google guava as maven dependency.
            – remove usage of solr:repositories as that was giving me a solr core error. I instead directly created the solr server using the solr home and solr default core properties.
            – had to adjust the root context and make it root.
            – increase xmx and permgen size

            Apologies to have pinged you quite a bit.
            Adil

          • Hi Adil,

            It is good to hear that you were able to solve your problem! Don’t worry about pinging me. I like to help my fellow developers. That is one reason why I started writing this blog.

  • How does spring data for solr support or integrate with ‘qt’ or custom query handlers, especially for read operations.

    Reply
    • The “normal” queries made against Solr documents are supported pretty well by following the Spring Data repository abstraction.

      Unfortunately, if you want to something “exotic”, you might have to create a custom repository implementation. The good news is that you can get access to SolrJ API so you can use it when you need something which isn’t supported by Spring Data Solr.

      The custom query handlers aren’t probably supported out of the box (I haven’t checked this project for a while now so this might have changed).

      Reply
  • Is it possible to configure two different HTTP Solr servers? or, work with two different collections , this because I’m trying to create two different Repository Interfaces, I’m new in Spring-Solr.

    Reply
    • This StackOverflow question explains how you can do that. Unfortunately it is possible only if you create your repositories manually.

      Reply
  • Petri

    I tried using multicores, got following exception
    org.springframework.data.solr.UncategorizedSolrException: Server at localhost:8983/solr returned non ok status:404, message:Not Found; nested exception is org.apache.solr.common.SolrException: Server at localhost:8983/solr returned non ok status:404, message:Not Found
    at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:110)
    at org.springframework.data.solr.core.SolrTemplate.saveBean(SolrTemplate.java:144)
    at org.springframework.data.solr.repository.support.SimpleSolrRepository.save
    (SimpleSolrRepository.java:137)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Meth

    my solr.xml,

    not sure why it’s not able to get the core while saving, can you please guide me?

    Reply
  • Petri,

    Do I have to do something like that,
    http://stackoverflow.com/questions/16859181/spring-data-solr-multiple-cores-and-repository

    or you have some other solutions ..?

    Reply
    • Hi Rupanjan,

      for some reason I thought that you followed the answer given at this comment which caused your problem.

      Anyway, I haven’t been using multiple cores myself so I would say that following the answer of that StackOverflow question is your best option.

      Reply
  • I have got following error when i had download a project from github and clean and build one of you project spring data solr – query method.
    Netbean 7.4
    apache tomcat 7.0.34

    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. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, lookup=, authenticationType=CONTAINER)}
    IN exampleApplicationContext-solr.xml

    In my scenario, what is my solrHome address.

    Reply
    • Hi Binay,

      I already answered to this question here.

      Reply
  • Dear Petri,
    Thank you for response and i really greatful to you.
    I have also got the problem after i have done as you said.
    In my solrconfig.xml,
    4.5
    ===============================
    Apache solr version : 4.5.1
    Apache Tomcat : 7.0.41
    Though application is running but solr server not starting or running
    org.apache.solr.common.SolrException: Could not load config for solrconfig.xml

    Caused by: org.apache.solr.common.SolrException: Invalid luceneMatchVersion ‘4.5’, valid values are: [LUCENE_30, LUCENE_31, LUCENE_32, LUCENE_33, LUCENE_34, LUCENE_35, LUCENE_36, LUCENE_40, LUCENE_41, LUCENE_CURRENT] or a string in format ‘V.V’

    Update: I removed the unnecessary part of the stack trace – Petri

    Reply
    • The error message states that the luceneMatchVersion ‘4.5’ is not valid. This seems a bit odd because I downloaded the latest Solr distribution (4.5.1) and it uses ‘4.5’ as the value of the luceneMatchVersion configuration property. Did you download this distribution as well or are you running Solr with Maven?

      Reply
      • Dear Petri,
        I already downloaded the latest Solr distribution (4.5.1). And I follow this Apache Solr 4 Cookbook for Running Solr on Apache Tomcat.I also able to run by manually solr with tomcat. But this won’t happen with netbeans after it automatically run tomcat.
        I think this is a version error or not
        POM
        4.1.0
        Solr distribution (4.5.1)
        But when i tried to update my solr version to 4.5.1 maven couldn’t download this
        Downloading: http://maven.restlet.org/org/restlet/jee/org.restlet.parent/2.1.1/org.restlet.parent-2.1.1.pom
        What is all this problem i’m really don’t get it or am i missing something on my configuration . i just want to run my solr on my tomcat and integrate with spring.
        Or do i have to go with your “running Solr with Maven”.

        Reply
        • Hi Binay,

          If you use Solr 4.1, you have to set the value of the luceneMatchVersion property to ‘4.1’.

          I know that this might not be the answer to your problem if you want to use Solr 4.5.1. It seems that the error which you encountered when you tried to use Solr 4.5.1 is a Maven related problem. Maybe the restlet.org Maven repository was down (it seems to be working fine right now), and you could not download the POM file.

          Maybe you could try updating the Solr version to 4.5.1 (remember to use the luceneMatchVersion ‘4.5’) and see if you can download the missing files.

          You can, of course, try the example application of my blog post. However, it uses Solr 4.3.X (I should probably update it when I have go time to do it).

          Reply
          • Dear Petri,
            Thank you for response, I have update the Solr version to 4.5.1 and But I got the following issue.
            org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘repositoryTodoIndexService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘todoDocumentRepositoryImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘solrTemplate’ defined in class path resource [net/petrikainulainen/spring/datasolr/config/EmbeddedSolrContext.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.data.solr.core.SolrTemplate net.petrikainulainen.spring.datasolr.config.EmbeddedSolrContext.solrTemplate() throws java.lang.Exception] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘solrServerFactoryBean’ defined in class path resource [net/petrikainulainen/spring/datasolr/config/EmbeddedSolrContext.class]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.apache.solr.core.CoreContainer.(Ljava/lang/String;Ljava/io/File;)V

          • It seems that Spring Data Solr 1.0 supports only Solr 4.3.1. The support for Solr 4.5.1 is added at Spring Data Solr 1.1.0 (no stable release yet). In other words, you can use the the following combinations of Spring Data Solr and Solr:

            • Spring Data Solr 1.0 and Solr 4.3.1
            • Spring Data Solr 1.1.0.BUILD-SNAPSHOT and Solr 4.5.1
  • Dear Petri,
    In my solr.home solr.xml contains

    and In my /apache-tomcat-7.0.41/conf/Catalina/localhost/solr.xml contains

    I have following configuration above for solr and tomcat.All other problem was solved and still got following issue when i run from netbeans however when i start tomcat manually it start both tomcat and solr.

    INFO – FileExchangeRateProvider – Reloading exchange rates from file currency.xml
    INFO – SolrCore – solr.NRTCachingDirectoryFactory
    INFO – SolrCore – [collection1] Opening new SolrCore at /home/binay/solr/collection1/, dataDir=/home/binay/solr/collection1/data/
    INFO – JmxMonitoredMap – JMX monitoring is enabled. Adding Solr mbeans to JMX Server: com.sun.jmx.mbeanserver.JmxMBeanServer@3d4c71b1
    INFO – SolrCore – [collection1] Added SolrEventListener for newSearcher: org.apache.solr.core.QuerySenderListener{queries=[]}
    INFO – SolrCore – [collection1] Added SolrEventListener for firstSearcher: org.apache.solr.core.QuerySenderListener{queries=[{q=static firstSearcher warming in solrconfig.xml}]}
    INFO – CachingDirectoryFactory – return new directory for /home/binay/solr/collection1/data
    DEBUG – CachingDirectoryFactory – Releasing directory: /home/binay/solr/collection1/data 0 false
    INFO – SolrCore – New index directory detected: old=null new=/home/binay/solr/collection1/data/index/
    INFO – CachingDirectoryFactory – return new directory for /home/binay/solr/collection1/data/index
    DEBUG – CachingDirectoryFactory – Releasing directory: /home/binay/solr/collection1/data/index 0 false
    INFO – SolrCore – [collection1] CLOSING SolrCore org.apache.solr.core.SolrCore@326b75b0
    INFO – SolrCoreState – Closing SolrCoreState
    INFO – DefaultSolrCoreState – SolrCoreState ref count has reached 0 – closing IndexWriter
    INFO – SolrCore – [collection1] Closing main searcher on request.
    INFO – CachingDirectoryFactory – Closing NRTCachingDirectoryFactory – 2 directories currently being tracked
    DEBUG – CachingDirectoryFactory – Closing NRTCachingDirectoryFactory – currently tracking: CachedDir<>
    DEBUG – CachingDirectoryFactory – Closing NRTCachingDirectoryFactory – currently tracking: CachedDir<>
    DEBUG – CachingDirectoryFactory – Closing directory when closing factory: /home/binay/solr/collection1/data
    INFO – CachingDirectoryFactory – looking to close /home/binay/solr/collection1/data [CachedDir<>]
    INFO – CachingDirectoryFactory – Closing directory: /home/binay/solr/collection1/data
    DEBUG – CachingDirectoryFactory – Closing directory when closing factory: /home/binay/solr/collection1/data/index
    INFO – CachingDirectoryFactory – looking to close /home/binay/solr/collection1/data/index [CachedDir<>]
    INFO – CachingDirectoryFactory – Closing directory: /home/binay/solr/collection1/data/index
    DEBUG – CachingDirectoryFactory – Removing from cache: CachedDir<>
    DEBUG – CachingDirectoryFactory – Removing from cache: CachedDir<>
    ERROR – CoreContainer – Unable to create core: collection1
    org.apache.solr.common.SolrException
    at org.apache.solr.core.SolrCore.(SolrCore.java:834)
    at org.apache.solr.core.SolrCore.(SolrCore.java:625)
    at org.apache.solr.core.CoreContainer.createFromLocal(CoreContainer.java:522)
    at org.apache.solr.core.CoreContainer.create(CoreContainer.java:557)
    at org.apache.solr.core.CoreContainer$1.call(CoreContainer.java:247)
    at org.apache.solr.core.CoreContainer$1.call(CoreContainer.java:239)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
    Caused by: java.nio.channels.OverlappingFileLockException
    at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255)
    at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152)
    at sun.nio.ch.FileChannelImpl.tryLock(FileChannelImpl.java:1056)
    at java.nio.channels.FileChannel.tryLock(FileChannel.java:1154)
    at org.apache.lucene.store.NativeFSLock.obtain(NativeFSLockFactory.java:217)
    at org.apache.lucene.store.NativeFSLock.isLocked(NativeFSLockFactory.java:319)
    at org.apache.lucene.index.IndexWriter.isLocked(IndexWriter.java:4245)
    at org.apache.solr.core.SolrCore.initIndex(SolrCore.java:480)
    at org.apache.solr.core.SolrCore.(SolrCore.java:755)
    … 11 more
    ERROR – CoreContainer – null:org.apache.solr.common.SolrException: Unable to create core: collection1
    at org.apache.solr.core.CoreContainer.recordAndThrow(CoreContainer.java:934)
    at org.apache.solr.core.CoreContainer.create(CoreContainer.java:566)
    at org.apache.solr.core.CoreContainer$1.call(CoreContainer.java:247)
    at org.apache.solr.core.CoreContainer$1.call(CoreContainer.java:239)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
    Caused by: org.apache.solr.common.SolrException
    at org.apache.solr.core.SolrCore.(SolrCore.java:834)
    at org.apache.solr.core.SolrCore.(SolrCore.java:625)
    at org.apache.solr.core.CoreContainer.createFromLocal(CoreContainer.java:522)
    at org.apache.solr.core.CoreContainer.create(CoreContainer.java:557)
    … 8 more
    Caused by: java.nio.channels.OverlappingFileLockException
    at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255)
    at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152)
    at sun.nio.ch.FileChannelImpl.tryLock(FileChannelImpl.java:1056)
    at java.nio.channels.FileChannel.tryLock(FileChannel.java:1154)
    at org.apache.lucene.store.NativeFSLock.obtain(NativeFSLockFactory.java:217)
    at org.apache.lucene.store.NativeFSLock.isLocked(NativeFSLockFactory.java:319)
    at org.apache.lucene.index.IndexWriter.isLocked(IndexWriter.java:4245)
    at org.apache.solr.core.SolrCore.initIndex(SolrCore.java:480)
    at org.apache.solr.core.SolrCore.(SolrCore.java:755)
    … 11 more

    Reply
    • Hi Binay,

      You cannot add XML to Wordpress comments because Wordpress removes from them before it saves the comment to the database. Anyway, since you can run Tomcat manually, I think that your configuration is fine.

      This seems like a Netbeans related problem. Unfortunately I have not used Netbeans for four years so I have no idea how you could solve this problem.

      Are you sure that another hasn’t obtained the file lock when you start Tomcat from Netbeans?

      Reply
  • Hello Petri,

    the lack of documentation about Spring Data Solr is really frustrating. Thank you for your time!
    I’ve followed your blog and tried to add Spring Data Solr functionality to my GWT application, which already worked with Solr through Solrj.
    In my case repository and service are not injected and are always null. I tried both @Resource and @Autowired annotations.
    I described the problem here: http://stackoverflow.com/questions/20198354/injecting-spring-data-solr-repository-always-returns-null

    Reply
    • Hi Konstantin,

      I have never used GWT but I assume that this is not a Spring Data Solr related problem because you cannot inject your service either. How did you integrate Spring and GWT?

      Reply
  • Hii,, I also use gxt over gwt, I tried your tutorial works well. but when I used gwt emulator the service had a problem like this ..
    Cannot resolve reference to bean ‘solrTemplate’ while setting bean property ‘solrOperations'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘solrTemplate’ defined in class path resource [application-context-solr.xml]: Cannot resolve reference to bean ‘solrServer’ while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘solrServer': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.apache.http.impl.conn.SchemeRegistryFactory.createSystemDefault()Lorg/apache/http/conn/scheme/SchemeRegistry;:

    I know, the problem is how to configure with gwt emulator works with spring data solr. But I don’t know how to resolve this, could you help me some clue ?,
    Thanks

    Reply
    • It seems that the static createSystemDefault() method of the SchemeRegisterFactory class isn’t found. This class belongs to the Apache HttpClient library. Which version of Apache HttpClient are you using? Is it possible that the GWT emulator uses another version of this library which doesn’t have this method?

      Reply
      • Thanks Petri, I’m using maven with spring data solr 1.0.0. I have check that the dependency has httpclient from apache http component version 4.2.2. I have try to exclude that the dependency to avoid conflict with existing dependency if any. I also try to search another dependency on my project to ensure there is only one httpclient dependency. Still not working ;(

        Reply
        • I’m sory, I’m forget. I use the unit test using mockito like your example works well without emulator,

          Reply
          • Hi,

            If the example works fine when you don’t use the GWT emulator, the problem is that that the emulator probably loads an incompatible version of the Apache HttpClient library. Can you see which version of the Apache HttpClient library is used by the GWT emulator?

            Have tried to resolve this conflict by using the dependency tree?

          • Yes, I have check it, everything it’s ok, Only spring data using httpclient from org.apache.httpcomponents artifact, hmmm , but I’m using common lang 2.6 on this project. Spring data using commons lang3, is it the problem ?

          • The dependencies of your project are probably just fine.

            Can you add a link to the website of the GWT emulator? I tried to google it but since I have no experience from GWT, I am not sure which search result is the correct one. Thus, it is probably best to ask this from you.

  • how to connection hadoop/share/hadoop/httpfs/tomcat with solr in ubuntu

    Reply
    • Hi,

      Could you provide a more detailed description of your problem?

      Reply
  • Hello,

    I have configured the Spellchecker successfully.

    Now can you guide me how can i use the suggestions by @Query ?

    Thanks,
    Ankur

    Reply
    • Hi Ankur,

      I answered to this question here.

      Reply
  • Hello

    Im trying to setup two different solrTemplates beans (solrTemplateA and solrTemplateB) in my xml context file, and injecting them in their corresponding repository but I’m getting the following error when loading the application context:

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘solrTemplateA': Cannot resolve reference to bean ‘solrTemplate’ while setting bean property ‘solrOperations'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘solrTemplate’ is defined

    The temaplates are config as

    And in the repository I inject the template as following:
    @Resource(name = “deviceEventsSolrTemplate”)
    SolrTemplate deviceEventsSolrTemplate;

    Do you have any idea of how this problem can be solved?

    Thanks in advance for you help

    Reply
  • Hi Petri

    I really like your spring solr data tutorial and I have learned a lot. But now I am running into an issue, infact 2 issues any help will be really appreciated.

    1. I have a Solr Document that i want to save. That Solr document has another object in it that I have created. When i try to save the document, solr does not save the data that emebbed object had.

    2. Same is happening with Map. Infaact in 1. atleast that object was storing the toString() value of the object so I could see the field for eg: Object had a field name data_txt(where _txt is wild card entry for multivalues) but in case of Map it does not even show that.

    can you please help

    Reply
    • Hi Sachin,

      here are some answers to your questions:

      1. As far as I know, Spring Data Solr doesn’t support embedded objects. Your best bet is to move the fields of this class to your “document class”.
      2. I have never tried to save Map objects to Solr index (I have saved lists of “simple” objects to multivalued fields) and the weird thing is that I couldn’t find anything interesting from Google either. That is why I have to assume that you cannot do this.
      Reply
  • Hi i have configured solr with 3 node cluseter , and trying to import the data from hbase tables for that i have used , dataimport handler, but i’m getting below error after startup tomcat ,

    i removed write.lock file in collection1/data/folder and restarted but still same issue, can u please help

    
    682050 [RecoveryThread] ERROR org.apache.solr.cloud.RecoveryStrategy  
    â Error while trying to recover. core=collection1:java.util.concurrent.ExecutionException:
     org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: 
    SolrCore 'collection1' is not available due to init failure: 
    Index locked for write for core collection1
            at java.util.concurrent.FutureTask.report(FutureTask.java:122)
            at java.util.concurrent.FutureTask.get(FutureTask.java:188)
            at org.apache.solr.cloud.RecoveryStrategy.sendPrepRecoveryCmd(RecoveryStrategy.java:611)
            at org.apache.solr.cloud.RecoveryStrategy.doRecovery(RecoveryStrategy.java:370)
            at org.apache.solr.cloud.RecoveryStrategy.run(RecoveryStrategy.java:235)
    
    
    Reply

Leave a Comment