1. Overview

In this article, we will be going to discuss how to integrate Elastic Search with spring boot. Spring boot started data Elastic Search is the extension of spring data which used to integrate elastic search with spring boot. Need to add spring-boot-starter-data-elasticsearch maven dependency in pom.xml

Here we have discussed step by step process and complete example with output to understand spring boot with the elastic search.

Steps to configure Elastic with spring boot

Step 1: Add spring-boot-starter-data-elasticsearch in pom.xml or Gradle file

Step 2: Create Elastic Search Bean

@Bean
public Client client() throws Exception {
    return new PreBuiltTransportClient(Settings.EMPTY)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(EsHost), EsPort));
}
@Bean
public ElasticsearchOperations elasticsearchTemplate() throws Exception {
    return new ElasticsearchTemplate(client());
}

Step 3: Make sure that our server running in same port that configures with client Bean

If you haven’t download Elastic search refer this link to download the latest version of Elastic Search and perform below steps to start the elastic search with default configuration.

Start Elastic Search

ES_HOME indicate where our elastic is available. No need to set ES_HOME as the environment, for our references we are using ES_HOME as base directory of ES.

//Go To Elastic Installation Directory 
cd ES_HOME\elasticsearch-6.2.4\bin
//Run bat file
elasticsearch.bat

Elastic search is up and running now, to verify ES, open browser and enter URL as localhost:9200, If everything is right you will able to see ES and Lucene version along with cluster information as below. : localhost:9200

{
    "name": "hB6XzMX",
    "cluster_name": "elasticsearch",
    "cluster_uuid": "_na_",
    "version": {
        "number": "6.2.4",
        "build_hash": "ccec39f",
        "build_date": "2018-04-12T20:37:28.497551Z",
        "build_snapshot": false,
        "lucene_version": "7.2.1",
        "minimum_wire_compatibility_version": "5.6.0",
        "minimum_index_compatibility_version": "5.0.0"
    },
    "tagline": "You Know, for Search"
}

Step 4: Create Elastic Service Repository which helps us to index and search document in Elastic Search. The repository is an interface which extends ElasticsearchRepository. ElasticsearchRepository example code available in bellow example:

2. spring boot elastic search example

In this example, we have used Elastic Search 6.2.4 with Spring boot 2.0.2. It is mandatory to use Spring boot 2.0.2 with this elastic search version.

Project Structure

Spring Boot Elastic Search Project Structure

Project Structure

2.1 pom.xml

  • spring-boot-starter-data-elasticsearch for elastic search.
<?xml version="1.0" encoding="UTF-8"?>
<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>ElasticSearchSpringBoot</groupId>
    <artifactId>ElasticSearchSpringBoot</artifactId>
    <version>1.0-SNAPSHOT</version>
    <description>Spring boot Elastic Search Version 6.2.4 Example</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.2 application.properties

elasticsearch.* are properties related to elastic search configurations. Other configurations related to elastic are available here

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

2.3 SpringBootConfig

package com.javadeveloperzone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootConfig.class, args);            // it wil start application
    }
}

2.4 ElasticSearchConfig

Elastic Search related configuration like elastic search host, port, cluster name etc.

package com.javadeveloperzone;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import java.net.InetAddress;

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.javadeveloperzone.repository")
public class ElasticSearchConfig {
    @Value("${elasticsearch.host}")
    private String EsHost;
    @Value("${elasticsearch.port}")
    private int EsPort;
    @Value("${elasticsearch.clustername}")
    private String EsClusterName;
    @Bean
    public Client client() throws Exception {
        return new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(EsHost), EsPort));
    }
    @Bean
    public ElasticsearchOperations elasticsearchTemplate() throws Exception {
        return new ElasticsearchTemplate(client());
    }
    //Embedded Elasticsearch Server
    /*@Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchTemplate(nodeBuilder().local(true).node().client());
    }*/
}

2.5 Creating a Document

A pojo class with Document annotation with indexName and type mapping. @id annotation indicates the field is a unique key field.

package com.javadeveloperzone.model;
import org.springframework.data.annotation.Id;

@org.springframework.data.elasticsearch.annotations.Document(indexName = "employee", type = "employee")
public class Document {
  @Id
  private String id;
  private String docType;
  private String docTitle;
  public Document() {
  }
  
  public Document(String id, String docType, String docTitle){
    this.id = id;
    this.docTitle = docTitle;
    this.docType = docType;
  }
  
  public void setId(String id){
    this.id = id;
  }
  
  public String getId(){
    return this.id;
  }
  @Override
  public String toString() {
    return "Document{" +
        "id='" + id + '\'' +
        ", docType='" + docType + '\'' +
        ", docTitle='" + docTitle + '\'' +
        '}';
  }
  public String getDocType() {
    return docType;
  }
  public void setDocType(String docType) {
    this.docType = docType;
  }
  public String getDocTitle() {
    return docTitle;
  }
  public void setDocTitle(String docTitle) {
    this.docTitle = docTitle;
  }
}

2.6 DocumentRepository

Our Document Repository which extends ElasticSearchRepository class and provides a various method of finding required information from elastic search.

