欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot集成es详解

程序员文章站 2022-06-25 11:46:51
1.导入 maven依赖 org.springframework.boot

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);
    //客户端发送请求
    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);
  }

8.批量增加

@test //批量丢入数据
  void testbulkindexrequest() throws ioexception {
    bulkrequest bulkrequest = new bulkrequest(); //大批量导入数据 本质是for循环
    bulkrequest.timeout("10s");
    arraylist<user> users = new arraylist<>();
    for(int i=0;i<10;i++){
      users.add(new user().setname("张三"+i+"号").setage(""+i));
    }

    //批处理请求
    for(int i =0;i<users.size();i++){
      bulkrequest.add(
          new indexrequest("mao")
          .id(""+(i+1))
          .source(json.tojsonstring(users.get(i)),xcontenttype.json)
      );

    }
    bulkresponse bulk = esthighlevelclient.bulk(bulkrequest, requestoptions.default);
    system.out.println(bulk);
    system.out.println(bulk.hasfailures());//查询是否失败

  }

9.精确查询

 @test//查询
  void testsearch() throws ioexception {
    searchrequest searchrequest = new searchrequest(esconstant.esconstant);
    searchsourcebuilder searchsourcebuilder = new searchsourcebuilder();
    //精确查询条件


    termquerybuilder termquerybuilder = querybuilders.termquery("age", "0");
    //查询所有
//    matchallquerybuilder matchallquerybuilder = querybuilders.matchallquery();
     searchsourcebuilder.query(termquerybuilder) ;//将规则加入
//     searchsourcebuilder.from(); //设置分页
//     searchsourcebuilder.size();
     searchsourcebuilder.timeout(new timevalue(60, timeunit.seconds));//设置高并发下的延迟时间
     searchsourcebuilder.highlighter();
     searchrequest.source(searchsourcebuilder);//将刚刚做的请求体放入
    searchresponse search = esthighlevelclient.search(searchrequest, requestoptions.default);//请求信息
    system.out.println(json.tojsonstring(search.gethits()));//返回查询情况 gethits 封装返回对象

    for( searchhit searchhit:search.gethits().gethits() ){
      system.out.println(searchhit.getsourceasmap());

    }
  } 

到此这篇关于springboot集成es详解的文章就介绍到这了,更多相关springboot集成es内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: springboot es