栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

es updateByQuery 学习使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

es updateByQuery 学习使用

这个api 一般在生产是被禁用的,
首先创建索引

PUT /tyl_2021
{
  "mappings": {
    "properties": {
      "title":{"type": "text"},
      "name":{"type": "text"},
      "age":{"type": "integer"},
      "created":{
        "type": "date",
        "format": "strict_date_optional_time||epoch_millis"
      }
    }
  },
  "settings": {
    "index":{
      "number_of_shards":1,
      "number_of_replicas":0
    }
  }
}

put 数据

PUT /tyl_2021/_doc/1
{
  "name":"tyl666",
  "title":"tyl18",
  "age":18,
  "created":"2020-11-01"
}
PUT /tyl_2021/_doc/2
{
  "name":"tyl666",
  "title":"tyl18",
  "age":18,
  "created":"2020-11-01"
}
PUT /tyl_2021/_doc/3
{
  "name":"tyl666",
  "title":"tyl18",
  "age":18,
  "created":"2020-11-01"
}

springboot api

       *  Map map =   new HashMap<>(5);
         *         map.put("title","i love you");
         *         // 封装script中的code 数据格式:ctx._source.goodsName=params.goodsName;
         *         StringBuilder sb = new StringBuilder();
         *         map.keySet().stream().forEach(p -> {
         *             sb.append("ctx._source.").append(p).append("=params.").append(p).append(";");
         *         });
         *         UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest("tyl_*");
         *         updateByQueryRequest.setRefresh(true);
         *         // 设置查询条件
         *         updateByQueryRequest.setQuery(new TermQueryBuilder("title", "张五111"));
         *         //设置版本冲突时继续
         *         updateByQueryRequest.setConflicts("proceed");
         *         script script = new script(scriptType.INLINE, "painless", sb.toString(), map);
         *         updateByQueryRequest.setscript(script);
         *
         *         try {
         *             BulkByScrollResponse bulkByScrollResponse = esClient1.updateByQuery(updateByQueryRequest, RequestOptions.DEFAULT);
         *             log.info("查询更新索引:[{}]操作共耗时:[{}],共修改文档数:[{}]",
         *                     "indexName", bulkByScrollResponse.getTook(), bulkByScrollResponse.getUpdated());
         *         } catch (IOException e) {
         *             log.error("查询更新索引:[{}]操作出现异常:{}", "indexName", e);
         *         }

查看结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/424874.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号