{{announcement.body}}
{{announcement.title}}

CRUD Application Using Spring Data Solr and Spring Boot

DZone 's Guide to

CRUD Application Using Spring Data Solr and Spring Boot

A quick and practical introduction to Spring Data Solr, covering how to make the basic configurations, define repositories, and perform CRUD operations.

· Big Data Zone ·
Free Resource

In this article, I will discuss an application with CRUD operation using Spring Data Solr and Spring Boot. 

Spring Data provides an easy and very generic way to create the repositories for each technology such as Solr, Cassandra, MongoDB, JPA etc. In my Spring Data Solr tutorial, I have discussed how to create Spring Data Solr repositories using the Spring Data interface or custom repository creation without using the Spring Data repository interface. Also, we have learned to create dynamic Solr queriessort the Solr query results, how to render the Solr query results with pagination.

Spring Data Solr CRUD Application

Apache Solr is an open source, ready-to-deploy, enterprise, full-text search engine. Here, I will show you how to do a simple Solr configuration and how to interact with the Solr server.

Step 1: Let's install the Apache Solr on your machine.

Step 2: Launch the Apache Solr as the following:

Image title

Step 3: Testing Apache Solr admin dashboard in your web browser: http://localhost:8983/solr/ as the following:

Image title

Step 4: Let’s create collections by using the following command.

bin\solr create -c Order

Step 5: After creating the Ordercollection, let’s move to index this collection with data using the application.

Adding Spring Data to the Application

The Spring team has created Spring Data projects to simplify DAO layer implementation and remove boilerplate code. Like other Spring Data projects, Spring Data Solr was also created to remove boilerplate code and simplify DAO implementation in Spring applications. Let’s see how to add Spring Data Solr in your application by using the following Maven and Gradle dependency.

Maven Dependency

Let’s start by adding the Spring Data Solr dependency to our pom.xml:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-solr</artifactId>
    <version>3.0.10.RELEASE</version>
</dependency>

Gradle Dependency

Let’s start by adding the Spring Data Solr dependency to our build.gradle:

{
compile group: 'org.springframework.data', name: 'spring-data-solr', version: '3.0.10.RELEASE'
}

You can configure the latest dependency of the Spring Data Solr project in your application. Next, let’s define a document class for the Order collection in this application.

Defining the Document Class

In this application, we are using Order collection, so, let’s define a document called Order as the following class:

package com.doj.app.pojo;

import org.springframework.data.annotation.Id;
import org.springframework.data.solr.core.mapping.Indexed;
import org.springframework.data.solr.core.mapping.SolrDocument;

/**
 * @author Dinesh.Rajput
 *
 */
@SolrDocument(collection = "Order")
public class Order {

 @Id
 @Indexed(name = "oid", type = "long")
 private Long orderid;

 @Indexed(name = "oname", type = "string")
 private String orderName;

 @Indexed(name = "odesc", type = "string")
 private String orderDescription;

 @Indexed(name = "pname", type = "string")
 private String productName;

 @Indexed(name = "cname", type = "string")
 private String customerName;

 @Indexed(name = "cmobile", type = "string")
 private String customerMobile;

 public Long getOrderid() {
  return orderid;
 }

 public void setOrderid(Long orderid) {
  this.orderid = orderid;
 }

 public String getOrderName() {
  return orderName;
 }

 public void setOrderName(String orderName) {
  this.orderName = orderName;
 }

 public String getProductName() {
  return productName;
 }

 public void setProductName(String productName) {
  this.productName = productName;
 }

 public String getCustomerName() {
  return customerName;
 }

 public void setCustomerName(String customerName) {
  this.customerName = customerName;
 }

 public String getCustomerMobile() {
  return customerMobile;
 }

 public void setCustomerMobile(String customerMobile) {
  this.customerMobile = customerMobile;
 }

 public String getOrderDescription() {
  return orderDescription;
 }

 public void setOrderDescription(String orderDescription) {
  this.orderDescription = orderDescription;
 }

}

In the above document class, we have annotated  @SolrDocument. This annotation indicates that the Order class is a Solr document and indexed to collection name  Order. In this class, you would notice, the fields are annotated with @Indexed. It indicates these fields will be indexed to the Order  collection and will be searchable. Also, a field of the Order class “orderid” is annotated with the @Id  annotation. This means that the field “orderid” is like primary key of this Solr document.

In the next section, I will define the Spring Data Solr repository interface for this application.

Defining the Repository Interface

Let’s create a repository interface for the Order Solr collection and this interface extending a repository provided by Spring Data Solr. As the given class below:

package com.doj.app.repository;

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.doj.app.pojo.Order;

/**
 * @author Dinesh.Rajput
 *
 */
