- 1、依赖导入pom.xml
- 2、编写application.yml配置文件
- 3、创建ES对应的实体对象
- 4、编写ElasticRepository接口
- 5、编写Service层
- 6、编写ElasticsearchController
- 7、编写SpringBoot启动类
- 8、测试
注意:
简单测试了一下,SpringBoot版本是2.3.0.RELEASE才兼容elasticsearch 7.x,可以使用ES的高级客户端。有误请指正,万分感谢。
2、编写application.yml配置文件4.0.0 com.dws.elasticsearch elasticsearch-master 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.3.0.RELEASE org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-elasticsearch org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
server:
port: 8090
servlet:
context-path: /elasticsearch
# elasticsearch
spring:
elasticsearch:
rest:
uris: http://localhost:9200
3、创建ES对应的实体对象
package com.dws.elasticsearch.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
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;
@Data
@NoArgsConstructor
@Accessors(chain = true)
@document(indexName = "ems",type= "_doc",shards = 1,replicas = 0)
public class DocBean {
@Id
private Long id;
@Field(type = FieldType.Keyword)
private String firstCode;
@Field(type = FieldType.Keyword)
private String secordCode;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String content;
@Field(type = FieldType.Integer)
private Integer type;
public DocBean(Long id, String firstCode, String secordCode, String content, Integer type) {
this.id = id;
this.firstCode = firstCode;
this.secordCode = secordCode;
this.content = content;
this.type = type;
}
}
4、编写ElasticRepository接口
package com.dws.elasticsearch.mapper; import com.dws.elasticsearch.entity.DocBean; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; public interface ElasticRepository extends ElasticsearchRepository{ }
想了解ElasticsearchRepository提供了哪些基础的CRUD操作,有兴趣的可以进入ElasticsearchRepository类中查看,通过自定义方法命名约定,提供了强大的自定义操作,在我的下一篇文章中有讲解,有兴趣可以去看看。
5、编写Service层package com.dws.elasticsearch.service;
import com.dws.elasticsearch.entity.DocBean;
import org.springframework.data.domain.Page;
import java.util.Iterator;
import java.util.List;
public interface IElasticService {
void createIndex();
void deleteIndex(String index);
void save(DocBean docBean);
void isExist();
void saveAll(List list);
Iterator findAll();
DocBean findById(Long id);
Page findByContent(String content);
Page findByFirstCode(String firstCode);
Page findBySecordCode(String secordCode);
Page query(String key);
}
实现逻辑:
package com.dws.elasticsearch.service.impl;
import com.dws.elasticsearch.entity.DocBean;
import com.dws.elasticsearch.mapper.ElasticRepository;
import com.dws.elasticsearch.service.IElasticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.stereotype.Service;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
@Service
public class ElasticServiceImpl implements IElasticService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
@Autowired
private ElasticRepository elasticRepository;
@Override
public void createIndex() {
elasticsearchRestTemplate.indexOps(IndexCoordinates.of("ems")).create();
}
@Override
public void deleteIndex(String index) {
elasticsearchRestTemplate.indexOps(IndexCoordinates.of("ems")).delete();
}
@Override
public void save(DocBean docBean) {
elasticRepository.save(docBean);
}
@Override
public void isExist() {
elasticsearchRestTemplate.indexOps(IndexCoordinates.of("ems")).exists();
}
@Override
public void saveAll(List list) {
elasticRepository.saveAll(list);
}
@Override
public Iterator findAll() {
Iterable repositoryAll = elasticRepository.findAll();
return repositoryAll.iterator();
}
@Override
public DocBean findById(Long id) {
Optional optional = elasticRepository.findById(id);
DocBean docBean = null;
if (optional.isPresent()){
docBean = optional.get();
}
return docBean;
}
@Override
public Page findByContent(String content) {
return null;
}
@Override
public Page findByFirstCode(String firstCode) {
return null;
}
@Override
public Page findBySecordCode(String secordCode) {
return null;
}
@Override
public Page query(String key) {
return null;
}
}
6、编写ElasticsearchController
package com.dws.elasticsearch.controller;
import com.dws.elasticsearch.entity.DocBean;
import com.dws.elasticsearch.service.IElasticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@RestController
@RequestMapping("/es")
public class ElasticsearchController {
@Autowired
private IElasticService elasticService;
@GetMapping(value = "/add",name = "初始化索引,并添加数据")
public void add(){
elasticService.createIndex();
List list =new ArrayList<>();
list.add(new DocBean(1L,"1870975","dbywuhe","2ajsjhh",1));
list.add(new DocBean(2L,"sajhjj","ajsbjah","jbasbakbjs",1));
list.add(new DocBean(3L,"akbsabjb","ajsjnkaj","jhahsajh",1));
elasticService.saveAll(list);
}
@GetMapping(value = "/all",name = "查询所有数据")
public Iterator all(){
return elasticService.findAll();
}
@GetMapping(value = "/delete",name = "删除索引")
public String delete(){
elasticService.deleteIndex("ems");
return "delete index success!!!!";
}
@GetMapping(value = "/create",name = "创建索引")
public String create(){
elasticService.createIndex();
return "create index success!!!!";
}
}
7、编写SpringBoot启动类
package com.dws.elasticsearch;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ElasticsearchApplication {
public static void main(String[] args) {
SpringApplication.run(ElasticsearchApplication.class,args);
}
}
8、测试
浏览器分别请求:
http://localhost:8090/elasticsearch/es/add
http://localhost:8090/elasticsearch/es/all



