ES 核心概念、基本操作和 SpringBoot 集成
最编程
2024-04-28 22:01:12
...
("/es")
public class EsController {
// @Qualifier("restHighLevelClient")
private RestHighLevelClient client;
("创建索引")
("/createIndex")
public void createIndex() throws IOException {
// 1.创建索引请求
CreateIndexRequest request = new CreateIndexRequest("index-2");
// 2.客户端执行请求
CreateIndexResponse response = client
.indices()
.create(request, RequestOptions.DEFAULT);
Console.log(response);
}
("获取索引")
("/getIndex")
public boolean getIndex() throws IOException {
// 1.创建索引请求
GetIndexRequest request = new GetIndexRequest("index-2");
// 2.客户端执行请求
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT);
System.out.println("[getIndexResponse:]" + getIndexResponse);
return exists;
}
("删除索引")
("/delIndex")
public AcknowledgedResponse delIndex() throws IOException {
// 1.创建索引请求
DeleteIndexRequest request = new DeleteIndexRequest("index-2");
// 2.客户端执行请求
AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
return delete;
}
("添加文档")
("/addDoc")
public IndexResponse addDoc() throws IOException {
// 1.创建文档信息
B b = new B("鲁智深", 18);
// 2.创建请求
IndexRequest request = new IndexRequest("index-2");
request.id("1");
request.timeout("1s");
request.source(JSONUtil.toJsonStr(b), XContentType.JSON);
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
return indexResponse;
}
("获取文档")
("/getDoc")
public String getDoc() throws IOException {
// 1.创建请求
GetRequest request = new GetRequest("index-2", "1");
// 2.客户端执行请求
GetResponse documentFields = client.get(request, RequestOptions.DEFAULT);
return documentFields.getSourceAsString();
}
("更新文档")
("/updateDoc")
public RestStatus updateDoc() throws IOException {
B b = new B("鲁智深", 19);
UpdateRequest request = new UpdateRequest("index-2", "1");
request.doc(JSONUtil.toJsonStr(b), XContentType.JSON);
UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
return updateResponse.status();
}
("删除文档")
("/delDoc")
public RestStatus delDoc() throws IOException {
DeleteRequest request = new DeleteRequest("index-2", "1");
DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
return deleteResponse.status();
}
("批量添加文档")
("/batchAddDoc")
public String batchAddDoc() throws IOException {
List<B> bList = new ArrayList<B>();
bList.add(new B("林冲", 20));
bList.add(new B("武松", 21));
bList.add(new B("扈三娘", 18));
bList.add(new B("张青", 22));
bList.add(new B("孙二娘", 18));
bList.add(new B("王英", 30));
bList.add(new B("花荣", 20));
BulkRequest bulkRequest = new BulkRequest();
for (int i = 0; i < bList.size(); i++) {
bulkRequest.add(
new IndexRequest("index-2").id("" + (i + 1)).source(JSONUtil.toJsonStr(bList.get(
上一篇: 搜索优化 - ES 中文搜索原理理解