11

ElasticSearch Java 高级客户端索引操作~

 3 years ago
source link: https://mp.weixin.qq.com/s?__biz=MzI1NDY0MTkzNQ%3D%3D&%3Bmid=2247491945&%3Bidx=1&%3Bsn=d41b55ed6c083990f0ce3aa3eb5999de
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

松哥原创的 Spring Boot 视频教程已经杀青,感兴趣的小伙伴戳这里--> Spring Boot+Vue+微人事视频教程

继续 Es 客户端~今天我们来看看 high level rest client ~

以下是视频笔记:

注意,笔记只是视频内容的一个简要记录,因此笔记内容比较简单,完整的内容可以查看视频。

28.1 索引管理

28.1.1 创建索引

首先创建一个普通的 Maven 项目,然后引入 high level rest client 依赖:

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

需要注意,依赖的版本和 Es 的版本要对应。

创建一个索引:

public class HighLevelTest {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http"),
new HttpHost("localhost", 9202, "http")
));
//删除已经存在的索引
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("blog");
client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
//创建一个索引
CreateIndexRequest blog1 = new CreateIndexRequest("blog");
//配置 settings,分片、副本等信息
blog1.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 2));
//配置字段类型,字段类型可以通过 JSON 字符串、Map 以及 XContentBuilder 三种方式来构建
//json 字符串的方式
blog1.mapping("{\"properties\": {\"title\": {\"type\": \"text\"}}}", XContentType.JSON);
//执行请求,创建索引
client.indices().create(blog1, RequestOptions.DEFAULT);
//关闭 client
client.close();
}
}

mapping 的配置,还有另外两种方式:

第一种,通过 map 构建 mapping:

public class HighLevelTest {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http"),
new HttpHost("localhost", 9202, "http")
));
//删除已经存在的索引
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("blog");
client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
//创建一个索引
CreateIndexRequest blog1 = new CreateIndexRequest("blog");
//配置 settings,分片、副本等信息
blog1.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 2));
//配置字段类型,字段类型可以通过 JSON 字符串、Map 以及 XContentBuilder 三种方式来构建
//json 字符串的方式
// blog1.mapping("{\"properties\": {\"title\": {\"type\": \"text\"}}}", XContentType.JSON);
//map 的方式
Map<String, String> title = new HashMap<>();
title.put("type", "text");
Map<String, Object> properties = new HashMap<>();
properties.put("title", title);
Map<String, Object> mappings = new HashMap<>();
mappings.put("properties", properties);
blog1.mapping(mappings);
//执行请求,创建索引
client.indices().create(blog1, RequestOptions.DEFAULT);
//关闭 client
client.close();
}
}

第二种,通过 XContentBuilder 构建 mapping:

public class HighLevelTest {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http"),
new HttpHost("localhost", 9202, "http")
));
//删除已经存在的索引
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("blog");
client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
//创建一个索引
CreateIndexRequest blog1 = new CreateIndexRequest("blog");
//配置 settings,分片、副本等信息
blog1.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 2));
//配置字段类型,字段类型可以通过 JSON 字符串、Map 以及 XContentBuilder 三种方式来构建
//json 字符串的方式
// blog1.mapping("{\"properties\": {\"title\": {\"type\": \"text\"}}}", XContentType.JSON);
//map 的方式
// Map<String, String> title = new HashMap<>();
// title.put("type", "text");
// Map<String, Object> properties = new HashMap<>();
// properties.put("title", title);
// Map<String, Object> mappings = new HashMap<>();
// mappings.put("properties", properties);
// blog1.mapping(mappings);
//XContentBuilder 方式
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
builder.startObject("properties");
builder.startObject("title");
builder.field("type", "text");
builder.endObject();
builder.endObject();
builder.endObject();
blog1.mapping(builder);
//执行请求,创建索引
client.indices().create(blog1, RequestOptions.DEFAULT);
//关闭 client
client.close();
}
}

还可以给索引配置别名:

public class HighLevelTest {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http"),
new HttpHost("localhost", 9202, "http")
));
//删除已经存在的索引
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("blog");
client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
//创建一个索引
CreateIndexRequest blog1 = new CreateIndexRequest("blog");
//配置 settings,分片、副本等信息
blog1.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 2));
//配置字段类型,字段类型可以通过 JSON 字符串、Map 以及 XContentBuilder 三种方式来构建
//json 字符串的方式
// blog1.mapping("{\"properties\": {\"title\": {\"type\": \"text\"}}}", XContentType.JSON);
//map 的方式
// Map<String, String> title = new HashMap<>();
// title.put("type", "text");
// Map<String, Object> properties = new HashMap<>();
// properties.put("title", title);
// Map<String, Object> mappings = new HashMap<>();
// mappings.put("properties", properties);
// blog1.mapping(mappings);
//XContentBuilder 方式
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
builder.startObject("properties");
builder.startObject("title");
builder.field("type", "text");
builder.endObject();
builder.endObject();
builder.endObject();
blog1.mapping(builder);
//配置别名
blog1.alias(new Alias("blog_alias"));
//执行请求,创建索引
client.indices().create(blog1, RequestOptions.DEFAULT);
//关闭 client
client.close();
}
}

