本文主要是介绍ES常用api,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.dd.cc;
import com.alibaba.fastjson.JSON;
import com.dd.cc.pojo.User;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
@SpringBootTest
class CcApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
/**
* 创建索引 (等于mysql创建table)
*/
@Test
public void createIndex(){
//创建索引
CreateIndexRequest request = new CreateIndexRequest("rwj");
try {
//通过客户端发送请求
restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取索引
*/
@Test
public void getIndex(){
GetIndexRequest request = new GetIndexRequest("test");
try {
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除索引
*/
@Test
public void deleteIndex(){
DeleteIndexRequest test = new DeleteIndexRequest("test");
try {
restHighLevelClient.indices().delete(test,RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 从一个索引里添加数据
*/
@Test
public void getText(){
User user = new User("dddd","aaaa");
IndexRequest request = new IndexRequest("rwj");
request.id("1");
request.source(JSON.toJSONString(user), XContentType.JSON);
try {
IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(index.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 判断文档里是否包含某条数据
*/
@Test
public void isExists(){
GetRequest rwj = new GetRequest("rwj", "1");
try {
boolean exists = restHighLevelClient.exists(rwj, RequestOptions.DEFAULT);
System.out.println(exists);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取文档信息
*/
@Test
public void getDocument(){
GetRequest rwj = new GetRequest("rwj", "1");
try {
GetResponse response = restHighLevelClient.get(rwj, RequestOptions.DEFAULT);
Map<String, Object> source = response.getSource();
System.out.println(source.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 更新文档记录
*/
@Test
public void updateText(){
UpdateRequest rwj = new UpdateRequest("rwj", "1");
User user = new User("rwjnb","chaojinb");
rwj.doc(JSON.toJSON(user),XContentType.JSON);
try {
restHighLevelClient.update(rwj,RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除文档数据
*/
@Test
public void deleteText(){
DeleteRequest rwj = new DeleteRequest("rwj", "1");
try {
DeleteResponse delete = restHighLevelClient.delete(rwj, RequestOptions.DEFAULT);
System.out.println(delete.status());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 批量插入数据
*/
@Test
public void testBulkRequest(){
BulkRequest bulkRequest = new BulkRequest();
ArrayList<User> list = new ArrayList<>();
list.add(new User("hhhh1","aaaaaaa"));
list.add(new User("hhhh2","aaaaaaa"));
list.add(new User("hhhh3","aaaaaaa"));
list.add(new User("hhhh4","aaaaaaa"));
list.add(new User("hhhh5","aaaaaaa"));
list.add(new User("hhhh6","aaaaaaa"));
list.add(new User("hhhh7","aaaaaaa"));
list.add(new User("hhhh8","aaaaaaa"));
list.add(new User("hhhh9","aaaaaaa"));
bulkRequest.timeout("3s");
for (int i = 0; i < list.size(); i++) {
bulkRequest.add(
// 批量对应的api即可
new IndexRequest("rwj")
.source(JSON.toJSONString(list.get(i)),XContentType.JSON)
);
}
try {
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk.status());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 批量查询
*/
@Test
public void searchRequestTest(){
SearchRequest rwj = new SearchRequest("rwj");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //构造器模式
//匹配的查询条件
// TermQueryBuilder hhh = QueryBuilders.termQuery("hhh","aaa"); //精确查询
MatchAllQueryBuilder allQuery = QueryBuilders.matchAllQuery(); //无条件匹配查询
searchSourceBuilder.query(allQuery);
rwj.source(searchSourceBuilder);
try {
SearchResponse search = restHighLevelClient.search(rwj, RequestOptions.DEFAULT);
for (SearchHit fields : search.getHits().getHits()) {
Map<String, Object> sourceAsMap = fields.getSourceAsMap();
System.out.println(sourceAsMap.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//================================================================================================
package com.dd.cc.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class EsConfig {
/**
* 创建一个es客户端 目前使用的es、ik、kibana是7.6.1 与springboot 2.3.3.re 版本吻合
* 目前es版本支持highlevel 其他版本不支持 升级es该客户端创建方式需要重写
* @return
*/
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client =
new RestHighLevelClient(
RestClient
.builder(
new HttpHost("localhost",9200,"http")));
return client;
}
}
这篇关于ES常用api的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!