ElasticSearch JavaRestClient之索引库操作

前言

在 Elasticsearch 中,索引库是数据存储和查询的基础,学习如何通过 Java 客户端进行索引库的操作是很重要的。以下是使用 Java 客户端来创建、查询和删除索引库的详细操作步骤和实现。



操作流程概述

首先对比一下,创建索引库的 Java API 和 Restful 接口 API 对比:

可以看出,操作的基本结构就是:创建请求对象设置请求体发送请求

  1. 创建请求对象:为不同的操作(如创建、查询、删除)创建对应的请求对象(例如:CreateIndexRequestGetIndexRequestDeleteIndexRequest)。
  2. 设置请求体:对于创建索引库等操作,可能需要设置请求体(通常是 JSON 格式的映射)。
  3. 发送请求:通过 Elasticsearch 客户端发送请求,执行具体操作(如创建、查询或删除)。



创建索引库

创建请求对象

创建索引库的请求对象使用 CreateIndexRequest 类,指定索引库的名称。

// 准备Request对象
CreateIndexRequest request = new CreateIndexRequest("items");

这行代码的作用是创建一个名为 "items" 的索引库。

设置请求体

请求体包含索引库的映射(mapping),通常是一个 JSON 格式的字符串。映射中包含字段定义、数据类型等信息。我们可以通过 .source() 方法将映射数据传入。

映射的例子:

private static String MAPPING_TIMPLATE = "{" +
    "\"mappings\": {" +
    "  \"properties\": {" +
    "    \"product_name\": {\"type\": \"text\"}," +
    "    \"price\": {\"type\": \"double\"}," +
    "    \"description\": {\"type\": \"text\"}" +
    "  }" +
    "}" +
"}";


// 准备请求参数
request.source(MAPPING_TIMPLATE, XContentType.JSON);

mappingTemplate 这里包含了定义的字段和它们的类型。source() 方法将这个 JSON 字符串设置为请求体,并指定内容类型为 JSON。

发送请求

最后,使用 ElasticsearchClient 执行创建操作:

// 发送请求
client.indices().create(request, RequestOptions.DEFAULT);



查询索引库

创建请求对象

查询索引库的请求对象使用 GetIndexRequest 类。我们只需要传递索引库的名称即可。

// 准备Request请求
GetIndexRequest request = new GetIndexRequest("items");
发送请求

查询索引库的操作使用 client.indices().get() 方法:

// 发送请求
GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);

GetIndexResponse 返回查询结果。可以通过它获取索引库的配置信息。

检查索引库是否存在

通常情况下,我们不需要获取完整的索引库信息,只需判断索引库是否存在。可以使用 exists() 方法:

boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);

此方法会返回一个布尔值,表示索引库是否存在。



删除索引库

创建请求对象

删除索引库的请求对象使用 DeleteIndexRequest 类,指定要删除的索引库名称。

// 准备Request对象
DeleteIndexRequest request = new DeleteIndexRequest("items");
发送请求

删除索引库的操作使用 client.indices().delete() 方法:

// 发送请求
client.indices().delete(request, RequestOptions.DEFAULT);



增删改查操作的 API 方法对比

操作请求对象类方法名返回值说明
创建索引库CreateIndexRequestclient.indices().create()CreateIndexResponse用于创建索引库
查询索引库GetIndexRequestclient.indices().get()GetIndexResponse获取索引库的详细信息
检查索引库GetIndexRequestclient.indices().exists()boolean判断索引库是否存在
删除索引库DeleteIndexRequestclient.indices().delete()DeleteIndexResponse删除指定的索引库



代码示例

创建索引库代码示例
// 注意导包应该是这个,否则会报错
import org.elasticsearch.client.indices.CreateIndexRequest;
@Test
void testCreateIndex() throws IOException {
    // 准备Request对象
    CreateIndexRequest request = new CreateIndexRequest("items");
    // 准备请求参数
    request.source(MAPPING_TIMPLATE, XContentType.JSON);
    // 发送请求
    client.indices().create(request, RequestOptions.DEFAULT);
}


// 声明请求参数
private static String MAPPING_TIMPLATE = "{\n" +
            "  \"mappings\": {\n" +
            "    \"properties\": {\n" +
            "      \"id\": {\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"name\": {\n" +
            "        \"type\": \"text\",\n" +
            "        \"analyzer\": \"ik_smart\"\n" +
            "      },\n" +
            "      \"price\": {\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"image\": {\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"index\": false\n" +
            "      },\n" +
            "      \"category\": {\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"brand\": {\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"sold\": {\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"commentCount\": {\n" +
            "        \"type\": \"integer\",\n" +
            "        \"index\": false\n" +
            "      },\n" +
            "      \"isAD\": {\n" +
            "        \"type\": \"boolean\"\n" +
            "      },\n" +
            "      \"updateTime\": {\n" +
            "        \"type\": \"date\"\n" +
            "      }\n" +
            "    }\n" +
            "  }\n" +
            "}";

测试结果:



查询索引库代码示例
 @Test
void testGetIndex() throws IOException {
    // 准备Request请求
    GetIndexRequest request = new GetIndexRequest("items");
    // 发送请求
    boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
    System.out.println("exists = " + exists);
}

测试结果:



删除索引库代码示例
@Test
void testDeleteIndex() throws IOException {
    // 准备Request对象
    DeleteIndexRequest request = new DeleteIndexRequest("items");
    // 发送请求
    client.indices().delete(request, RequestOptions.DEFAULT);
}

测试结果:



总结

通过 Elasticsearch 的 Java API,可以高效地对索引库进行增删改查操作。主要的操作包括:

  • 创建索引库:通过 CreateIndexRequestsource() 设置映射,然后使用 create() 方法发送请求。
  • 查询索引库:通过 GetIndexRequest 获取索引库信息,使用 get()exists() 判断索引库是否存在。
  • 删除索引库:通过 DeleteIndexRequest 删除索引库,使用 delete() 方法发送请求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值