zoukankan      html  css  js  c++  java
  • Elasticsearch在Java中的增删改查

    public class ElasticAPI {
        private static RestClient restClient;
    
        static {
            restClient=RestClient.builder(new HttpHost("localhost",9200,"http")).build();
        }
    
        /***
         * Index API
         * @throws IOException
         */
        @Test
        public void IndexAPI() throws IOException {
            String method = "PUT";
            String endpoint = "twitter/_doc/1";
            HttpEntity entity = new NStringEntity(
                    "{
    " +
                            "    "user" : "kimchy",
    " +
                            "    "post_date" : "2009-11-15T14:12:12",
    " +
                            "    "message" : "trying out Elasticsearch"
    " +
                            "}", ContentType.APPLICATION_JSON);
            Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    
        /**
         * 获取
         */
        @Test
        public void GetIndexAPI() throws IOException {
            String method = "GET";
            String endpoint = "twitter/_doc/1";
            Response response = restClient.performRequest(method,endpoint);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    
        /**
         * API还允许使用以下方式检查文档是否存在 HEAD:
         * 获取
         */
        @Test
        public void HeadIndexAPI() throws IOException {
            String method = "HEAD";
            String endpoint = "twitter/_doc/1";
            Response response = restClient.performRequest(method,endpoint);
            System.out.println(response.getEntity());
        }
    
        /**
         * ××××××
         * @throws IOException
         */
        public void OperationType() throws IOException {
            String method = "PUT";
            String endpoint = "twitter/docs/3";
            HttpEntity entity = new NStringEntity(
                    "{
    " +
                            "    "user" : "ssss",
    " +
                            "    "post_date" : "2009-11-15T14:12:12",
    " +
                            "    "message" : "Elasticsearch"
    " +
                            "}", ContentType.APPLICATION_JSON);
            Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    
        /**
         * ××××××
         * 获取
         */
        @Test
        public void GetOperationType() throws IOException {
            String method = "GET";
            String endpoint ="twitter/docs/3";
            Response response = restClient.performRequest(method,endpoint);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    
    
        /**
         * POST 产生一个随机id
         * @throws IOException
         */
        @Test
        public void IndexAPIPost() throws IOException {
            String method = "POST";
            String endpoint = "twitter/_doc";
            HttpEntity entity = new NStringEntity(
                    "{
    " +
                            "    "user" : "kimchy",
    " +
                            "    "post_date" : "2009-11-15T14:12:12",
    " +
                            "    "message" : "trying out Elasticsearch"
    " +
                            "}", ContentType.APPLICATION_JSON);
            Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    
        /**
         * 获取 根据随机id
         * @throws IOException
         */
        @Test
        public void GetIndexAPIPost() throws IOException {
            String method = "GET";
            String endpoint = "twitter/_doc/EWq-LmkBioM-ebzVc1rq";
            Response response = restClient.performRequest(method,endpoint);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    
        /**
         *Stored Fields
         *get操作允许指定将通过传递stored_fields参数返回的一组存储字段。如果未存储请求的字段,则将忽略它们。
         * 首先得添加相应映射
         */
    
        @Test
        public void Stored() throws IOException {
            String method = "PUT";
            String endpoint = "twitters";
            HttpEntity entity = new NStringEntity(
                    "{
    " +
                            "   "mappings": {
    " +
                            "      "_doc": {
    " +
                            "         "properties": {
    " +
                            "            "counter": {
    " +
                            "               "type": "integer",
    " +
                            "               "store": false
    " +
                            "            },
    " +
                            "            "tags": {
    " +
                            "               "type": "keyword",
    " +
                            "               "store": true
    " +
                            "            }
    " +
                            "         }
    " +
                            "      }
    " +
                            "   }
    " +
                            "}", ContentType.APPLICATION_JSON);
            Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
        @Test
        public void putS() throws IOException {
            String method = "PUT";
            String endpoint = "twitters/_doc/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()));
        }
    
        @Test
        public void getS() throws IOException {
            String method = "GET";
            String endpoint = "twitters/_doc/1?stored_fields=tags,counter";
            Response response = restClient.performRequest(method,endpoint);
            System.out.println(EntityUtils.toString(response.getEntity()));
            /**
             * {"_index":"twitters","_type":"_doc","_id":"1","_version":3,"found":true,"fields":{"tags":["red"]}}
             由于该counter字段未存储,因此get请求在尝试获取时只是忽略它stored_fields。map映射时,counter的store属性为false
             */
    
        }
    
        @Test
        public void putS2() throws IOException {
            String method = "PUT";
            String endpoint = "twitters/_doc/2?routing=user11";
            HttpEntity entity = new NStringEntity(
                    "{
    " +
                            "    "counter" : 1,
    " +
                            "    "tags" : ["white"]
    " +
                            "}", ContentType.APPLICATION_JSON);
            Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    
        @Test
        public void getS2() throws IOException {
            String method = "GET";
            String endpoint = "twitters/_doc/2?routing=user11&stored_fields=tags,counter";
            /**
             * {"_index":"twitters","_type":"_doc","_id":"2","_version":2,"_routing":"user11","found":true,"fields":{"tags":["white"]}}
             * 使用控制路由的能力进行索引时,为了获取文档,还应提供路由值。
             */
            Response response = restClient.performRequest(method,endpoint);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    
        @Test
        public void getTest() throws IOException {
            String method = "GET";
            String endpoint = "twitters/_doc/1/_source";
            /**
             * 只获取_source文档的字段,而不包含任何其他内容
             {
             "counter" : 1,
             "tags" : ["red"]
             }
             */
            Response response = restClient.performRequest(method,endpoint);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    }
  • 相关阅读:
    (转载)VS2010/MFC编程入门之四十六(MFC常用类:MFC异常处理)
    (转载)VS2010/MFC编程入门之四十五(MFC常用类:CFile文件操作类)
    (转载)VS2010/MFC编程入门之四十四(MFC常用类:定时器Timer)
    (转载)VS2010/MFC编程入门之四十三(MFC常用类:CTime类和CTimeSpan类)
    Android笔记之ImageView
    Activity的四种launchMode
    Eclipse使用技巧
    ubuntu下使用MyEclipse以及MyEclipse Tomcat
    Android笔记之Json数据解析
    【转】[Android_机制]_Http和Socket连接区别
  • 原文地址:https://www.cnblogs.com/heqiyoujing/p/11148206.html
Copyright © 2011-2022 走看看