ElasticSearch学习(五)—— Java写入数据

ElasticSearch学习(五)—— Java写入数据| Id | Title | DateAdded | SourceUrl | PostType | Body | BlogId | Description | DateUpdated | IsMarkdown | EntryName | CreatedTime | IsActive | AutoDesc | AccessPermission |

| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------|
| 15062373| ElasticSearch学习(五)—— Java写入数据| 2021-07-26T17:11:00| | BlogPost|

  • 7.2.0版本
  • 获取客户端连接
复制代码
private static int port = 9200;
private static String host = "192.168.1.105";

private static RestHighLevelClient getClient() {
RestHighLevelClient client
= new RestHighLevelClient(
RestClient.builder(
new HttpHost(host, port, "http")));
return client;
}

复制代码
  • 批量添加数据
复制代码
/**
     * bulk批量添加
      */
    public static void batchAddDocuments(){
        try {
            RestHighLevelClient client = getClient();
        Map</span>&lt;String, Object&gt; jsonMap = <span style="color: #0000ff;">new</span> HashMap&lt;String, Object&gt;<span style="color: #000000;">();
        jsonMap.put(</span>"id", "01209"<span style="color: #000000;">);
        jsonMap.put(</span>"title", "紅樓夢"<span style="color: #000000;">);
        jsonMap.put(</span>"author", "曹雪芹"<span style="color: #000000;">);
        jsonMap.put(</span>"classification", "小說"<span style="color: #000000;">);
        IndexRequest request </span>= <span style="color: #0000ff;">new</span> IndexRequest("book"<span style="color: #000000;">)
                .id(</span>"2"<span style="color: #000000;">).source(jsonMap);

        jsonMap.put(</span>"id", "01210"<span style="color: #000000;">);
        jsonMap.put(</span>"title", "水滸傳"<span style="color: #000000;">);
        jsonMap.put(</span>"author", "施耐庵"<span style="color: #000000;">);
        jsonMap.put(</span>"classification", "小說"<span style="color: #000000;">);
        IndexRequest request1 </span>= <span style="color: #0000ff;">new</span> IndexRequest("book"<span style="color: #000000;">)
                .id(</span>"3"<span style="color: #000000;">).source(jsonMap);

        BulkRequest bulkRequest </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> BulkRequest();
        bulkRequest.add(request);
        bulkRequest.add(request1);

        BulkResponse bulkResponse </span>=<span style="color: #000000;"> client.bulk(bulkRequest, RequestOptions.DEFAULT);
        </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (BulkItemResponse bulkItemResponse : bulkResponse) {
            DocWriteResponse itemResponse </span>=<span style="color: #000000;"> bulkItemResponse.getResponse();
            </span><span style="color: #0000ff;">switch</span><span style="color: #000000;"> (bulkItemResponse.getOpType()) {
                </span><span style="color: #0000ff;">case</span><span style="color: #000000;"> INDEX:
                    </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                </span><span style="color: #0000ff;">case</span><span style="color: #000000;"> CREATE:
                    IndexResponse indexResponse </span>=<span style="color: #000000;"> (IndexResponse) itemResponse;
                    System.out.println(</span>"新增文档成功!"<span style="color: #000000;">);
                    </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                </span><span style="color: #0000ff;">case</span><span style="color: #000000;"> UPDATE:
                    UpdateResponse updateResponse </span>=<span style="color: #000000;"> (UpdateResponse) itemResponse;
                    System.out.println(</span>"更新文档成功!"<span style="color: #000000;">);
                    </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                </span><span style="color: #0000ff;">case</span><span style="color: #000000;"> DELETE:
                    DeleteResponse deleteResponse </span>=<span style="color: #000000;"> (DeleteResponse) itemResponse;
                    System.out.println(</span>"删除文档成功!"<span style="color: #000000;">);
                    </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                </span><span style="color: #0000ff;">default</span><span style="color: #000000;">:
            }
        }
        client.close();
    }</span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e){
        e.printStackTrace();
    }
}</span></pre>
复制代码
  • 根据索引id删除
复制代码
/**
     * 根据索引id删除
     */
    public static void deleteDocument(){
        try {
            RestHighLevelClient client = getClient();
            DeleteRequest request = new DeleteRequest(
                    "ancientbook",
                    "2607");
            DeleteResponse deleteResponse = client.delete(
                    request, RequestOptions.DEFAULT);
            System.out.println(deleteResponse.getResult());
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
复制代码
  • 根据索引id获取
复制代码
/**
     * 根据索引id获取
     */
    public static void getIndex() {
        try {
            RestHighLevelClient client = getClient();
            GetRequest getRequest = new GetRequest(
                    "ancientbook",
                    "2607");
            GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
            String index = getResponse.getIndex();
            String id = getResponse.getId();
            if (getResponse.isExists()) {
                long version = getResponse.getVersion();
                String sourceAsString = getResponse.getSourceAsString();
                Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
                byte[] sourceAsBytes = getResponse.getSourceAsBytes();
                System.out.println("index:" + index);
                System.out.println("id:" + id);
                System.out.println("version:" + version);
                System.out.println("sourceAsString:" + sourceAsString);
                System.out.println("sourceAsMap:" + sourceAsMap);
                System.out.println("sourceAsBytes:" + sourceAsBytes);
            } else {
        }
        client.close();
    } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
        e.printStackTrace();
    }
}</span></pre>
复制代码

(1条消息) ElasticSearch学习(五)—— Java写入数据_yilia_jia的博客-CSDN博客

| 648658| | 2021-07-26T17:11:00| false| | 2021-07-26T17:11:09.857| true| 7.2.0版本 获取客户端连接 private static int port = 9200; private static String host = "192.168.1.105"; private static RestHighLevelClient getClient() { RestHig| Anonymous|
posted @   RalphLauren  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示