elasticsearch更新操作问题

elasticsearch在更新的时候,是通过id进行管理的,我们在前台传入id操作,id如果与elasticsearch相同,则覆盖,否则新增一条记录。且elasticsearch中的插入一条记录和更新一条记录的代码是一样的,如下:

    public boolean updateIndex(String indexName, String id, Map map)
            throws Exception {
        // TODO Auto-generated method stub
        boolean flag = false;
        JestHttpClient jestHttpClient = Connection.getClient();
        Bulk.Builder bulk = new Bulk.Builder().defaultIndex(indexName)
                .defaultType(indexName);
        String location = map.get("latitude")+","+map.get("longitude");
        map.put("location", location);
        Index index = new Index.Builder(map).id((String) map.get("id")).build();
            bulk.addAction(index);
        BulkResult br = jestHttpClient.execute(bulk.build());
        flag=br.isSucceeded();
        return flag;
    }
    public static  void main (String[] args){
        ElasticsearchServiceImpl es = new ElasticsearchServiceImpl();
        Map map =new HashMap<String, String>();
        map.put("id", "001");
        map.put("latitude", "32");
        map.put("longitude", "117");
        map.put("text", "何婷婷");
        System.out.println(map);
        try {
            es.updateIndex("omorequire", "10", map);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

测试结果如下:

 这样的话就完成了elasticsearch的更新操作

原文地址:https://www.cnblogs.com/youran-he/p/7574648.html