# SpringBoot整合Elasticsearch
程序员文章站
2022-05-02 11:41:07
...
SpringBoot整合Elasticsearch
1.SpringBoot项目引入最新的依赖:最简单的方法,其他依赖引入方式会导致jar包冲突
<!--引入ES依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2.配置ElasticSearch的信息Appllvation.yml如下:可以不配置
#ElasticSearch配置
#配置Es的节点
es:
host: 127.0.0.1
port: 9200
3.目前Es中有索引index1,该索引包含的字段有name,age
{"took":3,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"index1","_type":"user1","_id":"1","_score":1.0,"_source":{
"name":"test",
"age":23
}}]}}
4.在项目中创建实体类,并且通过注解来声明字段的映射属性。
@Data
@NoArgsConstructor
@Accessors(chain = true)
@Document(indexName = "index1",type = "user1", shards = 5, replicas = 1)
public class DocBean {
@Id
private int id;
@Field(type = FieldType.Keyword)
private String firstCode;
@Field(type = FieldType.Keyword)
private String name;
@Field(type = FieldType.Keyword)
private int age;
@Field(type = FieldType.Keyword)
private String secordCode;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String content;
public DocBean(int id, String firstCode, String name, int age, String secordCode, String content) {
this.id = id;
this.firstCode = firstCode;
this.name = name;
this.age = age;
this.secordCode = secordCode;
this.content = content;
}
}
5.ElasticService如下:
void createIndex();
void deleteIndex(String index);
void save(DocBean docBean);
void saveAll(List<DocBean> list);
Iterator<DocBean> findAll();
Page<DocBean> findByContent(String content);
Page<DocBean> findByFirstCode(String firstCode);
Page<DocBean> findBySecordCode(String secordCode);
Page<DocBean> query(String key);
6.实现类如下
@Service("elasticService")
public class ElasticServiceImpl implements ElasticService{
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
@Autowired
private ElasticRepository elasticRepository;
private Pageable pageable = PageRequest.of(0,10);
@Override
public void createIndex() {
}
@Override
public void deleteIndex(String index) {
}
@Override
public void save(DocBean docBean) {
elasticRepository.save(docBean);
}
@Override
public void saveAll(List<DocBean> list) {
elasticRepository.saveAll(list);
}
@Override
public Iterator<DocBean> findAll() {
return elasticRepository.findAll().iterator();
}
@Override
public Page<DocBean> findByContent(String content) {
return elasticRepository.findByContent(content,pageable);
}
@Override
public Page<DocBean> findByFirstCode(String firstCode) {
return elasticRepository.findByFirstCode(firstCode,pageable);
}
@Override
public Page<DocBean> findBySecordCode(String secordCode) {
return elasticRepository.findBySecordCode(secordCode,pageable);
}
@Override
public Page<DocBean> query(String key) {
return elasticRepository.findByContent(key,pageable);
}
}
7.Es测试Controller如下:
@Controller
@RequestMapping("/elastic")
@ResponseBody
public class ElasticController {
@Autowired
private ElasticService elasticService;
@RequestMapping("/add")
public String addindex(){
DocBean docBean = new DocBean(1, "张三", "张三", 20, "李四", "都是好人");
elasticService.save(docBean);
return "保存成功!";
}
@RequestMapping("/all")
@ResponseBody
public Iterator<DocBean> GetAll(){
Iterator<DocBean> all = elasticService.findAll();
return all;
}
@RequestMapping("/createindex")
public String createIndex(){
try{
elasticService.createIndex();
List<DocBean> list= new ArrayList<>();
list.add(new DocBean(1, "张三", "张三", 20, "李四", "都是好人"));
list.add(new DocBean(2, "张三", "张三", 20, "李四", "都是好人"));
elasticService.saveAll(list);
return "创建成功!";
}catch (Exception e){
e.printStackTrace();
return e.getMessage();
}
}
}
上一篇: 线程池源码中retry的理解
下一篇: spring概述(二)