Elasticsearch integration with Spring Data

We will configure Elasticsearch as a database to provide various CRUD operations for Blogpress applications. We will use Spring Data for this integration. Spring Data provides an abstract layer for data access from various providers, such as a relational database, a non-relational database, a map-reduced framework, and cloud services.

For each of these data providers, Spring supplies a set of libraries to interact with, while maintaining the abstraction to interact with them in a symmetrical manner. Spring Data spans across various modules, including Spring Data Common, Spring Data JPA, Spring Data REST, Spring Data LDAP, Spring Data MongoDB, Spring Data JDBC, and many more. Spring Data Elasticsearch is one of them t provide data access with Elasticsearch search engines.

We will use the Spring Data Elasticsearch module for the Blogpress application. The very first thing is to make this module available in our application. Unsurprisingly, this can be done by defining a starter in pom.xml as follows:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

Elasticsearch provides a Java API to interact with it programmatically. Soon after you activate the above starter, the required JARs will be added in the classpath to access the Elasticsearch Java API. At this moment, we need to instruct the Spring Data Elasticsearch module about the cluster name, port, and hostname on which the Elasticsearch server is running. You can define these configurations in the application.properties file (in the src/main/resource folder) as follows:

elasticsearch.clustername=elasticsearch
elasticsearch.host=localhost
elasticsearch.port=9300

This is equivalent to defining a database URL, the driver class name, and credentials for database interaction with Spring Data. The next step is to define a configuration class, which basically uses the previous details and prepares the required artifacts to interact with Elasticsearch, as follows:

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.nilangpatel.blogpress.repository")
@ComponentScan(basePackages = { "com.nilangpatel.blogpress.config" })
public class ElasticDataConfig {

@Value("${elasticsearch.host}")
private String esHost;

@Value("${elasticsearch.port}")
private int esPort;

@Value("${elasticsearch.clustername}")
private String esClusterName;

@Bean
public Client client() throws Exception {

TransportClientFactoryBean transportClientFactory = new TransportClientFactoryBean();
transportClientFactory.setClusterName(esClusterName);
transportClientFactory.afterPropertiesSet();

return transportClientFactory.getObject()
.addTransportAddress(
new TransportAddress(InetAddress.getByName(esHost), esPort));
}

@Bean
public ElasticsearchTemplate elasticsearchTemplate() throws Exception {
return new ElasticsearchTemplate(client());
}

}

This class reads the cluster name, port, and host values defined in the application.properties file with the @Value annotation. The client() method uses the TransactionClientFactory object to read the configuration data and return an object of the TransportClient class, which represents the client interface to interact with Elasticsearch.

The next elasticsearchTemplate() method uses this client object and creates the ElasticsearchTemplate object. Spring provides the data access template class for each of the data providers. The object of the ElasticsearchTemplate class is initialized with the object of the TransportClient class. This method is defined with the @Bean annotation so that the object of ElasticsearchTemplate is accessible with the @Autowired annotation to other classes.

This initialization happens when starting an application. The ElasticsearchTemplate class is the single point of interaction of the Elasticsearch engine with Spring Data. The @EnableElasticsearchRepositories annotation in this class is used to point the Spring JPA repository package that we are going to define next. Before that, we will first define a model class that represents a document in Elasticsearch.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.238.70