zoukankan      html  css  js  c++  java
  • Elasticsearch之更新

    public class UpdateElasticAPI {
        private static RestClient restClient;
    
        static {
            restClient=RestClient.builder(new HttpHost("localhost",9200,"http")).build();
        }
    
        /**
         * 1.创建文档
         * @throws Exception
         */
        @Test
        public void CreateDocument()throws Exception{
    
            String method = "PUT";
            String endpoint = "/update_index/test/1";
            HttpEntity entity = new NStringEntity(
                    "{
    " +
                            "    "counter" : 1,
    " +
                            "    "tags" : ["red"]
    " +
                            "}", ContentType.APPLICATION_JSON);
    
            Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    
        /**
         *2. 获取文档
         * @throws Exception
         */
        @Test
        public void getDocument()throws Exception{
            String method = "GET";
            String endpoint = "/update_index/test/1";
            Response response = restClient.performRequest(method,endpoint);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    
        /**
         * 3.更新文档,给counter字段加4
         * @throws Exception
         */
        @Test
        public void UpdateDocument()throws Exception{
    
            String method = "POST";
            String endpoint = "/update_index/test/1/_update";
            HttpEntity entity = new NStringEntity(
                    "{
    " +
                            "    "script" : {
    " +
                            "        "source": "ctx._source.counter += params.count",
    " +
                            "        "lang": "painless",
    " +
                            "        "params" : {
    " +
                            "            "count" : 4
    " +
                            "        }
    " +
                            "    }
    " +
                            "}", ContentType.APPLICATION_JSON);
    
            Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    
        /**
         * 4.部分文档更新增加字段(不存在name字段),存在则覆盖
         * @throws Exception
         */
        @Test
        public void SomeUpdateDocument()throws Exception{
    
            String method = "POST";
            String endpoint = "/update_index/test/1/_update";
            HttpEntity entity = new NStringEntity(
                    "{
    " +
                            "    "doc" : {
    " +
                            "        "user" : "kimchy"
    " +
                            "    }
    " +
                            "}", ContentType.APPLICATION_JSON);
    
            Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    
        /**
         * 5.按查询API更新
         */
    
    
    }
  • 相关阅读:
    H5页面跳到安卓APP和iosAPP
    JS location.href传参及接受参数
    获取当前日期及对应星期
    前端获取当前一周时间 数组形式
    Java基础(四) Object 数组转成 String 数组
    定时任务cron表达式详解
    jquery如何删除数组中的一个元素?
    Mybatis Mapper.xml 需要查询返回List<String>
    oracle的 listagg() WITHIN GROUP () 行转列函数的使用
    如何修改Oracle中表的字段长度?
  • 原文地址:https://www.cnblogs.com/heqiyoujing/p/11148283.html
Copyright © 2011-2022 走看看