使用elasticsearch REST API操作elasticsearch5.3

1.官方API地址

  https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-get.html

2.maven依赖

  <dependency>
           <groupId>org.elasticsearch.client</groupId>
           <artifactId>rest</artifactId>
           <version>5.3.0</version>
        </dependency>
        <dependency>
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
        <dependency>
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpasyncclient</artifactId>
            <version>4.1.3</version>
        </dependency>
         <dependency>
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpcore-nio</artifactId>
            <version>4.4.6</version>
        </dependency>
        <dependency>
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpcore</artifactId>
            <version>4.4.6</version>
        </dependency>

3.简单查询

   (1)连接

    RestClient restClient = RestClient.builder(
                new HttpHost("192.168.2.78", 9200, "http")).build();

    (2)GET查询(search url)

           Map<String,String> map=new HashMap<String,String>();
            map.put("q", "name:lili");

            response = restClient.performRequest("GET", "/index/type/_search",
                map);

    (3)GET查询(search body)

        JSONObject job=new JSONObject();
        job.put("name","lili");
        JSONObject job1=new JSONObject();
        job1.put("term", job);
        JSONObject job2=new JSONObject();
        job2.put("query",job1);
        String json=job2.toString();
        HttpEntity entity = new NStringEntity(json);

        response = restClient.performRequest("GET", "/onekeymove/source_category/_search",
                Collections.<String, String>emptyMap(),entity);

关于其它查询,官网也给出了示例

    https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值