[Elasticsearch from entry to the actual scene] spring boot operation elasticsearch integrated spring data elasticsearch

9150e4e5ly1flvtmb43t9g208c08caai.gif

The ups and downs of life, when there is fame day, Lost Souls have come down, people in the valley, only "survive, will win."


Foreword

Elasticsearch of Spring Data is part of the Spring Data project, the Spring Data project provides integrated Elasticsearch of Elasticsearch operations for Elastichsearch documents and interact easily write access layer code.


01
整合Spring Data Elasticsearch

Springboot create a new project, and check the Spring Data Elasticsearch, as shown:

image.png

Once created open pom look, as shown:

image.png

Wait maven down all the dependencies, and then open application.properties, add the following configuration:

1, spring.data.elasticsearch.repositories.enabled = true (open elasticsearch warehouse, default true)

2, spring.elasticsearch.rest.uris = elasticsearch the ip: port or elasticsearch domain (elasticsearch connection address)

3, spring.data.elasticsearch.client.reactive.connection-timeout = 3000 (elasticsearch connection timeout)

4, spring.data.elasticsearch.client.reactive.socket-timeout = 3000 (socket timeout time)

as follows:

spring.application.name=elasticSearchDemo
server.port=8080
# 开启es仓库
spring.data.elasticsearch.repositories.enabled=true
spring.elasticsearch.rest.uris=http://127.0.0.1:9200
spring.data.elasticsearch.client.reactive.connection-timeout=3000
spring.data.elasticsearch.client.reactive.socket-timeout=3000


02
Spring Data Elasticsearch核心

1, @ Document annotation

    Attributes:

        1, indexName Index Name

        2, refreshInterval index refresh interval

        3, indexStoreType index storage type generally used niofs

        4, shards fractal sheet, generally equal to the number of nodes elasticsearch

        5, replicas number of copies, generally equal shards - 1

2, @ Id annotation property level annotation, tagging variable is mapped to the id field in the document's index

3, ElasticsearchRepository inherited class, to achieve the specified index operation jpa

4、ElasticsearchRestTemplate 操作类,用于对elasticsearch的操作


03
实现操作elasticsearch的增删改查

1、创建一个DemoDO类,用于映射elasticsearch中的index

    1、类上添加注解:

    @Document(indexName = "demo", refreshInterval = "30s", indexStoreType = "niofs", shards = 1, replicas = 0)

    2、添加属性:id、name、age、nickName、device、lastLoginDate

    3、在属性id上添加注解:@Id

    4、添加get/set和toString

如图:

image.png

2、创建一个DemoRepository接口,用于实现jpa操作

    1、继承ElasticsearchRepository

    2、ElasticsearchRepository第一个类型为DemoDO,第二个类型为String,代表该接口用于操作DemoDO的indexName所写的索引,id为String类型(elasticsearch的id默认为string类型)

如下:

package com.elasticsearch.demo.repository;

import
com.elasticsearch.demo.dataobject.DemoDO;
import
org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface
DemoRepository extends ElasticsearchRepository<DemoDO, String> {
}

完成这两步以后,就已经完成准备工作了。

接下来我们利用DemoRepository来操作elasticsearch:

1、创建单元测试类

@SpringBootTest
class ElasticSearchDemoApplicationTests

2、注入DemoRepository

@Autowired
private DemoRepository repository;

3、新增save方法

@Test
public void save() {
DemoDO demoDO = new DemoDO();
   
demoDO.setDevice("phone");
   
demoDO.setAge((int) (Math.random() * 10 + 1));
   
demoDO.setName(UUID.randomUUID().toString());
   
demoDO.setNickName(UUID.randomUUID().toString());
   
demoDO.setLastLoginDate(new Date());
   
repository.save(demoDO);
}

3、新增修改方法(其实就是demoDO设置了id的值,就是为修改)

@Test
public void update() {
DemoDO demoDO = new DemoDO();
   demoDO.setId("");
   demoDO.setDevice("phone");
   demoDO.setAge((int) (Math.random() * 10 + 1));
   demoDO.setName(UUID.randomUUID().toString());
   demoDO.setNickName(UUID.randomUUID().toString());
   demoDO.setLastLoginDate(new Date());
   repository.save(demoDO);
}

4、新增查找方法

@Test
public void query() {
Iterable<DemoDO> demoDOIterator = repository.findAll();
}

5, the new method to delete

@Test
public void deleteAll() {
repository.deleteAll();
}


to sum up

Currently information elasticsearch operation is still relatively small, and I hope my article tips can help you solve problems and daily work, jpa there are many ways to use, easy to understand method name, you can see, as the aggregate query the next issue will continue to explain, thank you for your support, thank you.


Small hand flick, thumbs up there


Guess you like

Origin juejin.im/post/5e8ed5abe51d4546be39a3dd