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
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.7.0-x86_64.rpm
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.7.0-x86_64.rpm.sha512
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:

cluster.name: "docker-cluster"
network.host: 0.0.0.0

# custom config
node.name: "node-1"
discovery.seed_hosts: ["127.0.0.1", "[::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:

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

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

server.port=8500
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
spring.data.elasticsearch.cluster-name=my-application

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 {

    @Id
    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.

@Repository
public interface UserRepository extends ElasticsearchRepository<User, String> {

}

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

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;


    @Override
    public long count() {
        return userRepository.count();
    }

    @Override
    public User save(User user) {
        return userRepository.save(user);
    }

    @Override
    public void delete(User user) {
        userRepository.delete(user);
    }

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

    @Override
    public List<User> getByName(String name) {
        List<User> list = new ArrayList<>();
        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("name", name);
        Iterable<User> iterable = userRepository.search(matchQueryBuilder);
        iterable.forEach(e->list.add(e));
        return list;
    }

    @Override
    public Page<User> pageQuery(Integer pageNo, Integer pageSize, String kw) {
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchPhraseQuery("name", kw))
                .withPageable(PageRequest.of(pageNo, pageSize))
                .build();
        return userRepository.search(searchQuery);
    }
}

Write a test class and call the interface in turn:

@RestController
public class TestController {

    @Autowired
    private UserService userService;


    @GetMapping("/testInsert")
    public void testInsert() {
        User user = new User();
        user.setuId("1");
        user.setName("zhangsna");
        user.setAge(101);
        user.setAddress("Shenzhen, Guangdong Province");
        userService.save(user);

        user.setuId("2");
        user.setName("lisi");
        user.setAge(32);
        user.setAddress("Shenzhen, Guangdong Province");
        userService.save(user);

        user.setuId("3");
        user.setName("wangwu");
        user.setAge(34);
        user.setAddress("Shenzhen, Guangdong Province");
        userService.save(user);

    }

    @GetMapping("/testDelete")
    public void testDelete() {
        User user = new User();
        user.setuId("1");
        userService.delete(user);
    }

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

    @GetMapping("/testGetByName")
    public void testGetByName() {
        List<User> list = userService.getByName("lisi");
        System.out.println(list);
    }

    @GetMapping("/testPage")
    public void testPage() {
        Page<User> page = userService.pageQuery(0, 10, "wangwu");
        System.out.println(page.getTotalPages());
        System.out.println(page.getNumber());
        System.out.println(page.getContent());
    }
}

kibana show data

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

wget https://artifacts.elastic.co/downloads/kibana/kibana-7.7.0-x86_64.rpm
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 http://10.10.10.1:5601, 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