package com.javadeveloperzone.repository;
import java.util.List;
import com.javadeveloperzone.model.Document;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface DocumentRepository extends ElasticsearchRepository<Document, String> {
  List<Document> findByDocTitleEndsWith(String name);
  List<Document> findByDocTitleStartsWith(String name);
  List<Document> findByDocTypeEndsWith(String name);
  List<Document> findByDocTypeStartsWith(String name);
}

2.7 ESDocumentController

Rest Controller which handle all the requests like delete, save, retrieve etc…

package com.javadeveloperzone.controller;
import com.javadeveloperzone.model.Document;
import com.javadeveloperzone.repository.DocumentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * Created by JavaDeveloperZone.
 */
@RestController
public class ESDocumentController {
    @Autowired
    private DocumentRepository documentRepository;
    @RequestMapping("/")
    public String SpringBootESExample() {
        return "Welcome to Spring Boot Elastic Search Example";
    }
    @DeleteMapping("/delete")
    public String deleteAllDocuments() {
        try {   //delete all documents from solr core
            documentRepository.deleteAll();
            return "documents deleted succesfully!";
        }catch (Exception e){
        return "Failed to delete documents";
        }
    }
    @PostMapping("/save")
    public String saveAllDocuments() {
        //Store Documents
        documentRepository.saveAll(Arrays.asList(new Document("1", "pdf","Java Dev Zone"),
                new Document("2", "msg", "subject:reinvetion"),
                new Document("3", "pdf", "Spring boot sessions"),
                new Document("4", "docx", "meeting agenda"),
                new Document("5", "docx", "Spring boot + Elastic Search")));
        return "5 documents saved!!!";
    }
    @GetMapping("/getAll")
    public List<Document> getAllDocs() {
        List<Document> documents = new ArrayList<>();
        // iterate all documents and add it to list
        for (Document doc : this.documentRepository.findAll()) {
            documents.add(doc);
        }
        return documents;
    }
}

 

2.8 Build & Run Application

2018-06-05 23:12:41.760  INFO 3552 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-06-05 23:12:43.401  INFO 3552 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.ser[email protected]4c1d9d4b: startup date [Tue Jun 05 23:12:16 IST 2018]; root of context hierarchy
2018-06-05 23:12:43.666  INFO 3552 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.javadeveloperzone.controller.ESDocumentController.SpringBootESExample()
2018-06-05 23:12:43.666  INFO 3552 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/delete]}" onto public java.lang.String com.javadeveloperzone.controller.ESDocumentController.deleteAllDocuments()
2018-06-05 23:12:43.666  INFO 3552 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/save]}" onto public java.lang.String com.javadeveloperzone.controller.ESDocumentController.saveAllDocuments()
2018-06-05 23:12:43.666  INFO 3552 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/getAll]}" onto public java.util.List<com.javadeveloperzone.model.Document> com.javadeveloperzone.controller.ESDocumentController.getAllDocs()
2018-06-05 23:12:43.682  INFO 3552 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-06-05 23:12:43.682  INFO 3552 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-06-05 23:12:43.748  INFO 3552 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-06-05 23:12:43.749  INFO 3552 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-06-05 23:12:44.611  INFO 3552 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-06-05 23:12:44.751  INFO 3552 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-06-05 23:12:44.782  INFO 3552 --- [           main] com.javadeveloperzone.SpringBootConfig   : Started SpringBootConfig in 35.505 seconds (JVM running for 42.355)

2.9 Output

1 Save Documents

http://localhost:8080/save

Spring Boot Elastic Search Example Save

2 Retrieved Documents

http://localhost:8080/getAll

[
    {
        "id": "5",
        "docType": "docx",
        "docTitle": "Spring boot + Elastic Search"
    },
    {
        "id": "2",
        "docType": "msg",
        "docTitle": "subject:reinvetion"
    },
    {
        "id": "4",
        "docType": "docx",
        "docTitle": "meeting agenda"
    },
    {
        "id": "1",
        "docType": "pdf",
        "docTitle": "Java Dev Zone"
    },
    {
        "id": "3",
        "docType": "pdf",
        "docTitle": "Spring boot sessions"
    }
]

 

3 Delete Documents

http://localhost:8080/delete

Spring Boot Elastic Search Example Delete

 

3. Conclusion

In this article, we have discussed how to integrate elastic search with spring boot. We have also discussed various usage like save documents, delete documents, retrieve documents from elastic search.

4. References

5. Source Code

Elastic Search Spring Boot Document Example

You can also download the source code of Elastic Search Spring Boot Integration Example and other useful examples from our git repository.

Was this post helpful?

4 comments. Leave new

¿how to use with xpack?
plz helpme

We are working on more example of the elastic search. We will also publish xpack example as soon as possible.

You can refer : https://www.elastic.co/guide/en/x-pack/current/xpack-introduction.html

We will publish more elastic search example in category : https://javadeveloperzone.com/spring-boot/spring-boot-elastic-search-example/

Can you attach correct source code? Current contains solr project.

Thank you for report us. The code has been updated.

Leave a Reply

Your email address will not be published. Required fields are marked *