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的更新操作

  • 相关阅读:
    css中的背景、渐变 文本格式化和表格的常用属性
    HTML5中常见的英文单词
    matlab文件处理
    优先级队列
    编程珠玑(一)
    排序算法之希尔排序
    自己写的矩阵类Matrix
    排序算法之快速排序
    Thoughtworks公司面试题——MARS ROVERS问题
    matlab画图
  • 原文地址:https://www.cnblogs.com/youran-he/p/7574648.html
Copyright © 2011-2022 走看看