- SpringBoot版本和ES版本对应
使用ES前,看好自己SpringBoot版本和ES版本
(我这是使用的springboot版本是:2.3.7.RELEASE,ES版本为:7.2.0) 2. pom文件引用依赖
org.springframework.boot spring-boot-starter-data-elasticsearch org.springframework.data spring-data-elasticsearch
- application.yaml配置
我这里是本地安装的ES,具体使用注意修改地址
spring:
elasticsearch:
rest:
uris: http://127.0.0.1:9200 #多个地址用逗号分隔
- 创建对应的实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
@document(indexName = "user",shards = 3)
public class User {
@Id
private String id;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String name;
@Field(type = FieldType.Keyword)
private String sex;
@Field(type = FieldType.Integer)
private Integer age;
@Field(type = FieldType.Keyword, index = false)
private String grade;
}
- 基础的增删改查
(1)创建对应的索引
@Autowired
ElasticsearchRestTemplate restTemplate;
@GetMapping("/index")
public String indexExists() {
//创建索引
boolean user = restTemplate.createIndex(User.class, true);
System.out.println(user + "-------------");
return "success";
}
(2)添加数据
@GetMapping("/save")
public String save() {
User esEntity = new User("2", "李四", "男", 18, "1");
// 不要拿到自增的返回值
// UserEsEntity save = restTemplate.save(esEntity);
// String id = save.getId();
// System.out.println(save+"新加入的"+id);
// 拿到自增的返回值
IndexQuery indexQuery = new IndexQueryBuilder()
.withId(esEntity.getId())
.withObject(esEntity)
.build();
String id = restTemplate.index(indexQuery, IndexCoordinates.of("user"));
System.out.println(id+"新加入的"+id);
// 批量添加
// List list = new ArrayList<>();
// list.add(esEntity);
// list.add(esEntity);
// elasticsearchRestTemplate.save(list);
return "success";
}
(3)通过ID删除对应的数据
@GetMapping("/delete")
public String delete(User user) {
String id = restTemplate.delete(user.getId());
System.out.println(id+"删除的数据id");
return "success";
}
(4) 查询数据
分词检索记得按照分词器
@GetMapping("/get")
public List get(User user) {
List list = new ArrayList<>();
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
// queryBuilder.must(QueryBuilders.termQuery("age", 18)); //精确搜索 年龄是18岁的
// queryBuilder.must(QueryBuilders.termQuery("grade", 1)); //年级为1的
queryBuilder.must(QueryBuilders.matchQuery("name", "李六"));//分词检索
// 构建分页,page 从0开始
// Pageable pageable = PageRequest.of(0, 3);
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
NativeSearchQuery build = nativeSearchQueryBuilder
.withQuery(queryBuilder)
// .withPageable(pageRequest)
.build();
SearchHits searchHits = restTemplate.search(build, User.class);
List> hits = searchHits.getSearchHits();
for (SearchHit hit : hits) {
User content = hit.getContent();
list.add(content);
}
return list;
}
(5) 修改数据
@GetMapping("/update")
public List update(List ids){
for (String o : ids) {
// 根据ID 修改某个字段
document document = document.create();
document.putIfAbsent("name", "李思思"); //更新后的内容
UpdateQuery updateQuery = UpdateQuery.builder(o)
.withdocument(document)
.withDocAsUpsert(true) //不加默认false。true表示更新时不存在就插入
.build();
UpdateResponse response = restTemplate.update(updateQuery, IndexCoordinates.of("user"));
System.out.println(response.getResult().toString());//UPDATED 表示更新成功
}
return "success";
}



