Five minutes to integrate elastic search in springboot

Elasticsearch is an open-source distributed RESTful search and analysis engine, which enables you to store, search and analyze a large number of data quickly and almost in real time, and can solve more and more different application scenarios.

  • For example, it can be used to search the products of the online store, the search of users, and the search of transaction data.
  • Collection and storage of business
  • Report analysis

How to use elastic search to store and query data in spring boot? At present, there are several convenient ways:

  • REST Client
  • Spring Data
  • Spring Data Elasticsearch Repositories

This article will use spring data elastic search repositories to access elastic search.

get ready

You need to build an elastic search by yourself. The version of elastic search is 7.7.0. This elastic search is a stand-alone version of elastic search, and the production environment needs to use the cluster version. Execute the following command to install

# Download the rpm of elasticsearch
shasum -a 512 -c elasticsearch-7.7.0-x86_64.rpm.sha512 
sudo rpm --install elasticsearch-7.7.0-x86_64.rpm
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service
sudo systemctl start elasticsearch.servic

Modify es for other ip access: "docker-cluster"

# custom config "node-1"
discovery.seed_hosts: ["", "[::1]"]
cluster.initial_master_nodes: ["node-1"]
# Enable cross domain access support, default is false
http.cors.enabled: true
# Cross domain access allowed domain name addresses, (all domain names allowed) above use regular
http.cors.allow-origin: /.*/ 

Spring boot integrated Elasticsearch

In the pom file of spring boot project, the introduction of elastic search relies on spring boot starter data elastic search, as follows:


Configuration file in springboot File to fill in the configuration of elasticsearch. The 9300 port used here is the TCP protocol port.


Similar to most spring boot starter data JPA, entity objects are used to correspond with the storage structure fields of the database. Using @ Document(indexName = "user") annotation, a user index will be created in elasticsearch, and the white name uId field of @ id annotation is the id field of elasticsearch.

@Document(indexName = "user")
public class User implements Serializable {

    private String uId;

    private String name;

    private Integer age;

    private String address;
    //Omit getter setter 

Write an interface UserRepository to inherit the ElasticsearchRepository, which contains the basic ability of adding, deleting, modifying and querying. Add @ Repository annotation to the interface class and inject it into the spring container.

public interface UserRepository extends ElasticsearchRepository<User, String> {


UserServiceImpl class to operate the interface of UserRepository, to operate Elasticsearch.

public class UserServiceImpl implements UserService {

    private UserRepository userRepository;

    public long count() {
        return userRepository.count();

    public User save(User user) {

    public void delete(User user) {

    public Iterable<User> getAll() {
        return userRepository.findAll();

    public List<User> getByName(String name) {
        List<User> list = new ArrayList<>();
        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("name", name);
        Iterable<User> iterable =;
        return list;

    public Page<User> pageQuery(Integer pageNo, Integer pageSize, String kw) {
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchPhraseQuery("name", kw))
                .withPageable(PageRequest.of(pageNo, pageSize))

Write a test class and call the interface in turn:

public class TestController {

    private UserService userService;

    public void testInsert() {
        User user = new User();
        user.setAddress("Shenzhen, Guangdong Province");;

        user.setAddress("Shenzhen, Guangdong Province");;

        user.setAddress("Shenzhen, Guangdong Province");;


    public void testDelete() {
        User user = new User();

    public void testGetAll() {
        Iterable<User> iterable = userService.getAll();
        iterable.forEach(e -> System.out.println(e.toString()));

    public void testGetByName() {
        List<User> list = userService.getByName("lisi");

    public void testPage() {
        Page<User> page = userService.pageQuery(0, 10, "wangwu");

kibana show data

First, you need to follow kibana. The installation command is as follows:

shasum -a 512 kibana-7.7.0-x86_64.rpm 
sudo rpm --install kibana-7.7.0-x86_64.rpm

//Set kibana to start automatically
sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable kibana.service

sudo systemctl start kibana.service //Start kibana
sudo systemctl stop kibana.service   //Stop kibana

After starting successfully, visit kibana's address on the browser, enter the main interface of kibana. Then go to the Discover interface. Select the user's index to display the data normally. The data is as follows:

Tags: ElasticSearch Spring RPM sudo

Posted on Fri, 19 Jun 2020 01:52:01 -0400 by Christoph09