zoukankan      html  css  js  c++  java
  • 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的更新操作

  • 相关阅读:
    在IE中下载Office2007文件时在对话框中下载文件变成ZIP文件的问题
    异常:tomcat与windows时间不同步
    centos 解决"不在 sudoers 文件中。此事将被报告"的问题
    linux device drivers
    linux device drivers
    linux device drivers
    Source Insight 空格和Tab出现乱码
    光和颜色
    RTOS Thread stack and MSP/PSP registers in ARM Cortex-M3
    Keil MDK编译出现Error: L6405E
  • 原文地址:https://www.cnblogs.com/youran-he/p/7574648.html
Copyright © 2011-2022 走看看