es java 增删改查_java操作elasticsearch实现基本的增删改查操作
时间: 2023-07-07 20:39:44 浏览: 262
SpringBoot对Elasticsearch增删改查源码
要操作 Elasticsearch 进行增删改查操作,需要使用 Elasticsearch 的 Java 客户端库。以下是一些基本操作的示例代码:
1. 添加文档:
```
IndexRequest request = new IndexRequest("索引名称");
request.id("文档id");
request.source("字段1", "值1", "字段2", "值2", ...);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
```
2. 更新文档:
```
UpdateRequest request = new UpdateRequest("索引名称", "文档id");
request.doc("字段1", "新值1", "字段2", "新值2", ...);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
```
3. 删除文档:
```
DeleteRequest request = new DeleteRequest("索引名称", "文档id");
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
```
4. 查询文档:
```
SearchRequest request = new SearchRequest("索引名称");
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(QueryBuilders.matchQuery("字段名", "查询关键字"));
request.source(builder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
```
以上示例代码仅为演示基本操作,实际使用中还需考虑性能、错误处理等问题。
阅读全文