public interface SolrOrderRepository extends SolrCrudRepository & lt;
Order, Long & gt; {

 Order findByOrderid(Long orderid);

 @Query("odesc:*?0*")
 Page & lt;
 Order & gt;
 findByOrderDescription(String searchTerm, Pageable pageable);

 @Query("odesc:*?0* OR oname:*?0* OR pname:*?0*")
 Page & lt;
 Order & gt;
 findByCustomerQuery(String searchTerm, Pageable pageable);

}

In the above repository interface, we have naturally parametrized this with Order and Long  as our entity IDs. In this is Spring Boot application we don’t need to configure Spring Data Solr manually for the Spring Boot application. The Spring Boot application will automatically configure the required configurations by default, according to the library available on the classpath of the application. But we have to provide the Apache Solr server 'saddress by configuring it with the application.properties file.

Application Configuration File

First, let’s see the configuration file for the Spring Boot application.

application.properties

spring.data.solr.host=http://localhost:8983/solr/

Let’s assume, if you don’t want to use default configuration of the Spring Boot application or you are not using the Spring Boot for this application then you have to explicitly configure the Spring Data Solr into this Spring Data Solr CRUD Application.

Expicitly Java-Based Configuration

Let's look at the following Java-based configuration for Spring Data Solr in the Spring Data Solr CRUD application. 

/**
 * 
 */
package com.doj.app.config;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.springframework.beans.factory.annotation.Value;
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;

/**
 * @author Dinesh.Rajput
 *
 */
@Configuration
@EnableSolrRepositories(
 basePackages = "com.doj.app.repository")
@ComponentScan
public class SolrConfig {

 @Value("spring.data.solr.host") String solrURL;

 @Bean
 public SolrClient solrClient() {
  return new HttpSolrClient.Builder(solrURL).build();
 }

 @Bean
 public SolrTemplate solrTemplate(SolrClient client) throws Exception {
  return new SolrTemplate(client);
 }
}

We are using @EnableSolrRepositories to scan the packages for repositories. 

Note: If multi-core is not enabled, then by default Spring Data will assume that Solr configuration is for a single core.

Now, let's move to performing CRUD operation for this Solr collection in the Spring Data Solr CRUD Application.

Indexing, Updating, Deleting, and Querying

In order to search documents in Solr, documents should be indexed to the Solr repository. Let’s create a controller class which takes request parameters and indexes them to the Solr document, Order.

package com.doj.app.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.doj.app.pojo.Order;
import com.doj.app.repository.SolrOrderRepository;

/**
 * @author Dinesh.Rajput
 *
 */
@RestController
public class OrderController {

 @Autowired
 SolrOrderRepository solrOrderRepository;

 @PostMapping("/order")
 public String createOrder(@RequestBody Order order) {
  String description = "Order Created";
  solrOrderRepository.save(order);
  return description;
 }

 @GetMapping("/order/{orderid}")
 public Order readOrder(@PathVariable Long orderid) {
  return solrOrderRepository.findByOrderid(orderid);
 }

 @PutMapping("/order")
 public String updateOrder(@RequestBody Order order) {
  String description = "Order Updated";
  solrOrderRepository.save(order);
  return description;
 }

 @DeleteMapping("/order/{orderid}")
 public String deleteOrder(@PathVariable Long orderid) {
  String description = "Order Deleted";
  solrOrderRepository.delete(solrOrderRepository.findByOrderid(orderid));
  return description;
 }

 @GetMapping("/order/desc/{orderDesc}/{page}")
 public List & lt;
 Order & gt;
 findOrder(@PathVariable String orderDesc, @PathVariable int page) {
  return solrOrderRepository.findByOrderDescription(orderDesc, PageRequest.of(page, 2)).getContent();
 }

 @GetMapping("/order/search/{searchTerm}/{page}")
 public List & lt;
 Order & gt;
 findOrderBySearchTerm(@PathVariable String searchTerm, @PathVariable int page) {
  return solrOrderRepository.findByCustomerQuery(searchTerm, PageRequest.of(page, 2)).getContent();
 }
}

Create

In the above controller class, we have created all our CRUD operations and provided handler methods for these operations. Let’s run this application and index some data by using the createOrder() request handler method with the /order POST endpoint.

Read

In the above controller class, we have created the readOrder() request handler method with the /order/{orderid}GET endpoint to read the data from the Solr server using the Spring Data Solr.

Update

In the above controller class, we have created the updateOrder() request handler method with the/order PUT endpoint to update a given order value.

Delete

In the above controller class, we have created the deleteOrder() request handler method with the /order/{orderid} DELETEendpoint to delete a document from the Solr collection.

Image title

Image title

Summary

This article has been all about the Spring Data Solr with Spring Boot and Apache Solr. It is a quick and practical introduction to Spring Data Solr, covering how to make the basic configurations, define repositories, and perform CRUD operation. This application is available as a sample project on GitHub.

Topics:
spring data ,java ,big datat ,tutorial ,solr

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}