springboot整合ES的基本操作

2022-04-07  本文已影响0人  Java编程日记

springboot整合ES的基本操作

1,如何整合引入依赖坐标

2,简单的进行测试

首先要明确springboot对于elasticsearch的high-level并没有进行整合,这就表明我们需要手动导入坐标依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.78</version>
    </dependency>

接下来我们对elasticsearch进行测试

首先我们要测试是否可以开启客户端

@BeforeEach
void setUp() {

    HttpHost host=HttpHost.create("http://localhost:9200");
    RestClientBuilder builder= RestClient.builder(host);
    client=new RestHighLevelClient(builder);
}

@AfterEach
void tearDown() throws IOException {
    client.close();
}

客户端测试成功后我们再进行第一步简单操作建立一个索引
@Test
void contextLoads() throws IOException {

    CreateIndexRequest request=new CreateIndexRequest("books");

//这一段是对mappings进行设置字段和分子器
String json="{\n" +
" "mappings":{\n" +
" "properties":{\n" +
" "id":{\n" +
" "type":"keyword"\n" +
" },\n" +
" "name":{\n" +
" "type":"text",\n" +
" "analyzer":"ik_max_word",\n" +
" "copy_to":"all"\n" +
" },\n" +
" "type":{\n" +
" "type":"keyword"\n" +
" },\n" +
" "description":{\n" +
" "type":"text",\n" +
" "analyzer":"ik_max_word",\n" +
" "copy_to":"all"\n" +
" },\n" +
" "all":{\n" +
" "type":"text",\n" +
" "analyzer":"ik_max_word"\n" +
" }\n" +
"\n" +
"\n" +
" }\n" +
" }\n" +
"\n" +
"\n" +
"}";
//request.source()是对请求中参数的设置
request.source(json,XContentType.JSON);
//调用indices方法进行索引的创建
/*
* 这里的request代表我们需要发送一个请求
* RequestOptions.DEFAULT代表这里我们需要一个请求参数,我们这里直接默认就好了
* */
client.indices().create(request,RequestOptions.DEFAULT);
}
索引建立完毕后我们再添加文档

对于文档的添加我们有两种方式第一种就是单个添加

//这里我直接用了我mysql中的数据
//添加文档 利用json的转换 直接用mysql里面的数据
@Test
void createDoc() throws IOException {
book book = bookDao.selectById(1);
System.out.println(book);
IndexRequest request =new IndexRequest("books").id(book.getId().toString());
//将得到的数据转换成json格式
String json= JSON.toJSONString(book);
request.source(json,XContentType.JSON);
//调用index进行文档的添加
client.index(request,RequestOptions.DEFAULT);
}
第二种就是批量添加文档操作

//批量添加文档
@Test
void createDocMany() throws IOException {

    List<book> books = bookDao.selectList(null);
    BulkRequest bulk =new BulkRequest();
 for (book book:books) {
     IndexRequest request = new IndexRequest("books").id(book.getId().toString());
     String json = JSON.toJSONString(book);
     request.source(json, XContentType.JSON);
     //将遍历到的request都添加到bulk中
     bulk.add(request);
 }

//调用bulk()方法进行批量添加
    client.bulk(bulk,RequestOptions.DEFAULT);


}

在接下来就是查询了

先来个根据id查询

//查询
//按照id查询
@Test
void get() throws IOException {

    GetRequest request =new GetRequest("books","1");
    //利用get()方法进行根据id查询
    GetResponse response = client.get(request, RequestOptions.DEFAULT);
    //把里面的信息换成字符串
    String sourceAsString = response.getSourceAsString();
    System.out.println(sourceAsString);
}

再然后是我们日后常用到的条件查询了

//条件查询
@Test
void ifsearch() throws IOException {

    SearchRequest request=new SearchRequest("books");
    //builder一个搜素条件
    SearchSourceBuilder builder=new SearchSourceBuilder();
    builder.query(QueryBuilders.termQuery("name","sda"));
    request.source(builder);
    //调用条件搜索的方法
    SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    SearchHits hits = response.getHits();
    for(SearchHit hit:hits){

        String sourceAsString = hit.getSourceAsString();
        //将查询出来的json数据进行转换成book类型
        book book = JSON.parseObject(sourceAsString, book.class);

// System.out.println(sourceAsString);
System.out.println(book);

    }

}
上一篇下一篇

猜你喜欢

热点阅读