松哥原创的 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 基础知识:
-
打算出一个 ElasticSearch 教程,谁赞成,谁反对?
-
ElasticSearch 从安装开始
-
ElasticSearch 第三弹,核心概念介绍
-
ElasticSearch 中的中文分词器该怎么玩?
-
ElasticSearch 索引基本操作
-
ElasticSearch 文档的添加、获取以及更新
-
ElasticSearch 文档的删除和批量操作
-
ElasticSearch 文档路由,你的数据到底存在哪一个分片上?
-
ElasticSearch 并发的处理方式:锁和版本控制
-
ElasticSearch 中的倒排索引到底是什么?
-
ElasticSearch 动态映射与静态映射
-
ElasticSearch 四种字段类型详解
-
ElasticSearch 中的地理类型和特殊类型
-
ElasticSearch 23 种映射参数详解
-
ElasticSearch 如何配置某个字段的权重?
-
ElasticSearch 23 种映射参数详解【3】
-
ElasticSearch 映射模版
-
ElasticSearch 搜索入门
-
ElasticSearch 全文搜索怎么玩?
-
ElasticSearch 打错字还能搜索到?试试 fuzzy query!
-
ElasticSearch 复合查询,理解 Es 中的文档评分策略!
-
想搜索附近评分较高的餐厅,ElasticSearch 大显身手!
-
ElasticSearch 如何像 MySQL 一样做多表联合查询?
-
ElasticSearch 地理位置查询与特殊查询
-
ElasticSearch 搜索高亮与排序
-
ElasticSearch 指标聚合
-
ElasticSearch 桶聚合
-
ElasticSearch 管道聚合
-
Java 操作 ElasticSearch,so easy!
1
50+ 需求文档免费下载!
2
Spring Security 教程合集
3
接了两个私活,都是血汗钱
本文分享自微信公众号 - 江南一点雨(a_javaboy)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
相关文章
暂无评论...