如果觉得调 API 太麻烦,也可以直接上 JSON:

public class HighLevelTest2 {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http"),
new HttpHost("localhost", 9202, "http")
));
//删除已经存在的索引
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("blog");
client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
//创建一个索引
CreateIndexRequest blog1 = new CreateIndexRequest("blog");
//直接同构 JSON 配置索引
blog1.source("{\"settings\": {\"number_of_shards\": 3,\"number_of_replicas\": 2},\"mappings\": {\"properties\": {\"title\": {\"type\": \"keyword\"}}},\"aliases\": {\"blog_alias_javaboy\": {}}}", XContentType.JSON);
//执行请求,创建索引
client.indices().create(blog1, RequestOptions.DEFAULT);
//关闭 client
client.close();
}
}

另外还有一些其他的可选配置:

public class HighLevelTest2 {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http"),
new HttpHost("localhost", 9202, "http")
));
//删除已经存在的索引
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("blog");
client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
//创建一个索引
CreateIndexRequest blog1 = new CreateIndexRequest("blog");
//直接同构 JSON 配置索引
blog1.source("{\"settings\": {\"number_of_shards\": 3,\"number_of_replicas\": 2},\"mappings\": {\"properties\": {\"title\": {\"type\": \"keyword\"}}},\"aliases\": {\"blog_alias_javaboy\": {}}}", XContentType.JSON);
//请求超时时间,连接所有节点的超时时间
blog1.setTimeout(TimeValue.timeValueMinutes(2));
//连接 master 节点的超时时间
blog1.setMasterTimeout(TimeValue.timeValueMinutes(1));
//执行请求,创建索引
client.indices().create(blog1, RequestOptions.DEFAULT);
//关闭 client
client.close();
}
}

前面所有的请求都是同步的,会阻塞的,也可以异步:

public class HighLevelTest2 {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http"),
new HttpHost("localhost", 9202, "http")
));
//删除已经存在的索引
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("blog");
client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
//创建一个索引
CreateIndexRequest blog1 = new CreateIndexRequest("blog");
//直接同构 JSON 配置索引
blog1.source("{\"settings\": {\"number_of_shards\": 3,\"number_of_replicas\": 2},\"mappings\": {\"properties\": {\"title\": {\"type\": \"keyword\"}}},\"aliases\": {\"blog_alias_javaboy\": {}}}", XContentType.JSON);
//请求超时时间,连接所有节点的超时时间
blog1.setTimeout(TimeValue.timeValueMinutes(2));
//连接 master 节点的超时时间
blog1.setMasterTimeout(TimeValue.timeValueMinutes(1));
//执行请求,创建索引
// client.indices().create(blog1, RequestOptions.DEFAULT);
//异步创建索引
client.indices().createAsync(blog1, RequestOptions.DEFAULT, new ActionListener<CreateIndexResponse>() {
//请求成功
@Override
public void onResponse(CreateIndexResponse createIndexResponse) {
//关闭 client
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}

//请求失败
@Override
public void onFailure(Exception e) {

}
});
//关闭 client
// client.close();
}
}

ElasticSearch 基础知识:

  1. 打算出一个 ElasticSearch 教程,谁赞成,谁反对?

  2. ElasticSearch 从安装开始

  3. ElasticSearch 第三弹,核心概念介绍

  4. ElasticSearch 中的中文分词器该怎么玩?

  5. ElasticSearch 索引基本操作

  6. ElasticSearch 文档的添加、获取以及更新

  7. ElasticSearch 文档的删除和批量操作

  8. ElasticSearch 文档路由,你的数据到底存在哪一个分片上?

  9. ElasticSearch 并发的处理方式:锁和版本控制

  10. ElasticSearch 中的倒排索引到底是什么?

  11. ElasticSearch 动态映射与静态映射

  12. ElasticSearch 四种字段类型详解

  13. ElasticSearch 中的地理类型和特殊类型

  14. ElasticSearch 23 种映射参数详解

  15. ElasticSearch 如何配置某个字段的权重?

  16. ElasticSearch 23 种映射参数详解【3】

  17. ElasticSearch 映射模版

  18. ElasticSearch 搜索入门

  19. ElasticSearch 全文搜索怎么玩?

  20. ElasticSearch 打错字还能搜索到?试试 fuzzy query!

  21. ElasticSearch 复合查询,理解 Es 中的文档评分策略!

  22. 想搜索附近评分较高的餐厅,ElasticSearch 大显身手!

  23. ElasticSearch 如何像 MySQL 一样做多表联合查询?

  24. ElasticSearch 地理位置查询与特殊查询

  25. ElasticSearch 搜索高亮与排序

  26. ElasticSearch 指标聚合

  27. ElasticSearch 桶聚合

  28. ElasticSearch 管道聚合

  29. Java 操作 ElasticSearch,so easy!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK