ElasticSearch 实战:Java操作ElasticSearch索引之CRUD

在Elasticsearch中,使用Java操作索引(Index)的CRUD(创建、读取、更新、删除)操作,可以通过Elasticsearch的Java API来进行。以下是一个基于Elasticsearch官方High Level REST Client的简单示例,展示如何进行CRUD操作:

1. 添加依赖

在Maven项目中,需要在pom.xml文件中添加Elasticsearch Java High Level REST Client的依赖:

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <!-- 注意替换为对应的Elasticsearch版本 -->
    <version>7.17.0</version>
</dependency>
<!-- 如果还没有包含transport和core客户端 -->
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <!-- 注意替换为对应的Elasticsearch版本 -->
    <version>7.17.0</version>
</dependency>
2. 创建客户端

创建一个Elasticsearch客户端实例:

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.action.delete.DeleteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

// ...

RestHighLevelClient client = new RestHighLevelClient(
    RestClient.builder(
        new HttpHost("localhost", 9200, "http"),
        new HttpHost("localhost", 9201, "http") // 如果有多个节点
    )
);

// 使用完后记得关闭客户端
try {
    // ... CRUD操作
} finally {
    client.close();
}
3. CRUD操作示例
  • 创建(Create):插入或更新文档到索引
public IndexResponse createDocument(RestHighLevelClient client, String indexName, String id, Map<String, Object> document) throws IOException {
    IndexRequest request = new IndexRequest(indexName)
        .id(id)
        .source(document, XContentType.JSON);
    
    return client.index(request, RequestOptions.DEFAULT);
}

// 使用示例
Map<String, Object> doc = Map.of("title", "Example Document", "content", "This is an example document.");
IndexResponse response = createDocument(client, "my_index", "doc_id", doc);
  • 读取(Read):查询单个文档或执行搜索
public GetResponse getDocument(RestHighLevelClient client, String indexName, String id) throws IOException {
    GetRequest getRequest = new GetRequest(indexName, id);
    
    return client.get(getRequest, RequestOptions.DEFAULT);
}

// 使用示例
GetResponse getResp = getDocument(client, "my_index", "doc_id");
if (getResp.isExists()) {
    Map<String, Object> sourceAsMap = getResp.getSourceAsMap();
    // ...
}

// 搜索操作
public SearchResponse searchDocuments(RestHighLevelClient client, String indexName) throws IOException {
    SearchRequest searchRequest = new SearchRequest(indexName);
    // 可以添加匹配条件、排序等复杂搜索构造

    return client.search(searchRequest, RequestOptions.DEFAULT);
}

// 使用示例
SearchResponse searchResp = searchDocuments(client, "my_index");
searchResp.getHits().forEach(hit -> {
    Map<String, Object> source = hit.getSourceAsMap();
    // ...
});
  • 更新(Update):修改已有文档的部分或全部内容
public UpdateResponse updateDocument(RestHighLevelClient client, String indexName, String id, Map<String, Object> partialUpdate) throws IOException {
    UpdateRequest request = new UpdateRequest(indexName, id)
        .doc(partialUpdate, XContentType.JSON); // 或者使用script进行更复杂的更新
    
    return client.update(request, RequestOptions.DEFAULT);
}

// 使用示例
Map<String, Object> updateFields = Map.of("status", "updated");
UpdateResponse updateResp = updateDocument(client, "my_index", "doc_id", updateFields);
  • 删除(Delete):从索引中删除文档
public DeleteResponse deleteDocument(RestHighLevelClient client, String indexName, String id) throws IOException {
    DeleteRequest request = new DeleteRequest(indexName, id);
    
    return client.delete(request, RequestOptions.DEFAULT);
}

// 使用示例
DeleteResponse deleteResp = deleteDocument(client, "my_index", "doc_id");

请注意,以上代码片段仅演示了基础的CRUD操作,实际使用时可能需要处理更多的异常、响应结果解析以及其他高级特性。同时,示例中的API调用适用于Elasticsearch 7.x版本,不同版本之间API可能会有所差异,请根据实际使用的Elasticsearch版本查阅对应文档。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值