Spring Data Solr project created
This commit is contained in:
parent
412a93ce68
commit
771a81d5bd
|
@ -0,0 +1,78 @@
|
|||
<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.baeldung</groupId>
|
||||
<artifactId>spring-data-solr</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-data-solr</name>
|
||||
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<spring.version>4.2.5.RELEASE</spring.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
<spring-data-solr>2.0.4.RELEASE</spring-data-solr>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-solr</artifactId>
|
||||
<version>${spring-data-solr}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>1.2.16</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<includes>
|
||||
<include>**/*IntegrationTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.spring.data.solr.config;
|
||||
|
||||
import org.apache.solr.client.solrj.SolrClient;
|
||||
import org.apache.solr.client.solrj.impl.HttpSolrClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.solr.core.SolrTemplate;
|
||||
import org.springframework.data.solr.repository.config.EnableSolrRepositories;
|
||||
|
||||
@Configuration
|
||||
@EnableSolrRepositories(basePackages = "com.baeldung.spring.data.solr.repository", namedQueriesLocation = "classpath:solr-named-queries.properties", multicoreSupport = true)
|
||||
@ComponentScan
|
||||
public class SolrConfig {
|
||||
|
||||
@Bean
|
||||
public SolrClient solrClient() {
|
||||
return new HttpSolrClient("http://localhost:8983/solr");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SolrTemplate solrTemplate(SolrClient client) throws Exception {
|
||||
return new SolrTemplate(client);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package com.baeldung.spring.data.solr.model;
|
||||
|
||||
import org.apache.solr.client.solrj.beans.Field;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.solr.core.mapping.SolrDocument;
|
||||
|
||||
@SolrDocument(solrCoreName="product")
|
||||
public class Product {
|
||||
|
||||
@Id
|
||||
@Field("id")
|
||||
private String id;
|
||||
|
||||
@Field("name")
|
||||
private String name;
|
||||
|
||||
@Field("category")
|
||||
private String category;
|
||||
|
||||
@Field("description")
|
||||
private String description;
|
||||
|
||||
|
||||
public Product(String id,String name,String category){
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public void setCategory(String category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.spring.data.solr.repository;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.solr.repository.Query;
|
||||
import org.springframework.data.solr.repository.SolrCrudRepository;
|
||||
|
||||
import com.baeldung.spring.data.solr.model.Product;
|
||||
|
||||
public interface ProductRepository extends SolrCrudRepository<Product, String>{
|
||||
|
||||
public List<Product> findByName(String name);
|
||||
|
||||
@Query("name:*?0* OR category:*?0* OR description:*?0*")
|
||||
public Page<Product> findByCustomQuery(String searchTerm,Pageable pageable);
|
||||
|
||||
|
||||
// @Query(name="Product.findByNamedQuery")
|
||||
public Page<Product> findByNamedQuery(String searchTerm,Pageable pageable);
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
Product.findByNamedQuery=name:*?0* OR category:*?0* OR description:*?0*
|
|
@ -0,0 +1,117 @@
|
|||
package com.baeldung.spring.data.solr.repo;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.spring.data.solr.config.SolrConfig;
|
||||
import com.baeldung.spring.data.solr.model.Product;
|
||||
import com.baeldung.spring.data.solr.repository.ProductRepository;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = SolrConfig.class)
|
||||
public class ProductRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ProductRepository productRepository;
|
||||
|
||||
@Before
|
||||
public void clearSolrData(){
|
||||
productRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSavingProduct_thenAvailableOnRetrieval() throws Exception{
|
||||
final Product product = new Product("P00001","Desk","Furniture");
|
||||
product.setDescription("New Desk");
|
||||
productRepository.save(product);
|
||||
final Product retrievedProduct = productRepository.findOne(product.getId());
|
||||
assertEquals(product.getId(),retrievedProduct.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpdatingProduct_thenChangeAvailableOnRetrieval() throws Exception {
|
||||
final Product product = new Product("P0001", "T-Shirt","Kitchen");
|
||||
product.setDescription("New T-Shirt");
|
||||
productRepository.save(product);
|
||||
|
||||
product.setCategory("Clothes");
|
||||
productRepository.save(product);
|
||||
|
||||
final Product retrievedProduct = productRepository.findOne(product.getId());
|
||||
assertEquals(product.getCategory(), retrievedProduct.getCategory());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void whenDeletingProduct_thenNotAvailableOnRetrieval() throws Exception {
|
||||
final Product product = new Product("P0001", "Desk","Furniture");
|
||||
product.setDescription("New Desk");
|
||||
productRepository.save(product);
|
||||
|
||||
productRepository.delete(product);
|
||||
|
||||
Product retrievedProduct = productRepository.findOne(product.getId());
|
||||
assertNull(retrievedProduct);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindByName_thenAvailableOnRetrieval() throws Exception{
|
||||
final Product phone = new Product("P0001", "Phone", "Electronics");
|
||||
phone.setDescription("New Phone");
|
||||
productRepository.save(phone);
|
||||
|
||||
List<Product> retrievedProducts = productRepository.findByName("Phone");
|
||||
assertEquals(phone.getId(), retrievedProducts.get(0).getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSearchingProductsByQuery_thenAllMatchingProductsShouldAvialble() throws Exception {
|
||||
final Product phone = new Product("P0001", "Smart Phone", "Electronics");
|
||||
phone.setDescription("New Item");
|
||||
productRepository.save(phone);
|
||||
|
||||
final Product phoneCover = new Product("P0002", "Cover", "Phone");
|
||||
phoneCover.setDescription("New Product");
|
||||
productRepository.save(phoneCover);
|
||||
|
||||
final Product wirelessCharger = new Product("P0003", "Charging Cable", "Cable");
|
||||
wirelessCharger.setDescription("Wireless Charger for Phone");
|
||||
productRepository.save(wirelessCharger);
|
||||
|
||||
Page<Product> result = productRepository.findByCustomQuery("Pho", new PageRequest(0, 10));
|
||||
assertEquals(3, result.getNumberOfElements());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSearchingProductsByNamedQuery_thenAllMatchingProductsShouldAvialble() throws Exception {
|
||||
final Product phone = new Product("P0001", "Smart Phone", "Electronics");
|
||||
phone.setDescription("New Item");
|
||||
productRepository.save(phone);
|
||||
|
||||
final Product phoneCover = new Product("P0002", "Cover", "Phone");
|
||||
phoneCover.setDescription("New Product");
|
||||
productRepository.save(phoneCover);
|
||||
|
||||
final Product wirelessCharger = new Product("P0003", "Charging Cable", "Cable");
|
||||
wirelessCharger.setDescription("Wireless Charger for Phone");
|
||||
productRepository.save(wirelessCharger);
|
||||
|
||||
Page<Product> result = productRepository.findByNamedQuery("one", new PageRequest(0, 10));
|
||||
assertEquals(3, result.getNumberOfElements());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue