使用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

猜你喜欢

转载自girl-luo.iteye.com/blog/2368595
今日推荐