Springboot 整合ES客户端及查询文档

1. 整合ES客户端

(1) 导入坐标

//低级别不推荐使用
<dependency>
  <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

//推荐使用(高级别)
<dependency>
  <groupId>org.elasticsearch.client</groupId>
 <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

(2) 配置

yml:

//低级别
elasticsearch:
  rest:
    uris:http://localhost:9200
//高级别不用写配置

(3) 使用

@SpringbootTest
class SpringbootApplicationTest{
 // @Autowired
 // private ElasticsearchRestTemplate template;  //这种客户端不推荐使用(低级别)
 //高版本 创建客户端
    //由于Spring并未整合高版本,所以不能通过autowired注入 
  private RestHighLevelClient client;
    @Test
    void testCreateIndex() throws IOException{
      HttpHost host = HttpHost.create("http://localhost:9200");
      RestClientBuilder builder = RestClient.builder(host)
      client = new RestHightLevelClient(builder);
      CreateIndexRequest request = new CreateIndexRequest("books");
      client.indices().create(request,RequestOptions.DEFAULT);
      client.close();
    }
 @Test
    void testCreateIndexById() throws IOException{
    // 设置请求中的参数
      String json ="{}"
      request.source ("json",XContentType.JSON);
    //客户端操作
      CreateIndexRequest request = new CreateIndexRequest("books");
    //获取操作索引的客户端对象,调用创建索引操作 
      client.indices().create(request,RequestOptions.DEFAULT);
      client.close();
    }
  //添加文档(单个)
    void testCreatorDoc(){
     IndexRequest request = new IndexRequest("books").id("1");
     String json = "";
     request.source(json,XContentType.JSON); 
     client.index(request,RequestOptions.DEFAULT);
    }
 //批量添加文档
@Test
 void testCreateDocAll() throws IOException{
     List<Book> bookList = bookDao.selectList(null);
     BulkRequest bulk = new BulkRequest();
     for(Book book :bookList){
       IndexRequest request = new IndexRequest("books").id(book.getId().toString());
       String json = JSON.toJSONString(book);
       request.source(json,XContentType.JSON);
       bulk.add(request);
     }
     client.bulk(bulk,RequestOptions.DEFAULT);
     }

 }
}

2. 查询文档

@Test
 //按id查询
 void testGet(){
     GetRequest request = new GetRequest("books","1");
     GetResponse response = client.get(request,RequestOptions.DEFAULT);
     String json = response.getSourceAsString();
     System.out.println(json);
 }
@Test 
  //按条件查询
  void testSearch(){
    SearchRequest request = new SearchRequest("books");
    SearchSourceBuilder builder = new SearchSourceBuilder();

    builder.query(QueryBuilders.termQuery("all","spring"));
    request.source(builder);

    SearchResponse reponse = client.search(request,RequestOptions.DEFALT);
    SearchHits hits = response.getHits();
    for ( SearchHit hit : hits){
      String source = hit.getSourceAsString();
      Book book = JSON.parseObject(source,Book.class);
      System.out.println(book);
    }
  }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿土不土

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值