• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

springboot集成es详解

springboot 搞代码 4年前 (2022-01-09) 19次浏览 已收录 0个评论

1.导入 maven依赖

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-dataelasticsearch</artifactId>
<dependency>

注意 保持版本一致 我用的是7.6.2版本的

<properties>
      <java.version>1.8</java.version>
       <elasticsearch.version>7.6.2</elasticsearch.version>
     <!--自定义版本 保持版本一致-->
    </properties>

2.编写config类 相当于 xlm导入文档

@Configuration
public class ESConfig {

  @Bean
  public RestHighLevelClient restHighLevelClient (){
    RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
        RestClient.builder(
            new HttpHost("localhost",9100,"http")
        )
    );
    return restHighLevelClient;
  }

注意这里的端口号 一定不能搞错

3测试书写 添加 索引

@Test
  void contextLoads() throws IOException {

    //1.创建索引的请求
    CreateIndexRequest createIndexRequest = new CreateIndexRequest("mao");
    //2.执行请求 获得响应
    CreateIndexResponse createIndexResponse = estHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
    System.out.println(createIndexResponse);
  }

4.查询索引是否存在

@Test //查询索引是否存在
  void existIndex() throws IOException {
    GetIndexRequest getIndexRequest = new GetIndexRequest("test"); //获得索引请求
    boolean exists = estHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
    System.out.println(exists);
  }

5.删除索引

@Test//删除
  void delIndex() throws IOException {
    DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("test");
    AcknowledgedResponse delete = estHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
    System.out.println(delete);
    System.out.println(delete.isAcknowledged());
  }

6.添加文档数据 第一 要设置实体类 导入阿里巴巴JSON 工具类

 @Data
@Accessors(chain = true) //实体类

public class User {
  private String name;
  private String age;
}
@Test //添加文档
  void addDocument() throws IOException {
    //创建对象啊
    User user = new User().setAge("13").setName("mao");
    //创建请求
    IndexRequest request = new IndexRequest("mao");
    //设置规则 PUT /test/_doc/id
    request.id("1");
    request.timeout("1s");
    //将请求放入josn
    request.source(JSON.toJSONString(user),XContentType.JSON);
    //客户端发送请求
   <span>本文来源gaodai#ma#com搞*!代#%^码$网*</span> IndexResponse index = estHighLevelClient.index(request, RequestOptions.DEFAULT);
    //获取响应结果
    System.out.println(index.toString());
    System.out.println(index.status());
 <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.47</version>
    </dependency>

7.修改文档

@Test //Update 文档操作
  void GengXin() throws IOException {
    UpdateRequest updateRequest = new UpdateRequest("mao","1"); //请求更新文档
    updateRequest.timeout("1s"); //设置超时时间
    User user= new User().setName("张三").setAge("26");
    updateRequest.doc(JSON.toJSONString(user),XContentType.JSON); //将对象封装丢进去 XContentType方法 将要传输的数据进行告知
    UpdateResponse update = estHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);//发送请求
    System.out.println(update);
  }

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:springboot集成es详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址