In today’s post I would share a sample program to index and find documents into/from Apache Lucene Solr using Spring Data Solr. If you don’t have Solr in your local machine please download and install Solr from official site. This program is tested against the Solr server running in localhost and in default port. (http://localhost:8983/solr/) In this test program we will index two Product objects into Solr preloaded collection named collection1. Please see the schema changes below.
The complete source code for this post can be found in github.
Libraries used
Name | Version |
---|---|
SolrJ | 4.6.0 |
Spring Core | 4.0.0.RELEASE |
Spring Data Solr | 1.0.0.RELEASE |
JUnit | 4.8.1 |
Spring Test | 4.0.0.RELEASE |
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ourownjava</groupId> <artifactId>spring-data-solr</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.framework.version>4.0.0.RELEASE</spring.framework.version> <slf4j.version>1.6.1</slf4j.version> <junit.version>4.8.1</junit.version> <solr.version>4.6.0</solr.version> <spring.data.solr.version>1.0.0.RELEASE</spring.data.solr.version> </properties> <dependencies> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>${solr.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.framework.version}</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-solr</artifactId> <version>${spring.data.solr.version}</version> </dependency> <!-- testing dependencies --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.framework.version}</version> <scope>test</scope> </dependency> </dependencies> </project> |
ProductRepository
1 2 3 4 5 6 7 8 9 10 11 |
package com.ourownjava.spring.data.solr; import java.util.List; import org.springframework.data.solr.repository.SolrCrudRepository; /** * * @author ourownjava.com * */ public interface ProductRepository extends SolrCrudRepository<Product, String>{ public List<Product> findByName(final String name); } |
Product
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.ourownjava.spring.data.solr; import org.apache.solr.client.solrj.beans.Field; /** * * @author ourownajava.com * */ public class Product { @Field private String id; @Field private String name; @Field private Double price; public String getId() { return id; } public void setId(final String id) { this.id = id; } public String getName() { return name; } public void setName(final String name) { this.name = name; } public Double getPrice() { return price; } public void setPrice(final Double price) { this.price = price; } } |
TestProductRepository
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package com.ourownjava.spring.data.solr; import static org.junit.Assert.assertEquals; import java.util.ArrayList; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * @author ourownjava.com */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:test-root-context.xml") public class TestProductRepository { @Autowired private ApplicationContext applicationContext; @Autowired private ProductRepository productRepository; @Test public void shouldIndexProducts(){ productRepository.save(getProducts()); final Product product = productRepository.findOne("p1"); assertEquals("p1", product.getId()); assertEquals("pname1", product.getName()); } @Test public void shouldFindByName(){ final List<Product> products = productRepository.findByName("pname2"); assertEquals("p2", products.get(0).getId()); assertEquals("pname2", products.get(0).getName()); } private List<Product> getProducts(){ final List<Product> products = new ArrayList<Product>(); final Product product = new Product(); product.setId("p1"); product.setName("pname1"); product.setPrice(11.11); products.add(product); final Product product1 = new Product(); product1.setId("p2"); product1.setName("pname2"); product1.setPrice(12.12); products.add(product1); return products; } } |
test-root-context.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?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:mongo="http://www.springframework.org/schema/data/mongo" 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-3.2.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-1.0.xsd"> <description>test application context configuration</description> <context:component-scan base-package="com.ourownjava*" /> <solr:solr-server id="solrServer" url="http://localhost:8983/solr" /> <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate"> <constructor-arg index="0" ref="solrServer"/> </bean> <solr:repositories base-package="com.ourownjava.spring.data.solr" /> </beans> |
schema.xml addition
1 2 3 |
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="name" type="text_general" indexed="true" stored="true" omitNorms="true"/> <field name="price" type="double" indexed="true" stored="true" omitNorms="true"/> |
am doing solr integration with atg..could you please help me from the beginning…
Which version of ATG are you using?
10.1.1