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 application.properties, 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 elasticsearch-2.4.0.zip. Extract elasticsearch-2.4.0.zip
– 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:

Logs

III. Sourcecode

SpringBootElasticSearch

By JavaSampleApproach | April 14, 2017.

Last updated on August 16, 2017.


Related Posts


Got Something To Say:

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

*