Elasticsearch通过Java客户端API操作索引库(三)
  19qMgiCiiRfc 2023年11月05日 26 0

一、添加依赖

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

二、创建索引库

1. 创建Request对象。因为是创建索引库的操作,因此Request是CreateIndexRequest。

2. 添加请求参数,其实就是DSL的JSON参数部分。因为json字符串很长,这里是定义了静态字符串常量MAPPING_TEMPLATE,让代码看起来更加优雅。

3. 发送请求,client.indices()方法的返回值是IndicesClient类型,封装了所有与索引库操作有关的方法。

示例代码:

package com.example.esdemo;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

@SpringBootTest
class EsDemoApplicationTests {
    public static final String MAPPING_TEMPLATE = "{\n" +
            "  \"mappings\": {\n" +
            "    \"language\":{\n" +
            "       \"properties\": {\n" +
            "      \"info\": {\n" +
            "        \"type\": \"text\",\n" +
            "        \"analyzer\": \"standard\"\n" +
            "      },\n" +
            "      \"email\": {\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"index\": false\n" +
            "      },\n" +
            "      \"name\": {\n" +
            "        \"type\": \"object\",\n" +
            "        \"properties\": {\n" +
            "          \"firstName\": {\n" +
            "            \"type\": \"keyword\"\n" +
            "          },\n" +
            "          \"lastName\":{\n" +
            "            \"type\": \"keyword\"\n" +
            "          }\n" +
            "        }\n" +
            "      }\n" +
            "    }\n" +
            "    }\n" +
            "   \n" +
            "  }\n" +
            "}";

    private RestHighLevelClient client;

    @BeforeEach
    void setUp() {
        // 初始化代码 建立连接
        this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://127.0.0.1:9200")));
    }

    /**
     * 新增索引库
     *
     * @throws IOException
     */
    @Test
    void add() throws IOException {
        // 1.创建Request对象
        CreateIndexRequest request = new CreateIndexRequest("myindex");
        // 2.准备请求的参数:DSL语句
        request.source(MAPPING_TEMPLATE, XContentType.JSON);
        // 3.发送请求
        client.indices().create(request, RequestOptions.DEFAULT);
    }

    @AfterEach
    void tearDown() throws IOException {
        // 结束代码
        this.client.close();
    }

}

Elasticsearch通过Java客户端API操作索引库(三)_索引

三、删除索引库

1. 创建Request对象。这次是DeleteIndexRequest对象

2.发送请求。改用delete方

package com.example.esdemo;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

@SpringBootTest
class EsDemoApplicationTests {
    
    private RestHighLevelClient client;

    @BeforeEach
    void setUp() {
        // 初始化代码 建立连接
        this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://127.0.0.1:9200")));
    }

    /**
     * 删除索引库
     *
     * @throws IOException
     */
    @Test
    void delete() throws IOException {
        //1.创建Request对象
        DeleteIndexRequest request = new DeleteIndexRequest("myindex");
        // 2.发送请求
        client.indices().delete(request, RequestOptions.DEFAULT);
    }

    @AfterEach
    void tearDown() throws IOException {
        // 结束代码
        this.client.close();
    }

}

Elasticsearch通过Java客户端API操作索引库(三)_索引_02

四、查询索引库

1. 创建Request对象。这次是GetIndexRequest对象

2.发送请求。改用exists方法

示例代码:

package com.example.esdemo;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

@SpringBootTest
class EsDemoApplicationTests {
   
    private RestHighLevelClient client;

    @BeforeEach
    void setUp() {
        // 初始化代码 建立连接
        this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://127.0.0.1:9200")));
    }

    /**
     * 查询索引库
     *
     * @throws IOException
     */
    @Test
    void contextLoads() throws IOException {
        //1.创建Request对象
        GetIndexRequest request = new GetIndexRequest("myindex");
        // 2.发送请求
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists ? "索引库已经存在!" : "索引库不存在");
    }

    @AfterEach
    void tearDown() throws IOException {
        // 结束代码
        this.client.close();
    }

}

输出:

索引库不存在


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
19qMgiCiiRfc