• ES入门 (12)Java API 操作(3)DML 新增文档/修改文档/查询文档/删除文档/批量操作


    1 新增文档

    创建数据模型
    package com.atguigu.es.test;
    
    public class User {
        private String name;
        private String sex;
        private Integer age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    }
    创建数据,添加到文档中
    package com.atguigu.es.test;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.apache.http.HttpHost;
    import org.elasticsearch.action.index.IndexRequest;
    import org.elasticsearch.action.index.IndexResponse;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.client.indices.GetIndexRequest;
    import org.elasticsearch.client.indices.GetIndexResponse;
    import org.elasticsearch.common.xcontent.XContentType;
    
    public class ESTest_Doc_Insert {
        public static void main(String[] args) throws Exception {
    
            RestHighLevelClient esClient = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http"))
            );
    
            // 插入数据
            IndexRequest request = new IndexRequest();
            request.index("user").id("1001");
    
            User user = new User();
            user.setName("zhangsan");
            user.setAge(30);
            user.setSex("男");
    
            // 向ES插入数据,必须将数据转换位JSON格式
            ObjectMapper mapper = new ObjectMapper();
            String userJson = mapper.writeValueAsString(user);
            request.source(userJson, XContentType.JSON);
    
            IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);
    
            System.out.println(response.getResult());
    
            esClient.close();
        }
    }
    操作结果:

    2 修改文档

    package com.atguigu.es.test;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.apache.http.HttpHost;
    import org.elasticsearch.action.index.IndexRequest;
    import org.elasticsearch.action.index.IndexResponse;
    import org.elasticsearch.action.update.UpdateRequest;
    import org.elasticsearch.action.update.UpdateResponse;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.common.xcontent.XContentType;
    
    public class ESTest_Doc_Update {
        public static void main(String[] args) throws Exception {
    
            RestHighLevelClient esClient = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http"))
            );
    
            // 修改数据
            UpdateRequest request = new UpdateRequest();
            request.index("user").id("1001");
            request.doc(XContentType.JSON, "sex", "女");
    
            UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);
    
            System.out.println(response.getResult());
    
            esClient.close();
        }
    }

    3 查询文档

    package com.atguigu.es.test;
    
    import org.apache.http.HttpHost;
    import org.elasticsearch.action.get.GetRequest;
    import org.elasticsearch.action.get.GetResponse;
    import org.elasticsearch.action.update.UpdateRequest;
    import org.elasticsearch.action.update.UpdateResponse;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.common.xcontent.XContentType;
    
    public class ESTest_Doc_Get {
        public static void main(String[] args) throws Exception {
    
            RestHighLevelClient esClient = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http"))
            );
    
            // 查询数据
            GetRequest request = new GetRequest();
            request.index("user").id("1001");
            GetResponse response = esClient.get(request, RequestOptions.DEFAULT);
    
            System.out.println(response.getSourceAsString());
    
            esClient.close();
        }
    }

     4 删除文档

    package com.atguigu.es.test;
    
    import org.apache.http.HttpHost;
    import org.elasticsearch.action.delete.DeleteRequest;
    import org.elasticsearch.action.delete.DeleteResponse;
    import org.elasticsearch.action.get.GetRequest;
    import org.elasticsearch.action.get.GetResponse;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestHighLevelClient;
    
    public class ESTest_Doc_Delete {
        public static void main(String[] args) throws Exception {
    
            RestHighLevelClient esClient = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http"))
            );
    
    
            DeleteRequest request = new DeleteRequest();
            request.index("user").id("1001");
    
            DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);
            System.out.println(response.toString());
    
            esClient.close();
        }
    }

    5 批量操作

    批量新增:
    package com.atguigu.es.test;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.apache.http.HttpHost;
    import org.elasticsearch.action.bulk.BulkRequest;
    import org.elasticsearch.action.bulk.BulkResponse;
    import org.elasticsearch.action.index.IndexRequest;
    import org.elasticsearch.action.index.IndexResponse;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.common.xcontent.XContentType;
    
    public class ESTest_Doc_Insert_Batch {
        public static void main(String[] args) throws Exception {
    
            RestHighLevelClient esClient = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http"))
            );
    
            // 批量插入数据
            BulkRequest request = new BulkRequest();
    
    //        request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "zhangsan", "age",30,"sex","男"));
    //        request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "lisi", "age",30,"sex","女"));
    //        request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "wangwu", "age",40,"sex","男"));
    //        request.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON, "name", "wangwu1", "age",40,"sex","女"));
    //        request.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON, "name", "wangwu2", "age",50,"sex","男"));
    //        request.add(new IndexRequest().index("user").id("1006").source(XContentType.JSON, "name", "wangwu3", "age",50,"sex","男"));
            //request.add(new IndexRequest().index("user").id("1007").source(XContentType.JSON, "name", "wangwu44", "age",60,"sex","男"));
            //request.add(new IndexRequest().index("user").id("1008").source(XContentType.JSON, "name", "wangwu555", "age",60,"sex","男"));
            request.add(new IndexRequest().index("user").id("1009").source(XContentType.JSON, "name", "wangwu66666", "age",60,"sex","男"));
    
            BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
            System.out.println(response.getTook());
            System.out.println(response.getItems());
    
            esClient.close();
        }
    }

    批量删除:
    package com.atguigu.es.test;
    
    import org.apache.http.HttpHost;
    import org.elasticsearch.action.bulk.BulkRequest;
    import org.elasticsearch.action.bulk.BulkResponse;
    import org.elasticsearch.action.delete.DeleteRequest;
    import org.elasticsearch.action.index.IndexRequest;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.common.xcontent.XContentType;
    
    public class ESTest_Doc_Delete_Batch {
        public static void main(String[] args) throws Exception {
    
            RestHighLevelClient esClient = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http"))
            );
    
            // 批量删除数据
            BulkRequest request = new BulkRequest();
    
            request.add(new DeleteRequest().index("user").id("1001"));
            request.add(new DeleteRequest().index("user").id("1002"));
            request.add(new DeleteRequest().index("user").id("1003"));
    
            BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
            System.out.println(response.getTook());
            System.out.println(response.getItems());
    
            esClient.close();
        }
    }
     
     
     

    本文来自博客园,作者:秋华,转载请注明原文链接:https://www.cnblogs.com/qiu-hua/p/15216663.html

  • 相关阅读:
    DOM——《JavaScript高级程序设计》笔记
    (转)用js无法获取style样式的问题解析与解决方法
    【2】可视化格式模型、背景、链接、表格表单——《精通CSS‘》
    安装sql server 2008 R2,提示错误:此计算机上安装了 Microsoft Visual Studio 2008 的早期版本。请在安装 SQL Server 2008 前将 Microsoft Visual Studio 2008 升级到 SP1。
    C#读取Word表格中的数据 (转)
    C#在Word文档指定位置处理表格
    c#操作word书签
    c# 操作word的
    函数参数压栈顺序2
    可变长参数
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/15216663.html
Copyright © 2020-2023  润新知