使用elasticsearch REST API操作elasticsearch5.3

1.官方API地址

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

2.maven依赖

 
           org.elasticsearch.client
           rest
           5.3.0
       

       
             org.apache.httpcomponents
             httpclient
            4.5.3
       

       
             org.apache.httpcomponents
             httpasyncclient
            4.1.3
       

        
             org.apache.httpcomponents
             httpcore-nio
            4.4.6
       

       
             org.apache.httpcomponents
             httpcore
            4.4.6
       

3.简单查询

   (1)连接

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

    (2)GET查询(search url)

           Map map=new HashMap();
            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.emptyMap(),entity);

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

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

你可能感兴趣的:(elasticsearch)