一、下载ES
ES:下载中心 - Elastic 中文社区
安装需要JDK,JAVA_HOMEElasticSearch
下载并解压,进入bin
目录,双击elasticsearch.bat
就能启动,访问地址localhost:9200
注:本文使用的的ES版本为7.17.0文章来源:https://www.toymoban.com/news/detail-522163.html
二、创建springboot项目,引入依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.4.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.4.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.6</version>
</dependency>
三、新建ES配置,注入RestHighLevelClient 客户端
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1",9200,"http"))
);
return client;
}
}
四、测试索引API
@Autowired
@Qualifier("restHighLevelClient")
public RestHighLevelClient client;
@Test
void testCreateIndex() throws Exception{
//创建索引请求
CreateIndexRequest request = new CreateIndexRequest("xr_index");
//客户端执行请求,获得响应
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(JSONUtil.toJsonStr(response));
}
//测试索引的是否存在
@Test
void testExistIndex() throws Exception {
GetIndexRequest request= new GetIndexRequest("xr_index");
boolean exist = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exist);
}
//删除索引
@Test
void testDeleteIndex() throws Exception {
DeleteIndexRequest request= new DeleteIndexRequest("xr_index");
AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(JSONUtil.toJsonStr(response));
}
五、测试测试文档API
//添加文档
@Test
void testAddDocument() throws Exception {
Map<String, String> hashMap = new HashMap<>();
hashMap.put("name","雪人");
hashMap.put("age","22");
//创建请求
IndexRequest request = new IndexRequest("bai_index");
//规则 put/bai_index/_doc/1
request.id("2");
request.timeout(TimeValue.timeValueSeconds(1));
//放入数据
request.source(JSONUtil.toJsonStr(hashMap), XContentType.JSON);
//发送请求
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println(JSONUtil.toJsonStr(response));
}
//测试文档是否存在
@Test
void testExitDocument() throws Exception{
GetRequest request = new GetRequest("xr_index", "2");
boolean exists = client.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
//测试获取文档
@Test
void testGetDocument() throws Exception{
GetRequest request = new GetRequest("xr_index", "2");
GetResponse response = client.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
}
//测试修改文档
@Test
void testUpdateDocument() throws Exception{
UpdateRequest request = new UpdateRequest("xr", "2");
request.timeout(TimeValue.timeValueSeconds(1));
Map<String, String> hashMap = new HashMap<>();
hashMap.put("name","独自堆雪人");
hashMap.put("age","22");
request.doc(JSONUtil.toJsonStr(hashMap),XContentType.JSON);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println(JSONUtil.toJsonStr(response));
}
//测试删除文档
@Test
void testDeleteDocument() throws Exception {
DeleteRequest request= new DeleteRequest("独自堆雪人","2");
request.timeout(TimeValue.timeValueSeconds(1));
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
System.out.println(response.status());
}
//测试添加文档
@Test
void testBulkAddDocument() throws Exception {
BulkRequest request = new BulkRequest();
request.timeout(TimeValue.timeValueSeconds(1));
List<Object> userlist=new ArrayList<>();
for (int i = 0; i < 10; i++) {
Map<String, String> hashMap = new HashMap<>();
hashMap.put("name","bai"+i);
hashMap.put("age",""+i);
userlist.add(hashMap);
}
//批量处理请求 (下面如果不设置id就会随机生成一个id)
for (int i = 0; i < userlist.size(); i++) {
request.add(
new IndexRequest("bai_index")
.id(""+(i+1))
.source(JSONUtil.toJsonStr(userlist.get(i)),XContentType.JSON)
);
}
BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.hasFailures());//返回false表示添加成功
}
//测试查询文档
@Test
void testSearchDocument() throws Exception {
SearchRequest request = new SearchRequest("bai_index");
//构建搜索条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.highlighter();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "bai1");
sourceBuilder.query(termQueryBuilder);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
System.out.println(JSONUtil.toJsonStr(response.getHits()));
System.out.println("=====================");
for (SearchHit documentFields : response.getHits().getHits()) {
System.out.println(documentFields.getSourceAsMap());
}
}
注意ES版本与引入ES客户端对应,否则会出现一些api调用兼容问题。文章来源地址https://www.toymoban.com/news/detail-522163.html
到了这里,关于Es集成Springboot的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!