松哥原创的 Spring Boot 视频教程已经杀青,感兴趣的小伙伴戳这里-->Spring Boot Vue 微人事视频教程
今天我们来继续看 ElasticSearch 中的 Java 高级客户端,来看看 RestHighLevelClient 如何操作索引?
索引基本操作:
索引别名管理:
以下是视频笔记:
注意,笔记只是视频内容的一个简要记录,因此笔记内容比较简单,完整的内容可以查看视频。
28.1.2 查询索引是否存在
代码语言:javascript复制public class HighLevelTest3 {
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")
));
GetIndexRequest blog = new GetIndexRequest("blog2");
boolean exists = client.indices().exists(blog, RequestOptions.DEFAULT);
System.out.println("exists = " exists);
//关闭 client
client.close();
}
}
28.1.3 关闭/打开索引
关闭:
代码语言:javascript复制public class HighLevelTest4 {
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")
));
CloseIndexRequest blog = new CloseIndexRequest("blog");
CloseIndexResponse close = client.indices().close(blog, RequestOptions.DEFAULT);
List<CloseIndexResponse.IndexResult> indices = close.getIndices();
for (CloseIndexResponse.IndexResult index : indices) {
System.out.println("index.getIndex() = " index.getIndex());
}
//关闭 client
client.close();
}
}
打开:
代码语言:javascript复制public class HighLevelTest4 {
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")
));
OpenIndexRequest blog = new OpenIndexRequest("blog");
client.indices().open(blog, RequestOptions.DEFAULT);
//关闭 client
client.close();
}
}
28.1.4 索引修改
代码语言:javascript复制public class HighLevelTest5 {
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")
));
UpdateSettingsRequest request = new UpdateSettingsRequest("blog");
request.settings(Settings.builder().put("index.blocks.write", true).build());
client.indices().putSettings(request, RequestOptions.DEFAULT);
//关闭 client
client.close();
}
}
28.1.5 克隆索引
被克隆的索引需要是只读索引,可以通过 28.1.4 小节中的方式设置索引为只读。
代码语言:javascript复制public class HighLevelTest6 {
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")
));
ResizeRequest request = new ResizeRequest("blog2", "blog");
client.indices().clone(request, RequestOptions.DEFAULT);
//关闭 client
client.close();
}
}
28.1.6 查看索引
代码语言:javascript复制public class HighLevelTest7 {
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")
));
GetSettingsRequest request = new GetSettingsRequest().indices("blog");
//设置需要互殴去的具体的参数,不设置则返回所有参数
request.names("index.blocks.write");
GetSettingsResponse response = client.indices().getSettings(request, RequestOptions.DEFAULT);
ImmutableOpenMap<String, Settings> indexToSettings = response.getIndexToSettings();
System.out.println(indexToSettings);
String s = response.getSetting("blog", "index.number_of_replicas");
System.out.println(s);
//关闭 client
client.close();
}
}
28.1.7 Refresh & Flush
Es 底层依赖 Lucene,而 Lucene 中有 reopen 和 commit 两种操作,还有一个特殊的概念叫做 segment。
Es 中,基本的存储单元是 shard,对应到 Lucene 上,就是一个索引,Lucene 中的索引由 segment 组成,每个 segment 相当于 es 中的倒排索引。每个 es 文档创建时,都会写入到一个新的 segment 中,删除文档时,只是从属于它的 segment 处标记为删除,并没有从磁盘中删除。
Lucene 中:
reopen 可以让数据搜索到,但是不保证数据被持久化到磁盘中。
commit 可以让数据持久化。
Es 中:
默认是每秒 refresh 一次(Es 中文档被索引之后,首先添加到内存缓冲区,refresh 操作将内存缓冲区中的数据拷贝到新创建的 segment 中,这里是在内存中操作的)。
flush 将内存中的数据持久化到磁盘中。一般来说,flush 的时间间隔比较久,默认 30 分钟。
代码语言:javascript复制public class HighLevelTest8 {
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")
));
RefreshRequest request = new RefreshRequest("blog");
client.indices().refresh(request, RequestOptions.DEFAULT);
FlushRequest flushRequest = new FlushRequest("blog");
client.indices().flush(flushRequest, RequestOptions.DEFAULT);
//关闭 client
client.close();
}
}
28.1.9 索引别名
索引的别名类似于 MySQL 中的视图。
28.1.9.1 添加别名
添加一个普通的别名:
代码语言:javascript复制public class HighLevelTest9 {
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")
));
IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest();
IndicesAliasesRequest.AliasActions aliasAction = new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.ADD);
aliasAction.index("books").alias("books_alias");
indicesAliasesRequest.addAliasAction(aliasAction);
client.indices().updateAliases(indicesAliasesRequest, RequestOptions.DEFAULT);
//关闭 client
client.close();
}
}
添加一个带 filter 的别名:
代码语言:javascript复制public class HighLevelTest9 {
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")
));
IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest();
IndicesAliasesRequest.AliasActions aliasAction = new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.ADD);
aliasAction.index("books").alias("books_alias2").filter("{"term": {"name": "java"}}");
indicesAliasesRequest.addAliasAction(aliasAction);
client.indices().updateAliases(indicesAliasesRequest, RequestOptions.DEFAULT);
//关闭 client
client.close();
}
}
现在,books 索引将存在两个别名,其中,books_alias2 自动过滤 name 中含有 java 的文档。
28.1.9.2 删除别名
代码语言:javascript复制public class HighLevelTest9 {
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")
));
IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest();
IndicesAliasesRequest.AliasActions aliasAction = new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.REMOVE);
aliasAction.index("books").alias("books_alias");
indicesAliasesRequest.addAliasAction(aliasAction);
client.indices().updateAliases(indicesAliasesRequest, RequestOptions.DEFAULT);
//关闭 client
client.close();
}
}
第二种移除方式:
代码语言:javascript复制public class HighLevelTest9 {
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")
));
DeleteAliasRequest deleteAliasRequest = new DeleteAliasRequest("books", "books_alias2");
client.indices().deleteAlias(deleteAliasRequest, RequestOptions.DEFAULT);
//关闭 client
client.close();
}
}
28.1.9.3 判断别名是否存在
代码语言:javascript复制public class HighLevelTest9 {
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")
));
GetAliasesRequest books_alias = new GetAliasesRequest("books_alias");
//指定查看某一个索引的别名,不指定,则会搜索所有的别名
books_alias.indices("books");
boolean b = client.indices().existsAlias(books_alias, RequestOptions.DEFAULT);
System.out.println(b);
//关闭 client
client.close();
}
}
28.1.9.4 获取别名
代码语言:javascript复制public class HighLevelTest9 {
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")
));
GetAliasesRequest books_alias = new GetAliasesRequest("books_alias");
//指定查看某一个索引的别名,不指定,则会搜索所有的别名
books_alias.indices("books");
GetAliasesResponse response = client.indices().getAlias(books_alias, RequestOptions.DEFAULT);
Map<String, Set<AliasMetadata>> aliases = response.getAliases();
System.out.println("aliases = " aliases);
//关闭 client
client.close();
}
}