There are ups and downs in life. There are days when the scenery is infinite, and there are days when the soul is down and out. When people are at their lowest point, they can only “survive and win”.


preface

Spring Data for Elasticsearch is part of the Spring Data project, which provides integration for Elasticsearch operations. Use to interact with Elastichsearch documents and easily write access layer code.


01
Integrate Spring Data Elasticsearch

Create a new SpringBoot project and check Spring Data Elasticsearch as shown:

After creation, open the POM and have a look, as shown below:

Wait for Maven to pull down all dependencies, then open application.properties and add the following configurations:

1, spring. Data. Elasticsearch. Repositories. Enabled = true (open the elasticsearch warehouse, the default true)

2, spring. Elasticsearch. Rest. Uris, = elasticsearch IP: port or elasticsearch domain (connection elasticsearch address)

3, spring. Data. Elasticsearch. Client. Reactive. Connection timeout = 3000 (connection elasticsearch timeout)

Four, spring. Data. Elasticsearch. Client. Reactive. The socket – timeout = 3000 (socket timeout)

As follows:

spring.application.name=elasticSearchDemo

server.port=8080

Open the ES repository

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 core

1, @document annotation

Properties:

1. IndexName indicates the indexName

RefreshInterval Interval for refreshing indexes

3. IndexStoreType Index storage type. Niofs is generally used

4. The number of Shards fragments is generally equal to the number of ElasticSearch nodes

5, replicas, generally equal to Ards – 1

2, @ID annotation attribute level annotation, annotation variables map to the Id field of document in index

ElasticsearchRepository is an extension of the ElasticsearchRepository class

4, ElasticsearchRestTemplate action class, used to the operation of the elasticsearch


03
Implementation operation ElasticSearch add delete change check

Create a DemoDO class that maps index from ElasticSearch

1. Add a comment to the class:

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

2. Add id, name, age, nickName, device, lastLoginDate

3. Add @id annotation to attribute ID

4. Add get/set and toString

As shown in figure:

2. Create a DemoRepository interface to implement jPA operations

1. Inherit ElasticsearchRepository

ElasticsearchRepository (DemoDO, String, indexName); Id: String (default elasticSearch ID: String)

As follows:

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> {

}

After these two steps, the preparation is complete.

Next we use DemoRepository to manipulate ElasticSearch:

1. Create a unit test class

@SpringBootTest

class ElasticSearchDemoApplicationTests

Inject DemoRepository

@Autowired

private DemoRepository repository;

3. Add the save method

@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);

}

DemoDO set the id of the 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. Add search methods

@Test

public void query() {

Iterable<DemoDO> demoDOIterator = repository.findAll();

}

5. Add deletion methods

@Test

public void deleteAll() {

repository.deleteAll();

}


Total knot

There are a lot of methods that you can use in jPA, and you can use them in jPA. You can use them in JPA, and you can use them in JPA.Thank you for your support. Thank you.