栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Elasticsearch 文档数据保存修改

Elasticsearch 文档数据保存修改

直接存储JSON类数据

    public boolean save(JSonObject object, String index, String id) {
        boolean result;
        Assert.notNull(index, INDEX_NULL);
        IndexRequest request = new IndexRequest(index).source(object);
        if(isNotEmpty(id)){
            request.id(id);
        }
        try {
            client.index(request, RequestOptions.DEFAULT);
            result = true;
        } catch (IOException e) {
            log.error("ES保存JSON数据失败:", e);
            result = false;
        }
        return result;
    }

    
    public boolean bulk(JSonArray list, String index, String routing) {
        boolean result;
        Assert.notEmpty(list, BULK_LIST_NULL);
        Assert.notNull(index, INDEX_NULL);
        BulkRequest bulkRequest = new BulkRequest();
        list.forEach(o -> {
            JSonObject obj = (JSONObject) o;
            IndexRequest indexRequest = new IndexRequest(index).source(obj);
            if(isNotEmpty(obj.getString("_id"))){
                indexRequest.id(obj.getString("_id"));
            }
            if(isNotEmpty(routing)){
                indexRequest.routing(routing);
            }
            bulkRequest.add(indexRequest);
        });
        try {
            client.bulk(bulkRequest, RequestOptions.DEFAULT);
            result = true;
        } catch (IOException e) {
            log.error("ES批量插入JSON数据失败:", e);
            result = false;
        }
        return result;
    }


    
    public boolean updateById(String id, String index, JSonObject object) {
        return updateById(id, index, object, null);
    }

    
    public boolean updateById(String id, String index, JSonObject object, String routing) {
        boolean result;
        Assert.notNull(id, PARAM_ID_NULL);
        Assert.notNull(index, INDEX_NULL);
        UpdateRequest request = new UpdateRequest(index, id).doc(object);
        if (null != routing) {
            request.routing(routing);
        }
        try {
            client.update(request, RequestOptions.DEFAULT);
            result = true;
        } catch (Exception e) {
            log.error("指定索引更新JSON失败,Exception:", e);
            result = false;
        }
        return result;
    }

}

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

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

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