ElasticSearch如何使用RestClient操作索引库和文档,看完这篇佬会有新的收获

907 阅读5分钟

前言:上文介绍了使用DSL语言操作索引库和文档,本篇文章将介绍使用Java中的RestClient来对索引库和文档进行操作。 希望能够加深自己的印象以及帮助到其他的小伙伴儿们😉😉。 如果文章有什么需要改进的地方还请大佬不吝赐教👏👏。

以下正文开始

在这里插入图片描述

好,那就详细记录下这块的知识。

前面记录了在网页端使用DSL语句对Elasticsearch的索引库和文档进行增删改查的简单操作。但是在日常的开发工作中,还是用Java语言操作比较多,因此需要使用Elasticsearch官方提供的RestClient操作索引库和文档。

首先准备一个索引库名为hotel的库并分析其中字段的数据结构,然后根据字段的名称,数据类型,是否参与搜索,是否分词,分词器等条件来完善其mapping,在其内部定义了一个名为“all”字段的属性,这个字段目的是将其他同时参与搜索的字段cope_to在一起,搜索的时候根据“all”字段内的查询条件一起搜索,可以提高搜索效率:

PUT /hotel
{
  "mappings": {
    "properties": {
      "id":{
        "type": "keyword"
      },
      "name":{
        "type": "text",
        "analyzer": "ik_max_word",
        "copy_to": "all"
      },
      "address":{
        "type": "keyword",
        "index": false
      },
      "price":{
        "type": "integer"
      },
      "score":{
        "type": "integer"
      },
      "brand":{
        "type": "keyword",
        "copy_to": "all"
      },
      "city":{
        "type":"keyword"
      },
      "starName":{
        "type": "keyword"
      },
      "business":{
        "type": "keyword"
        , "copy_to": "all"
      },
      "location":{
        "type": "geo_point"
      },
      "pic":{
        "type": "keyword",
        "index": false
      },
       "all":{
        "type": "text",
        "analyzer": "ik_max_word"
      }
      }
  }
}

那么,如何在idea中操作对索引库和文档进行操作呢?

倘若我们想要使用RestClient来操作,首要任务就是引入其依赖:

        <!--elasticsearch-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.12.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
        </dependency>

第二步,在测试类中编写测试方法,我们需要创建RestClient对象,然后对RestClient进行初始化,当然创建完成RestClient后需要销毁,代码如下:

    private RestHighLevelClient client;
    @BeforeEach //创建对象初始化
    void setUp() {
        client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.220.***:9200") //创建方法
        ));
    }
    @AfterEach //创建完成后销毁
    void tearDown() throws IOException {
        client.close();
    }

RestClient操作索引库

创建索引库

这里的创建索引库方法和我们上面DSL语句的含义是一样的,虽然描述方式有所不同。

  • 第一行代码创建索引库,相当于DSL语句中的PUT /hotel
  • 第二行代码准备请求参数,MAPPING_TEMPLATE为创建DSL语句中的内容(除去PUT /hotel那些)
  • 第三步调用client的indices()拿到操作索引库的所有方法,然后取出create方法
    @Test
    void testCreateIndex() throws IOException {
        // 1.准备Request      PUT /hotel
        CreateIndexRequest request = new CreateIndexRequest("hotel");
        // 2.准备请求参数
        request.source(MAPPING_TEMPLATE, XContentType.JSON);//这里将DSL语句封装成了MAPPING_TEMPLATE,优雅美观
        // 3.发送请求
        client.indices().create(request, RequestOptions.DEFAULT);
    }

可以进入indices方法的源码,查看得返回的是indicesClient,如下:

public final IndicesClient indices() {
    return this.indicesClient;
}

delete的方法的源码,由此可见上面传入request请求即可:

public final class IndicesClient {
    private final RestHighLevelClient restHighLevelClient;

    IndicesClient(RestHighLevelClient restHighLevelClient) {
        this.restHighLevelClient = restHighLevelClient;
    }
    
