一云头、配置
pom依賴
引入pom依賴,我引用的版本中引入的elasticsearch版本是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>
配置文件:
spring:
elasticsearch:
rest:
uris: http://127.0.0.1:9200
二趾痘、創(chuàng)建測(cè)試代碼
實(shí)體類
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: 測(cè)試es實(shí)體
* @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;
}
mapper
import com.cloud.bssp.user.entity.TestEsDO;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
/**
* @description: 測(cè)試es持久層
* @author:weirx
* @date:2021/4/12 15:35
* @version:3.0
*/
@Repository
public interface TestEsMapper extends ElasticsearchRepository<TestEsDO, Long> {
}
service
import com.cloud.bssp.user.entity.TestEsDO;
import java.util.List;
/**
* description: es測(cè)試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();
}
serviceImpl
import java.util.List;
/**
* Description:
* Create Date: 2021-03-24T14:09:18.513
* Modified By:<br>
* Modified Date:<br>
* Why & 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> findByPageable() {
//設(shè)置排序(排序方式,正序還是倒序榛斯,排序的 id)
Sort sort = Sort.by(Sort.Direction.DESC, "id");
//當(dāng)前頁观游,第一頁從 0 開始,1 表示第二頁
int currentPage = 0;
//每頁顯示多少條
int pageSize = 5;
//設(shè)置查詢分頁
PageRequest pageRequest = PageRequest.of(currentPage, pageSize, sort);
//查詢條件
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "elasticsearch");
//分頁查詢
Page<TestEsDO> page = testEsMapper.search(termQueryBuilder,pageRequest);
return page;
}
}
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測(cè)試控制器
*
* @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, 你好驮俗,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();
}
}
三懂缕、測(cè)試
啟動(dòng)項(xiàng)目
關(guān)注以下兩條日志信息,發(fā)現(xiàn)一條創(chuàng)建索引王凑,一條是設(shè)置mapping的搪柑,索引名稱是在實(shí)體類上配置的名稱聋丝。
2021-04-12 16:34:02.949 WARN 36752 --- [/O dispatcher 1] o.e.c.RestClient : 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 create index requests is 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 in put mapping requests is deprecated. The parameter will be removed in the next major version."]
使用restTemplate創(chuàng)建一條索引,并創(chuàng)建mapping工碾。
復(fù)制一份實(shí)體類:
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: 測(cè)試es實(shí)體
* @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;
}
調(diào)用http://localhost:8088/es/createIndex弱睦,查看kibana信息。
kibana
新建數(shù)據(jù)
調(diào)用http://localhost:8088/es/save渊额,查看kibana結(jié)果:
kibana
根據(jù)id獲取
調(diào)用http://localhost:8088/es/getById?id=1000况木,返回結(jié)果:
結(jié)果
獲取全部
調(diào)用http://localhost:8088/es/getAll,返回結(jié)果:
結(jié)果
刪除文檔信息
調(diào)用http://localhost:8088/es/delete?id=1000,查看kibana:
kibana
分頁條件查詢
調(diào)用http://localhost:8088/es/page旬迹,查看結(jié)果:
結(jié)果