欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

ES 核心概念、基本操作和 SpringBoot 集成

最编程 2024-04-28 22:01:12
...
@Slf4j
@RequestMapping("/es")
@RestController
public class EsController {

@Autowired
// @Qualifier("restHighLevelClient")
private RestHighLevelClient client;

@ApiOperation("创建索引")
@PostMapping("/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);
}

@ApiOperation("获取索引")
@PostMapping("/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;
}

@ApiOperation("删除索引")
@PostMapping("/delIndex")
public AcknowledgedResponse delIndex() throws IOException {
// 1.创建索引请求
DeleteIndexRequest request = new DeleteIndexRequest("index-2");
// 2.客户端执行请求
AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
return delete;
}

@ApiOperation("添加文档")
@PostMapping("/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;
}

@ApiOperation("获取文档")
@PostMapping("/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();
}

@ApiOperation("更新文档")
@PostMapping("/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();
}

@ApiOperation("删除文档")
@PostMapping("/delDoc")
public RestStatus delDoc() throws IOException {
DeleteRequest request = new DeleteRequest("index-2", "1");
DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
return deleteResponse.status();
}

@ApiOperation("批量添加文档")
@PostMapping("/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 中文搜索原理理解

下一篇: Elasticsearch (Kibana) 基本语法