    public AcknowledgedResponse delete(DeleteIndexRequest deleteIndexRequest, RequestOptions options) throws IOException {
        return restHighLevelClient.performRequestAndParseEntity(deleteIndexRequest, IndicesRequestConverters::deleteIndex, options,
            AcknowledgedResponse::fromXContent, emptySet());
    }
}

删除索引库

根据以上信息,我们不难得出使用RestClient删除索引库和判断索引库是否存在的相关代码:

    @Test //判断索引库是否存在
    void testExistsIndex() throws IOException {
        // 1.准备Request,注意这块是获取索引库请求而不是创建
        GetIndexRequest request = new GetIndexRequest("hotel");
        // 3.发送请求,调用exists方法
        boolean isExists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.err.println(isExists ? "索引库存在" : "索引库不存在");
    }
    
    @Test  //删除索引库操作
    void testDeleteIndex() throws IOException {
        // 1.准备Request,指定删除哪个索引库
        DeleteIndexRequest request = new DeleteIndexRequest("hotel"); 
        // 3.发送请求,调用delete方法
        client.indices().delete(request, RequestOptions.DEFAULT);
    }

综上所述,索引库操作的基本步骤: • 初始化RestHighLevelClient • 创建XxxIndexRequest。Xxx可以是Create,Get,Delete • 准备DSL语句( Create时需要) • 发送请求。调用RestHighLevelClient#indices().xxx()方法,xxx可以是create,exists,delete

RestClient操作文档

操作文档和操作索引库一样,需要完成RestClient的初始化和销毁操作,这里不展现重复代码了。

插入文档

前面调用MybatisPlus中查询的方法从数据库中查询出ID为61083的信息,由于索引库和数据库中的某字段不是很对应,所以做了一次转换。之后开始操作文档。

  • 第一步,创建文档,与POST /索引库名称/_doc/1相对应
  • 第二步,准备json文档,上部代码已经对数据json序列化了
  • 第三步,直接调用index方法发送请求
    private RestHighLevelClient client;
    @Autowired
    private IHotelService hotelService;
    
    @Test
    void testAddDocument() throws IOException {
        // 1.查询数据库hotel数据
        Hotel hotel = hotelService.getById(61083L);
        // 2.转换为HotelDoc
        HotelDoc hotelDoc = new HotelDoc(hotel);
        // 3.转JSON
        String json = JSON.toJSONString(hotelDoc);

        // 1.准备Request
        IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());//索引库中对id的要求为"keyword",因此要转换成string类型
        // 2.准备请求参数DSL,其实就是文档的JSON字符串
        request.source(json, XContentType.JSON);
        // 3.发送请求
        client.index(request, RequestOptions.DEFAULT);
    }

查询文档

根据id查询文档信息,相对应的DSL语句为:GET /数据库名称/_doc/1。查询是新建GetRequest对象。

    @Test
    void testGetDocumentById() throws IOException {
        // 1.准备Request      // GET /hotel/_doc/{id}
        GetRequest request = new GetRequest("hotel", "61083");
        // 2.发送请求
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        // 3.解析响应结果
        String json = response.getSourceAsString();
        //查询出来的对象是json形式,这里转换成HotelDoc对象形式
        HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
        System.out.println("hotelDoc = " + hotelDoc);
    }

修改文档

前面介绍的修改有两种方式,一种为全局修改,一种为增量修改,这里以增量修改为例(DSL语句:POST /数据库名称/_doc/1):

    @Test
    void testUpdateById() throws IOException {
        // 1.准备Request
        UpdateRequest request = new UpdateRequest("hotel", "61083");
        // 2.准备参数,这里是需要改变的参数
        request.doc(
                "price", "870"
        );
        // 3.发送请求
        client.update(request, RequestOptions.DEFAULT);
    }

文档操作的基本步骤: • 初始化RestHighLevelClient • 创建XxxRequest。Xxx可以是Index,Get,Update,Delete • 准备参数(Index和Update时需要) • 发送请求。调用RestHighLevelClient.xxx()方法,xxx可以是index,get,update,delete • 解析结果(Get时需要,将查询出的json形式转化为对象形式)

本篇文章就先分享到这里了,后续会继续分享其他方面的知识,感谢大佬认真读完支持咯~