Skip to content

Commit 771a81d

Browse files
committed
Spring Data Solr project created
1 parent 412a93c commit 771a81d

6 files changed

Lines changed: 307 additions & 0 deletions

File tree

‎spring-data-solr/pom.xml‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>spring-data-solr</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>spring-data-solr</name>
11+
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<spring.version>4.2.5.RELEASE</spring.version>
16+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
17+
<spring-data-solr>2.0.4.RELEASE</spring-data-solr>
18+
</properties>
19+
20+
<dependencies>
21+
22+
<dependency>
23+
<groupId>org.springframework</groupId>
24+
<artifactId>spring-core</artifactId>
25+
<version>${spring.version}</version>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.springframework.data</groupId>
30+
<artifactId>spring-data-solr</artifactId>
31+
<version>${spring-data-solr}</version>
32+
</dependency>
33+
34+
35+
<dependency>
36+
<groupId>org.springframework</groupId>
37+
<artifactId>spring-context</artifactId>
38+
<version>${spring.version}</version>
39+
</dependency>
40+
41+
42+
<dependency>
43+
<groupId>log4j</groupId>
44+
<artifactId>log4j</artifactId>
45+
<version>1.2.16</version>
46+
</dependency>
47+
48+
49+
<dependency>
50+
<groupId>junit</groupId>
51+
<artifactId>junit</artifactId>
52+
<version>4.12</version>
53+
<scope>test</scope>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>org.springframework</groupId>
58+
<artifactId>spring-test</artifactId>
59+
<version>${spring.version}</version>
60+
<scope>test</scope>
61+
</dependency>
62+
63+
</dependencies>
64+
<build>
65+
<plugins>
66+
<plugin>
67+
<groupId>org.apache.maven.plugins</groupId>
68+
<artifactId>maven-surefire-plugin</artifactId>
69+
<version>${maven-surefire-plugin.version}</version>
70+
<configuration>
71+
<includes>
72+
<include>**/*IntegrationTest.java</include>
73+
</includes>
74+
</configuration>
75+
</plugin>
76+
</plugins>
77+
</build>
78+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.spring.data.solr.config;
2+
3+
import org.apache.solr.client.solrj.SolrClient;
4+
import org.apache.solr.client.solrj.impl.HttpSolrClient;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.ComponentScan;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.data.solr.core.SolrTemplate;
9+
import org.springframework.data.solr.repository.config.EnableSolrRepositories;
10+
11+
@Configuration
12+
@EnableSolrRepositories(basePackages = "com.baeldung.spring.data.solr.repository", namedQueriesLocation = "classpath:solr-named-queries.properties", multicoreSupport = true)
13+
@ComponentScan
14+
public class SolrConfig {
15+
16+
@Bean
17+
public SolrClient solrClient() {
18+
return new HttpSolrClient("http://localhost:8983/solr");
19+
}
20+
21+
@Bean
22+
public SolrTemplate solrTemplate(SolrClient client) throws Exception {
23+
return new SolrTemplate(client);
24+
}
25+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.spring.data.solr.model;
2+
3+
import org.apache.solr.client.solrj.beans.Field;
4+
import org.springframework.data.annotation.Id;
5+
import org.springframework.data.solr.core.mapping.SolrDocument;
6+
7+
@SolrDocument(solrCoreName="product")
8+
public class Product {
9+
10+
@Id
11+
@Field("id")
12+
private String id;
13+
14+
@Field("name")
15+
private String name;
16+
17+
@Field("category")
18+
private String category;
19+
20+
@Field("description")
21+
private String description;
22+
23+
24+
public Product(String id,String name,String category){
25+
this.id = id;
26+
this.name = name;
27+
this.category = category;
28+
}
29+
30+
public String getId() {
31+
return id;
32+
}
33+
34+
public void setId(String id) {
35+
this.id = id;
36+
}
37+
38+
public String getName() {
39+
return name;
40+
}
41+
42+
public void setName(String name) {
43+
this.name = name;
44+
}
45+
46+
public String getCategory() {
47+
return category;
48+
}
49+
50+
public void setCategory(String category) {
51+
this.category = category;
52+
}
53+
54+
public String getDescription() {
55+
return description;
56+
}
57+
58+
public void setDescription(String description) {
59+
this.description = description;
60+
}
61+
62+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.spring.data.solr.repository;
2+
3+
4+
import java.util.List;
5+
6+
import org.springframework.data.domain.Page;
7+
import org.springframework.data.domain.Pageable;
8+
import org.springframework.data.solr.repository.Query;
9+
import org.springframework.data.solr.repository.SolrCrudRepository;
10+
11+
import com.baeldung.spring.data.solr.model.Product;
12+
13+
public interface ProductRepository extends SolrCrudRepository<Product, String>{
14+
15+
public List<Product> findByName(String name);
16+
17+
@Query("name:*?0* OR category:*?0* OR description:*?0*")
18+
public Page<Product> findByCustomQuery(String searchTerm,Pageable pageable);
19+
20+
21+
// @Query(name="Product.findByNamedQuery")
22+
public Page<Product> findByNamedQuery(String searchTerm,Pageable pageable);
23+
24+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Product.findByNamedQuery=name:*?0* OR category:*?0* OR description:*?0*
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.baeldung.spring.data.solr.repo;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNull;
5+
6+
import java.util.List;
7+
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.data.domain.Page;
13+
import org.springframework.data.domain.PageRequest;
14+
import org.springframework.test.context.ContextConfiguration;
15+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
16+
17+
import com.baeldung.spring.data.solr.config.SolrConfig;
18+
import com.baeldung.spring.data.solr.model.Product;
19+
import com.baeldung.spring.data.solr.repository.ProductRepository;
20+
21+
22+
@RunWith(SpringJUnit4ClassRunner.class)
23+
@ContextConfiguration(classes = SolrConfig.class)
24+
public class ProductRepositoryIntegrationTest {
25+
26+
@Autowired
27+
private ProductRepository productRepository;
28+
29+
@Before
30+
public void clearSolrData(){
31+
productRepository.deleteAll();
32+
}
33+
34+
@Test
35+
public void whenSavingProduct_thenAvailableOnRetrieval() throws Exception{
36+
final Product product = new Product("P00001","Desk","Furniture");
37+
product.setDescription("New Desk");
38+
productRepository.save(product);
39+
final Product retrievedProduct = productRepository.findOne(product.getId());
40+
assertEquals(product.getId(),retrievedProduct.getId());
41+
}
42+
43+
@Test
44+
public void whenUpdatingProduct_thenChangeAvailableOnRetrieval() throws Exception {
45+
final Product product = new Product("P0001", "T-Shirt","Kitchen");
46+
product.setDescription("New T-Shirt");
47+
productRepository.save(product);
48+
49+
product.setCategory("Clothes");
50+
productRepository.save(product);
51+
52+
final Product retrievedProduct = productRepository.findOne(product.getId());
53+
assertEquals(product.getCategory(), retrievedProduct.getCategory());
54+
}
55+
56+
57+
58+
@Test
59+
public void whenDeletingProduct_thenNotAvailableOnRetrieval() throws Exception {
60+
final Product product = new Product("P0001", "Desk","Furniture");
61+
product.setDescription("New Desk");
62+
productRepository.save(product);
63+
64+
productRepository.delete(product);
65+
66+
Product retrievedProduct = productRepository.findOne(product.getId());
67+
assertNull(retrievedProduct);
68+
69+
}
70+
71+
@Test
72+
public void whenFindByName_thenAvailableOnRetrieval() throws Exception{
73+
final Product phone = new Product("P0001", "Phone", "Electronics");
74+
phone.setDescription("New Phone");
75+
productRepository.save(phone);
76+
77+
List<Product> retrievedProducts = productRepository.findByName("Phone");
78+
assertEquals(phone.getId(), retrievedProducts.get(0).getId());
79+
}
80+
81+
@Test
82+
public void whenSearchingProductsByQuery_thenAllMatchingProductsShouldAvialble() throws Exception {
83+
final Product phone = new Product("P0001", "Smart Phone", "Electronics");
84+
phone.setDescription("New Item");
85+
productRepository.save(phone);
86+
87+
final Product phoneCover = new Product("P0002", "Cover", "Phone");
88+
phoneCover.setDescription("New Product");
89+
productRepository.save(phoneCover);
90+
91+
final Product wirelessCharger = new Product("P0003", "Charging Cable", "Cable");
92+
wirelessCharger.setDescription("Wireless Charger for Phone");
93+
productRepository.save(wirelessCharger);
94+
95+
Page<Product> result = productRepository.findByCustomQuery("Pho", new PageRequest(0, 10));
96+
assertEquals(3, result.getNumberOfElements());
97+
}
98+
99+
@Test
100+
public void whenSearchingProductsByNamedQuery_thenAllMatchingProductsShouldAvialble() throws Exception {
101+
final Product phone = new Product("P0001", "Smart Phone", "Electronics");
102+
phone.setDescription("New Item");
103+
productRepository.save(phone);
104+
105+
final Product phoneCover = new Product("P0002", "Cover", "Phone");
106+
phoneCover.setDescription("New Product");
107+
productRepository.save(phoneCover);
108+
109+
final Product wirelessCharger = new Product("P0003", "Charging Cable", "Cable");
110+
wirelessCharger.setDescription("Wireless Charger for Phone");
111+
productRepository.save(wirelessCharger);
112+
113+
Page<Product> result = productRepository.findByNamedQuery("one", new PageRequest(0, 10));
114+
assertEquals(3, result.getNumberOfElements());
115+
}
116+
117+
}

0 commit comments

Comments
 (0)