开发者学堂课程【SpringBoot 快速掌握 - 高级应用:SpringBoot 整合 Jest 操作ES】学习笔记,与课程紧密联系,让用户快速学习知识
课程地址:https://developer.aliyun.com/learning/course/613/detail/9307
SpringBoot 整合 Jest 操作 ES
内容介绍:
1.基本步骤
2.创建新工程
3.自动配置
4.测试操作
5.测试搜索
1.基本步骤
引入 spring-boot-starter-data-elasticsearch
安装 SpringData 对应版本的 ElasticSearch
application.yml 配置
Spring Boot 自动配置的
ElasticsearchRepository
ElasticsearchTemplateClient
测试 ElasticSearch
2.创建新工程
第一步创建 Group:
com.atguigu , Artifact: springboot-03-elastic
下一步选中 web 模块(版本以1.5.12为例)
NoSQL 选中 Elasticsearch
完成 Elasticsearch
3.自动配置
SpringBoot 默认使用 SpringDate ElasticSearch 模块进行操作
SpringBoot 默认支持两种技术来和 ES 交互
(1)Jest (默认不生效),需要导入(io.searchbox.client.JestClient)
(2)SpringDate ElasticSearch:
Client 节点信息需要配置 clusterNodes 和 clusterName;ElasticsearchTemplate 操作 es;
编写一个 ElasticsearchRepository 的子接口来操作 es;
4.测试操作
public, void contextLoads() {
给 Es 中索引(保存)一个文档
Article article = new Article();
article.setId(1)
article.setTitle("zhangsan");
article.setAuthor("zhangsan" )
article.setContent("Hello World");
构建一个索引功能
Indexindex=new Index.Builder(article). index(“atguigu").type("news").build();
try{
执行
jestClient.execute(index);
}catch (IOException e){
e.printStackTrace();
}
5.测试搜索
Public void search(){
查询表达式
String json =”{\n”+
“ \”query\”:{\n”+
“ \”match\”:{\n”+
“ \”content\”: \”hello\”\n +
“ }\n” +
“ }\n” +
“ }”;
构建搜索功能Search search = new Search.Builder(json). addIndex( indexName: " atguigu"). addType
执行
try {
SearchResult result = jestClient .execute(search);
System.out.println(result.get)sonString());
} catch (IOException e) {
e.printStackTrace();
}
查看输出性结果是否正确
正确情况下:
更多项目查找 GitHub 下 jest 的 documation 文档查看有关内容
如图: