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

es 相关

时间:2019-10-31 16:27:43      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:doc   cti   安装   arc   ast   delete   null   使用   put   

1.安装 es

2.安装 grunt

3.安装 logstash

4.安装 kibana

5.引入 jar 包
<dependency>
  <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.3.1</version>
    <exclusions>
      <exclusion>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
      </exclusion>
      <exclusion>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-client</artifactId>
      </exclusion>
    </exclusions>
</dependency>

<dependency>
  <groupId>org.elasticsearch.client</groupId>
  <artifactId>elasticsearch-rest-client</artifactId>
  <version>7.3.1</version>
</dependency>

<dependency>
  <groupId>org.elasticsearch</groupId>
  <artifactId>elasticsearch</artifactId>
  <version>7.3.1</version>
</dependency>

6.启动类中注入 bean

@Bean
public RestHighLevelClient client(){
  RestHighLevelClient client=new RestHighLevelClient(
    RestClient.builder(
    new HttpHost("localhost",9200,"http")
    //这里如果要用client去访问其他节点,就添加进去
    )
  );
  return client;
}

7.使用 client 进行增删改查

/**
* 新增
* @param user
* @return
*/
@RequestMapping("/addEsUser")
@ResponseBody
public String addEsUser(@RequestBody User user){
  XContentBuilder builder=null;
  IndexRequest request=new IndexRequest("user");
  String id = "smj";
  try {
    builder = XContentFactory.jsonBuilder();
    builder.startObject()
    .field("userName",user.getUserName())
    .field("userPassword",user.getUserPassword())
    .field("create_time",new Date())
    .field("creator","smj")
    .endObject();
    request.id(id).opType("create").source(builder);
    IndexResponse response=client.index(request, RequestOptions.DEFAULT);
    logger.info("返回结果为:" + JSON.toJSONString(response.getResult()));
  } catch (IOException e) {
    e.printStackTrace();
  }
  return "嘿嘿嘿";
}

/**
* 查询
* @param id
* @return
*/
@RequestMapping("/getEsUser")
@ResponseBody
public String getEsUser(String id){
  GetRequest getRequest=new GetRequest("user",id);
  Map map=new HashMap();
  GetResponse response=null;
  try{
    response= client.get(getRequest, RequestOptions.DEFAULT);

  } catch (IOException e) {
    e.printStackTrace();
  }
  if(response.isExists()){
    map.put("success",true);
    map.put("data",response.getSource());
  }else{
    map.put("success",false);
  }
  return JSON.toJSONString(map);
}

/**
* 删除
* @param id
* @return
*/
@RequestMapping("/delEsUser")
@ResponseBody
public String delEsUser(String id){
  XContentBuilder builder=null;
  DeleteRequest request=new DeleteRequest("user",id);
  DeleteResponse response = null;
  try {
    response = client.delete(request, RequestOptions.DEFAULT);
    logger.info("返回结果为:" + JSON.toJSONString(response.getResult()));
  } catch (IOException e) {
    e.printStackTrace();
  }
  return JSON.toJSONString(response.getResult());
}

/**
* 修改
* @param user
* @return
*/
@RequestMapping("/updateEsUser")
@ResponseBody
public String updateEsUser(@RequestBody User user){
  XContentBuilder builder=null;
  UpdateRequest request = new UpdateRequest("user","smj");
  try {
    builder = XContentFactory.jsonBuilder();
    builder.startObject()
    .field("userName",user.getUserName())
    .field("userPassword",user.getUserPassword())
    .field("create_time",new Date())
    .field("creator","smj")
    .endObject();
    request.doc(builder);
    UpdateResponse response=client.update(request, RequestOptions.DEFAULT);
    logger.info("返回结果为:" + JSON.toJSONString(response.getResult()));
  } catch (IOException e) {
    e.printStackTrace();
  }
  return "嘿嘿嘿";
}

es 相关

标签:doc   cti   安装   arc   ast   delete   null   使用   put   

原文地址:https://www.cnblogs.com/smj1990/p/11771594.html

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