第六节 ElasticSearch的Java High Level REST Client API

亮子 2021-06-16 02:32:51 22779 1 1 0

1、官方文档

1)、官网

https://www.elastic.co/cn/

2)、官方文档

https://www.elastic.co/guide/index.html

elasticsearch 官网

3)、客户端文档

选择Elasticsearch: Store, Search, and Analyze区域的Elasticsearch Clients链接,如下图:

客户端文档

选择Java REST Client [7.13] — other versions,然后选择自己需要的版本就可以了,如下图:

Java REST Client

4)、Java High Level REST Client

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.8/index.html

Java High Level REST Client Search Apis

Java High Level REST Client Index Apis

2、添加依赖

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

3、添加测试依赖

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

4、创建测试类

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Before;
import org.junit.Test;

public class DemoElasticSearchTest {

    private RestHighLevelClient client = null;

    @Before
    public void init() {
        client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http")));
    }

    @Test
    public void createIndex() {
        System.out.println(client);
    }
}

5、创建索引

    @Test
    public void createIndex() throws IOException {
        // 创建请求
        CreateIndexRequest request = new CreateIndexRequest("tb_hero");

        // 设置索引参数
        request.settings(Settings.builder()
                .put("index.number_of_shards", 3)
                .put("index.number_of_replicas", 2)
        );

        //
        // 设置映射
        Map<String, Object> properties = new HashMap<>();

        //-- name
        Map<String, Object> name = new HashMap<>();
        name.put("type", "text");
        properties.put("name", name);

        //-- gender
        Map<String, Object> gender = new HashMap<>();
        gender.put("type", "text");
        properties.put("gender", gender);

        //-- age
        Map<String, Object> age = new HashMap<>();
        age.put("type", "integer");
        properties.put("age", age);

        //-- work
        Map<String, Object> work = new HashMap<>();
        work.put("type", "text");
        properties.put("work", work);

        Map<String, Object> mapping = new HashMap<>();
        mapping.put("properties", properties);

        request.mapping(mapping);

        CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
        client.close();

        System.out.println("-- end");
    }

6、判断索引是否存在

    @Test
    public void indexExits() throws IOException {

        GetIndexRequest request = new GetIndexRequest("tb_hero");
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);

        System.out.println(exists);
    }

7、修改索引设置

    @Test
    public void updateIndexSetting() throws IOException {
        UpdateSettingsRequest request = new UpdateSettingsRequest("tb_hero");

        String settingKey = "index.number_of_replicas";
        int settingValue = 0;
        Settings settings =
                Settings.builder()
                        .put(settingKey, settingValue)
                        .build();

        request.settings(settings);

        AcknowledgedResponse updateSettingsResponse =
                client.indices().putSettings(request, RequestOptions.DEFAULT);
        System.out.println(updateSettingsResponse);
    }

8、修改索引的映射

  • 增加映射字段
    @Test
    public void appendIndexMapping() throws IOException {
        PutMappingRequest request = new PutMappingRequest("tb_hero");

        Map<String, Object> message = new HashMap<>();
        message.put("type", "text");

        Map<String, Object> properties = new HashMap<>();
        properties.put("message", message);

        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("properties", properties);

        request.source(jsonMap);

        AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);
        System.out.println(putMappingResponse);
    }
  • 添加ik分词器映射字段
    @Test
    public void putIndexMapping() throws IOException {
        PutMappingRequest request = new PutMappingRequest("tb_hero");

        Map<String, Object> message = new HashMap<>();
        message.put("type", "text");
        message.put("analyzer", "ik_smart");

        Map<String, Object> properties = new HashMap<>();
        properties.put("message2", message);

        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("properties", properties);

        request.source(jsonMap);

        AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);
        System.out.println(putMappingResponse);
    }
  • 获取映射字段
@Test
    public void getIndexMapping() throws IOException {
        GetMappingsRequest request = new GetMappingsRequest();
        request.indices("tb_hero");

        GetMappingsResponse getMappingResponse = client.indices().getMapping(request, RequestOptions.DEFAULT);

        Map<String, MappingMetadata> allMappings = getMappingResponse.mappings();
        MappingMetadata indexMapping = allMappings.get("tb_hero");
        Map<String, Object> mapping = indexMapping.sourceAsMap();

        System.out.println(mapping);
    }

8、删除索引

    public void deleteIndex() throws IOException {
        // 删除
        DeleteIndexRequest request = new DeleteIndexRequest("tb_hero");
        AcknowledgedResponse deleteIndexResponse = client.indices().delete(request, RequestOptions.DEFAULT);

        // 检测
        GetIndexRequest getRequest = new GetIndexRequest("tb_hero");
        boolean exists = client.indices().exists(getRequest, RequestOptions.DEFAULT);

        System.out.println(exists);
    }