springboot检索之使用JestClient整合elasticsearch测试
原创
©著作权归作者所有:来自51CTO博客作者yOhohoh的原创作品,请联系作者获取转载授权,否则将追究法律责任
1-新建工程
2-选中web,elasticsearch模块
springboot默认使用spring data elasticsearch模块进行操作
3-
springboot默认支持两种技术来和es交互
Jest(默认不生效),需要导入jest的工具包(io.searchbox.client.JestClient)
springdata elasticsearch
client节点信息 clusterNodes,clusterName
ElasticsearchTemplate操作elasticsearch
编写一个ElasticsearchRepository的子接口来操作elasticsearch
4-来到maven中央仓库 搜索jest
选择6版本的
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>6.3.0</version>
</dependency>
修改pom.xml文件
5-编写application.properties
spring.elasticsearch.jest.uris=http://192.168.3.18:9200
6-启动,连接池连到9200端口
7-新建bean包 Article类
package com.example.springbootelasticsearch.bean;
import io.searchbox.annotations.JestId;
public class Article {
@JestId //标记主键
private Integer id;
private String author;
private String title;
private String content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
8-编写测试类
package com.example.springbootelasticsearch;
import com.example.springbootelasticsearch.bean.Article;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
class SpringbootElasticsearchApplicationTests {
@Autowired
JestClient jestClient;
@Test
void contextLoads() {
//给elasticsearch中索引(保存)一个文档
Article article = new Article();
article.setId(1);
article.setTitle("好消息");
article.setAuthor("zhangsan");
article.setContent("hello world");
//构建一个索引功能,不能大小写混着写
//Index index = new Index.Builder(article).index("testElasticSearch").type("news").id("id").build();
//Index index = new Index.Builder(article).index("testElasticSearch").type("news").build();
Index index = new Index.Builder(article).index("test").type("news").build();
try {
jestClient.execute(index);
} catch (IOException e) {
e.printStackTrace();
}
}
}
9-运行测试类
查看所有索引
查询结果
10-测试类中新增
//测试搜索
@Test
public void search(){
//查询表达式,修改属性值为article的属性content
String queryString="{\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"content\" : \"hello\"\n" +
" }\n" +
" }\n" +
"}";
//构建搜索功能
Search search = new Search.Builder(queryString).addIndex("test").addType("news").build();
//执行
try {
SearchResult result = jestClient.execute(search);
System.out.println(result.getJsonString());
} catch (IOException e) {
e.printStackTrace();
}
}
运行测试方法,打印出
2019-11-29 17:39:08.817 INFO 9940 --- [ main] .SpringbootElasticsearchApplicationTests : Started SpringbootElasticsearchApplicationTests in 2.741 seconds (JVM running for 4.002)
{"took":4,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"test","_type":"news","_id":"1","_score":0.2876821,"_source":{"id":1,"author":"zhangsan","title":"好消息","content":"hello world"}}]}}
2019-11-29 17:39:09.086 INFO 9940 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
11-查看官方文档