【RestTemplate】使用RestTemplate访问Elasticsearch示例

【依赖】

一般来说,SpringBoot程序自带RestTemplate,不需要额外引用。

如果是Maven程序,可以载入以下引用:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>

 

【GET请求】

Code:

复制代码
import org.springframework.web.client.RestTemplate;

public class Test1 {
    public static void main(String[] args){
        RestTemplate restTemplate = new RestTemplate();
        String json = restTemplate.getForObject("http://192.168.32.130:9200/",String.class);
        System.out.println(json);
    }
}
复制代码

输出:

复制代码
{
  "name" : "node-1",
  "cluster_name" : "dual-app130",
  "cluster_uuid" : "OHQmV0p8QUCLutfepnipqQ",
  "version" : {
    "number" : "7.11.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "8ced7813d6f16d2ef30792e2fcde3e755795ee04",
    "build_date" : "2021-02-08T22:44:01.320463Z",
    "build_snapshot" : false,
    "lucene_version" : "8.7.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}
复制代码

 

【POST请求】

code:

复制代码
import org.springframework.web.client.RestTemplate;

import java.util.LinkedHashMap;
import java.util.Map;

public class Test1 {
    public static void main(String[] args){

        RestTemplate restTemplate = new RestTemplate();
        Map<String,Object> paramMap=new LinkedHashMap<String,Object>();
        paramMap.put("name", "hy");
        paramMap.put("age", "25");

        String rsps=restTemplate.postForObject("http://192.168.32.130:9200/moft/emp/111?pretty",paramMap,String.class);
        System.out.println(rsps);
    }

}
复制代码

输出:

复制代码
{
  "_index" : "moft",
  "_type" : "emp",
  "_id" : "111",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
复制代码

 查询刚才插入的数据:

        RestTemplate restTemplate = new RestTemplate();
        String json = restTemplate.getForObject("http://192.168.32.130:9200/moft/emp/111?pretty",String.class);
        System.out.println(json);

输出:

复制代码
{
  "_index" : "moft",
  "_type" : "emp",
  "_id" : "111",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "hy",
    "age" : "25"
  }
}
复制代码

 

【查找emp下所有】

Code:

        RestTemplate restTemplate = new RestTemplate();
        String json = restTemplate.getForObject("http://192.168.32.130:9200/moft/emp/_search?pretty",String.class);
        System.out.println(json);

输出:

复制代码
{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "moft",
        "_type" : "emp",
        "_id" : "111",
        "_score" : 1.0,
        "_source" : {
          "name" : "hy",
          "age" : "25"
        }
      },
      {
        "_index" : "moft",
        "_type" : "emp",
        "_id" : "112",
        "_score" : 1.0,
        "_source" : {
          "name" : "巴菲特",
          "age" : "27"
        }
      },
      {
        "_index" : "moft",
        "_type" : "emp",
        "_id" : "113",
        "_score" : 1.0,
        "_source" : {
          "name" : "比尔盖茨",
          "age" : "29"
        }
      }
    ]
  }
}
复制代码

 

【按名称查找】

 Code:

        RestTemplate restTemplate = new RestTemplate();
        String json = restTemplate.getForObject("http://192.168.32.130:9200/moft/emp/_search?q=name:比尔盖茨&pretty",String.class);
        System.out.println(json);

输出:

复制代码
{
  "took" : 49,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 2.4398782,
    "hits" : [
      {
        "_index" : "moft",
        "_type" : "emp",
        "_id" : "113",
        "_score" : 2.4398782,
        "_source" : {
          "name" : "比尔盖茨",
          "age" : "29"
        }
      }
    ]
  }
}
复制代码

 

posted @   逆火狂飙  阅读(409)  评论(2编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2020-02-25 Oracle的timestamp字段更新实验 结论:只有逐条更新才能保证timestamp字段有差别,批量更新只会得到一致的时间,此操作无关时间精度.
2015-02-25 【高中数学/极值问题】已知:a,b皆为正实数,且2a+b=1,求:a/(2-2a)+b/(2-b)的最小值?
2014-02-25 【Canvas与光阑】立方体六彩光阑
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示