zoukankan      html  css  js  c++  java
  • elasticsearch RestHighLevelClient 关于document的常用操作 ---------- 新增篇

    es中的新增操作分类两大类:普通新增和批量新增

    普通新增使用IndexRequest即可

    批量新增使用BulkRequest通过循环的方式将数据进行统一装载最后执行bulk操作

    普通新增:

       //es单条操作--添加文档(记录)
      public String addEsDocument() throws IOException { //开始设置属性 Elasticsearch elasticsearch = new Elasticsearch(); elasticsearch.setUserName("张三"); elasticsearch.setAge(161); elasticsearch.setSex("男"); elasticsearch.setLastName("sss"); elasticsearch.setHeight(175); elasticsearch.setWeight(160); elasticsearch.setEducation("硕士"); elasticsearch.setTotalScore(205.36); //创建新增文档的请求对象 IndexRequest indexRequest = new IndexRequest(); //指定对那个索引进行操作 // indexRequest.index("user").id("1002");//此处不建议指定id值,可使用系统提供的全局uuid indexRequest.index("user"); //将设置好的属性值转为json格式 ObjectMapper objectMapper = new ObjectMapper(); String elasticsearchJson = objectMapper.writeValueAsString(elasticsearch); //以json格式添加文档信息 indexRequest.source(elasticsearchJson , XContentType.JSON); //像es服务发送请求 IndexResponse esAddInfo = this.client.index(indexRequest, RequestOptions.DEFAULT); System.out.println("es index _index is : " + esAddInfo.getIndex());//获取当前索引 System.out.println("es index _id is : " + esAddInfo.getId());//获取生成的ID System.out.println("es index _result is : " + esAddInfo.getResult());//获取操作结果 return "add es document complete"; }

    批量新增:

        //批量插入
        public String bulkAddEsDocument(){
    
            //获取数据源
            List<Map<String , Object>> testData = getTestData();
    
            //创建批量新增文档请求
            BulkRequest bulkRequest = new BulkRequest();
    
            //批量为属性赋值
            int size = testData.size();
            for (int i=0; i<size; i++) {
                bulkRequest.add(new IndexRequest().index("user").source(testData.get(i)));
            }
    
            //发送批量新新增请求
            try {
                BulkResponse bulk = this.client.bulk(bulkRequest, RequestOptions.DEFAULT);
                System.out.println(bulk.status());
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return "bulk add es document complete";
        }
    
    
        //批量插入的数据源
        private static List<Map<String , Object>> getTestData(){
    
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("userName" , "marray");
            map1.put("sex" , "男");
            map1.put("age" , 26);
            map1.put("lastName" , "aa");
            map1.put("height" , 180);
            map1.put("weight" , 120);
            map1.put("education" , "本科");
            map1.put("totalScore" , 269.36);
    
            HashMap<String, Object> map2 = new HashMap<>();
            map2.put("userName" , "Tom");
            map2.put("sex" , "男");
            map2.put("age" , 17);
            map2.put("lastName" , "bb");
            map2.put("height" , 190);
            map2.put("weight" , 230);
            map2.put("education" , "本科");
            map2.put("totalScore" , 278.21);
    
            HashMap<String, Object> map3 = new HashMap<>();
            map3.put("userName" , "Jerry");
            map3.put("sex" , "女");
            map3.put("age" , 19);
            map3.put("lastName" , "cc");
            map3.put("height" , 165);
            map3.put("weight" , 80);
            map3.put("education" , "小学");
            map3.put("totalScore" , 2693.02);
    
            HashMap<String, Object> map4 = new HashMap<>();
            map4.put("userName" , "Tank");
            map4.put("sex" , "女");
            map4.put("age" , 23);
            map4.put("lastName" , "dd");
            map4.put("height" , 123);
            map4.put("weight" , 11);
            map4.put("education" , "本科");
            map4.put("totalScore" , 222.22);
    
            HashMap<String, Object> map5 = new HashMap<>();
            map5.put("userName" , "张三");
            map5.put("sex" , "男");
            map5.put("age" , 43);
            map5.put("lastName" , "ee");
            map5.put("height" , 198);
            map5.put("weight" , 280);
            map5.put("education" , "高中");
            map5.put("totalScore" , 36.25);
    
            HashMap<String, Object> map6 = new HashMap<>();
            map6.put("userName" , "李四");
            map6.put("sex" , "男");
            map6.put("age" , 25);
            map6.put("lastName" , "ff");
            map6.put("height" , 169);
            map6.put("weight" , 110);
            map6.put("education" , "本科");
            map6.put("totalScore" , 85.36);
    
            List<Map<String , Object>> objects = new ArrayList<>();
            objects.add(map1);
            objects.add(map2);
            objects.add(map3);
            objects.add(map4);
            objects.add(map5);
            objects.add(map6);
    
            return objects;
        }

    特殊处理

    针对map数据类型的新增与其他新增无区别也需要依托实体类来进行操作

        public String addEsDocument() throws IOException {
    
            //开始设置属性
            Map map = new HashMap();
            map.put("语文"  , "11");
            map.put("数学" , "22");
            map.put("地理" , "33");
            map.put("生物" , "44");
            map.put("化学" , "55");
            map.put("历史" , "66");
            Person person = new Person("王五" , 21 , map);
    
            //创建新增文档的请求对象
            IndexRequest indexRequest = new IndexRequest();
    
            //指定对那个索引进行操作
    //        indexRequest.index("user").id("1002");//此处不建议指定id值,可使用系统提供的全局uuid
            indexRequest.index("map_test");
    
            //将设置好的属性值转为json格式
            ObjectMapper objectMapper = new ObjectMapper();
            String elasticsearchJson = objectMapper.writeValueAsString(person);
    
            //以json格式添加文档信息
            indexRequest.source(elasticsearchJson , XContentType.JSON);
    
            //像es服务发送请求
            IndexResponse esAddInfo = this.client.index(indexRequest, RequestOptions.DEFAULT);
    
            System.out.println("es index _index is : " + esAddInfo.getIndex());//获取当前索引
            System.out.println("es index _id is : " + esAddInfo.getId());//获取生成的ID
            System.out.println("es index _result is : " + esAddInfo.getResult());//获取操作结果
    
            return "add es document complete";
        }
  • 相关阅读:
    MySQL中如何使用布尔类型【转】
    你所不知道的Android Studio调试技巧【转】
    设计模式之工厂模式(factory pattern)【转】
    layuiadmin+tp5后台内容管理系统【转】
    PHPStorm怎么修改选中的背景颜色呢?【转】
    PHP保留两位小数的几种方法【转】
    jquery的css()函数同时设置多个css属性值
    Flutter text设置行间距【转】
    Flutter入门-布局Container、Padding、Align、Center【转】
    redis下载地址
  • 原文地址:https://www.cnblogs.com/hxjz/p/14703639.html
Copyright © 2011-2022 走看看