I am participating in the Mid-Autumn Festival Creative Submission contest, please see: Mid-Autumn Festival Creative Submission Contest for details.

A, configuration,

Pom depends on

Introducing POM dependencies, the version I referenced introduced ElasticSearch at 7.6.x.

<! -- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> < version > 2.3.9. RELEASE < / version > < / dependency >Copy the code

Configuration file:

Spring: ElasticSearch: rest: uris: http://127.0.0.1:9200Copy the code

Create test code

Entity class

import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; /** * @description: Test es entity * @author: weirx * @date: 2021/4/12 15:33 * @version: 3.0 */ @data @document (indexName = "test_es", shards = 3, replicas = 1) public class TestEsDO {@id private Long Id; @Field(type = FieldType.Text, analyzer = "ik_max_word") private String name; @Field(type = FieldType.Text, analyzer = "ik_max_word") private String desc; }Copy the code

mapper

import com.cloud.bssp.user.entity.TestEsDO; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; /** * @description: Test es persistence layer * @author: weirx * @date: 2021/4/12 15:35 * @version: 3.0 */ @repository Public interface extends ElasticsearchRepository<TestEsDO, Long> {}Copy the code

service

import com.cloud.bssp.user.entity.TestEsDO; import java.util.List; Service ** @author: weirx * @time: 2021/4/12 16:28 */ public interface TestEsService { List<TestEsDO> getAll(); TestEsDO getById(Long id); TestEsDO save(TestEsDO testEsDO); void delete(Long id); Page<TestEsDO> findByPageable(); }Copy the code

serviceImpl

import java.util.List; /** * Description: * Create Date: 2021-03-24T14:09:18.513 * Modified By: <br> * Modified Date: <br> * What is Modified: <br> * * @author weirx * @version 1.0 */ @service public class TestEsServiceImpl implements TestEsService {@autoWired private TestEsMapper testEsMapper; @Override public List<TestEsDO> getAll() { Iterable<TestEsDO> all = testEsMapper.findAll(); List<TestEsDO> testEsDOS = IterUtil.toList(all); return testEsDOS; } @Override public TestEsDO getById(Long id) { return testEsMapper.findById(id).get(); } @Override public TestEsDO save(TestEsDO testEsDO) { return testEsMapper.save(testEsDO); } @Override public void delete(Long id) { testEsMapper.deleteById(id); } @override public Page<TestEsDO> eable() {Override public Page<TestEsDO> findByPageable() { Sort = sort.by (sort.direction.desc, "id"); Int currentPage = 0; int currentPage = 0; Int pageSize = 5; PageRequest = PageRequest. Of (currentPage, pageSize, sort); TermQueryBuilder = QueryBuilders. TermQuery ("name", "elasticSearch "); / / paging query Page < TestEsDO > Page = testEsMapper. Search (termQueryBuilder, pageRequest); return page; }}Copy the code

controller

import com.cloud.bssp.user.dto.UserDTO; import com.cloud.bssp.user.entity.TestEsDO; import com.cloud.bssp.user.service.TestEsService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * description: es Test controller ** @author: weirx * @time: 2021/4/12 16:29 */ @Slf4j @RestController @RequestMapping("/es") public class TestEsController { @Autowired private ElasticsearchRestTemplate elasticsearchRestTemplate; @Autowired private TestEsService testEsService; @RequestMapping("/getAll") public List<TestEsDO> getAll() { return testEsService.getAll(); } @RequestMapping("/getById") public TestEsDO getById(Long id) { return testEsService.getById(id); } @RequestMapping("/save") public TestEsDO save() { TestEsDO testEsDO = new TestEsDO(); testEsDO.setId(1000L); testEsDO.setName("elasticsearch"); Testesdo.setdesc (" Hello ElasticSearch, hello elasticSearch "); return testEsService.save(testEsDO); } @RequestMapping("/delete") public void delete(Long id) { testEsService.delete(id); } @RequestMapping("/createIndex") public void createIndex() { elasticsearchRestTemplate.createIndex(TestEsDO1.class); elasticsearchRestTemplate.putMapping(TestEsDO1.class); } @RequestMapping("/page") public Page<TestEsDO> page() { return testEsService.findByPageable(); }}Copy the code

Three, test,

Start the project

Note The following two logs show that the index is created and the index is set to mapping. The index name is the name configured on the entity class.

2021-04-12 16:34:02.949 WARN 36752 -- [/O dispatcher 1] O.E.C. Rclient: Request [PUT] http://127.0.0.1:9200/test_es?master_timeout=30s&include_type_name=true&timeout=30s returned 1 warnings: [299 Elasticsearch - 7.9.2 - d34da0ea4a966c4e49417f2da2f244e3e97b4e6e "[types removal] Using include_type_name in the create Index requests are deprecated. The parameter will be removed in The next major version."] 2021-04-12 16:34:03.031 WARN 36752 --- [/O dispatcher 1] o.e.c.RestClient : Request [PUT] http://127.0.0.1:9200/test_es/_mapping/testesdo?master_timeout=30s&include_type_name=true&timeout=30s returned 1 warnings: [299 Elasticsearch - 7.9.2 - d34da0ea4a966c4e49417f2da2f244e3e97b4e6e "[types removal] Using include_type_name put in mapping requests is deprecated. The parameter will be removed in the next major version."]Copy the code

Use restTemplate to create an index and create the Mapping.

Copy an entity class:

package com.cloud.bssp.user.entity; import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; /** * @description: Test es entity * @author: weirx * @date: 2021/4/12 15:33 * @version: 3.0 */ @data @document (indexName = "test_es_1", shards = 3, replicas = 1) public class TestEsDO1 {@id private Long Id; @Field(type = FieldType.Text, analyzer = "ik_max_word") private String name; @Field(type = FieldType.Text, analyzer = "ik_max_word") private String desc; }Copy the code

Call http://localhost:8088/es/createIndex, view kibana information.

The new data

Call http://localhost:8088/es/save, view the kibana results:

Obtain by ID

Called http://localhost:8088/es/getById? If id=1000, return the following result:

Access to all

Call http://localhost:8088/es/getAll, return results:

Deleting Document Information

Called http://localhost:8088/es/delete? Id =1000;

Paging condition query

Call http://localhost:8088/es/page, view the results: