es 相关

Posted smj1990

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了es 相关相关的知识,希望对你有一定的参考价值。

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 相关的主要内容,如果未能解决你的问题,请参考以下文章

es相关命令

es 相关

es基本修改相关的

es相关总结

ES6相关知识

ES6-ES11相关