ElasticSearch 实战:ElasticSearch JavaApi 文档增删改查操作

Elasticsearch 实战:Elasticsearch Java API 文档增删改查操作

使用 Elasticsearch Java API 进行文档的增删改查操作,主要包括索引文档、获取文档、更新文档、删除文档等。以下是一些常见文档操作的示例:

**1. **索引文档(Index)

import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

public class DocumentIndexExample {

    public String indexDocument(RestHighLevelClient client, String indexName, String documentId, String jsonContent) throws IOException {
        IndexRequest request = new IndexRequest(indexName)
                .id(documentId)
                .source(jsonContent, XContentType.JSON);

        IndexResponse response = client.index(request, RequestOptions.DEFAULT);

        return response.getResult().toString();  // 返回索引操作结果(created、updated 或 noop)
    }
}

**2. **获取文档(Get)

import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;

public class DocumentGetExample {

    public String getDocument(RestHighLevelClient client, String indexName, String documentId) throws IOException {
        GetRequest request = new GetRequest(indexName, documentId);

        GetResponse response = client.get(request, RequestOptions.DEFAULT);

        return response.getSourceAsString();  // 返回文档源 JSON 字符串
    }
}

**3. **更新文档(Update)

import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

public class DocumentUpdateExample {

    public String updateDocument(RestHighLevelClient client, String indexName, String documentId, String jsonPatch) throws IOException {
        UpdateRequest request = new UpdateRequest(indexName, documentId)
                .doc(jsonPatch, XContentType.JSON);

        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);

        return response.getResult().toString();  // 返回更新操作结果(updated 或noop)
    }
}

**4. **删除文档(Delete)

import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;

public class DocumentDeleteExample {

    public boolean deleteDocument(RestHighLevelClient client, String indexName, String documentId) throws IOException {
        DeleteRequest request = new DeleteRequest(indexName, documentId);

        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);

        return response.getResult().equals(DocWriteResponse.Result.DELETED);  // 返回是否成功删除文档
    }
}

以上示例展示了如何使用 Elasticsearch Java API 进行文档的索引、获取、更新和删除操作。在实际应用中,根据业务需求调用相应的方法进行文档管理。记得处理可能出现的异常,并确保客户端在使用完毕后正确关闭。对于复杂的文档更新,可以使用 scriptupsert 等高级选项。

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值