How to start SpringBoot ElasticSearch using Spring Data

Elasticsearch is a distributed, full-text search engine based on Lucene with JSON schema. In the tutorial, JavaSampleApproach will show you how to start SpringBoot ElasticSearch by sample code.

Related posts:
Apache Solr – How to start Spring Data Solr with SpringBoot

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.1.RELEASE
– ElasticSearch: 2.4.0

II. Practice

Step to do
– Create SpringBoot project
– Create a mapping model
– Create an ElasticSearch repository
– Create a test client
– Run & check result

1. Create SpringBoot project

– Using SpringToolSuite, create a SpringBoot project. Then add elasticsearch dependency:

2. Create a mapping model

Using annotation: @Document(indexName = "javasampleapproach", type = "customer")

3. Create an ElasticSearch repository

Create an interface just extends ElasticsearchRepository

Open, configure elasticsearch cluster:

4. Create a test client

In main class, override function run(String... arg0) of CommandLineRunner interface.
Using @Resource CustomerRepository customerRepository to work with ES:

– Save documents:

– Find a Document by Id

– Find all documents:

– Find documents by firstname field:

– Find a document page by firstname field:

– Find documents by age:

– Delete a document:

Full SourceCode:

5. Run & Check results
5.1 Setup Elasticsearch

– Download Extract
– Then go to: \elasticsearch-2.4.0\bin , run elasticsearch.bat by cmd

springboot elasticsearch - run

5.2 Run & Check results

Build & Run the project with Spring Boot App mode:


III. Sourcecode


By grokonez | April 14, 2017.

Last updated on August 16, 2017.

Related Posts

4 thoughts on “How to start SpringBoot ElasticSearch using Spring Data”

  1. Hi

    I am new to Elastic Search and your post was very helpful for my scenario. When I try to run your application with the below configuration I am getting following error. Any inputs will be helpful thank you in advance.
    ElasticSearch :- 5.5.1

    I am able to access my ElasticServer


    Error :-


    1. Give authentication details in application.proeprties. For authentication, you need to add ‘x-pack’ add-on in elastic-search

Got Something To Say:

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