zoukankan      html  css  js  c++  java
  • Elasticsearch示例

    /**
     * @author: yqq
     * @date: 2019/2/28
     * @description:
     */
    public class TestMain {
        private static RestClient restClient;
    
        static {
            restClient=RestClient.builder(new HttpHost("localhost",9200,"http")).build();
        }
    
        /**
         * 1.查询所有数据
         * @throws Exception
         */
        @Test
        public void QueryAllSkuId() throws Exception {
            String method = "POST";
            String endpoint = "/sku/doc/_search";
            HttpEntity entity = new NStringEntity("{
    " +
                    "  "query": {
    " +
                    "    "match_all": {}
    " +
                    "  }
    " +
                    "}", ContentType.APPLICATION_JSON);
    
            Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    
        /**
         * 2.查询匹配,message为条件,新增的时候必须包含该字段
         * @throws IOException
         */
        @Test
        public void searchSkuId () throws IOException {
    
            /**
             * 添加的数据格式
             {"skuId":"111111111","content":"对于非基本类型,也就是常说的引用数据类型"}
             */
    
            String method = "POST";
            String endpoint = "/sku/doc/_search";
            HttpEntity entity = new NStringEntity(
                    "{
    " +
                            "  "query": { 
    " +
                            "    "match": {
    " +
                            "      "content": "基本类型"
    " +
                            "    }
    " +
                            "  }
    " +
                            "}", ContentType.APPLICATION_JSON);
            Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    
    
        /**
         * 3.查询所有数据
         * @throws Exception
         */
        @Test
        public void QueryAll() throws Exception {
            String method = "POST";
            String endpoint = "/delete-index/_search/";
            HttpEntity entity = new NStringEntity("{
    " +
                    "  "query": {
    " +
                    "    "match_all": {}
    " +
                    "  }
    " +
                    "}", ContentType.APPLICATION_JSON);
    
            Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    
        /**
         *4. 查询匹配,message为条件,新增的时候必须包含该字段
         * @throws IOException
         */
        @Test
        public void search () throws IOException {
    
            /**
             * 添加的数据格式
             {
    " +
             "    "user" : "kimchy",
    " +
             "    "post_date" : "2009-11-15T14:12:12",
    " +
             "    "message" : "trying out Elasticsearch"
    " +
             "}
             */
    
            String method = "POST";
            String endpoint = "/delete-index/_search/";
            HttpEntity entity = new NStringEntity(
                    "{
    " +
                            "  "query": { 
    " +
                            "    "match": {
    " +
                            "      "message": "out"
    " +
                            "    }
    " +
                            "  }
    " +
                            "}", ContentType.APPLICATION_JSON);
            Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    }

     引入依赖:

            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-client</artifactId>
                <version>6.5.3</version>
            </dependency>        
  • 相关阅读:
    C# 5.0 in a Nutshell 读书笔记(一) 野峰
    CLR via C# 4th Edition 野峰
    杰佛里给 Windows Server 2019 Automation with PowerShell 一书所写序言 野峰
    .NET的跨平台调用一例(PreserveSig) 野峰
    Http请求响应
    为什么不要追随新的linux发行版
    make: *** [out/host/linuxx86/obj/STATIC_LIBRARIES/libgtest_host_intermediates/gtestall.o] Error change
    linux 下at&t语法的汇编之hello world!!
    android 编译错误001_cm9 for onda elite vi10
    把vim当作16进制编辑器使用、、
  • 原文地址:https://www.cnblogs.com/heqiyoujing/p/11148299.html
Copyright © 2011-2022 走看看