码迷,mamicode.com
首页 > 其他好文 > 详细

Elasticsearch示例

时间:2019-07-07 22:45:51      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:ring   添加   color   基本   content   method   esc   ast   tostring   

/**
 * @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("{\n" +
                "  \"query\": {\n" +
                "    \"match_all\": {}\n" +
                "  }\n" +
                "}", 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(
                "{\n" +
                        "  \"query\": { \n" +
                        "    \"match\": {\n" +
                        "      \"content\": \"基本类型\"\n" +
                        "    }\n" +
                        "  }\n" +
                        "}", 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("{\n" +
                "  \"query\": {\n" +
                "    \"match_all\": {}\n" +
                "  }\n" +
                "}", 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 {

        /**
         * 添加的数据格式
         {\n" +
         "    \"user\" : \"kimchy\",\n" +
         "    \"post_date\" : \"2009-11-15T14:12:12\",\n" +
         "    \"message\" : \"trying out Elasticsearch\"\n" +
         "}
         */

        String method = "POST";
        String endpoint = "/delete-index/_search/";
        HttpEntity entity = new NStringEntity(
                "{\n" +
                        "  \"query\": { \n" +
                        "    \"match\": {\n" +
                        "      \"message\": \"out\"\n" +
                        "    }\n" +
                        "  }\n" +
                        "}", 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>        

 

Elasticsearch示例

标签:ring   添加   color   基本   content   method   esc   ast   tostring   

原文地址:https://www.cnblogs.com/heqiyoujing/p/11148299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!