zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    nodejs 的序列化与反序列化
    Visual Studio 监视与快速监视即时窗口没有智能提示
    mysql 备份数据语句
    mysql 导入sql 2006
    MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
    怎么查看mysql的安装目录
    【支付宝】退款接口 报 “缺少签名参数”
    【支付宝】"验签出错,sign值与sign_type参数指定的签名类型不一致:sign_type参数值为RSA,您实际用的签名类型可能是RSA2"
    【支付宝】支付 系统繁忙,请稍后再试(ALIN10146)
    php插入日志到数据库,对象转json
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/15216663.html
Copyright © 2011-2022 走看看