ElasticSearch7.6.x 模板及滚动索引创建及注意事项
@[TOC]
声明:
==注意点1:滚动索引是设置索引,而非创建索引,且设置一次结果返回 “rolled_over” : true,则会按照设定规则创建新索引,名字递增,而非一次设置永久有效==
==注意点2:设置滚动索引会出现两个别名,一个读取别名(在模板中定义),一个写入别名(在创建index中指定)==
举例说明 创建模板+设置滚动索引+读写
判断模板是否存在
try {
IndexTemplatesExistRequest request = new IndexTemplatesExistRequest("test-logs");
boolean exists = client.indices().existsTemplate(request, RequestOptions.DEFAULT);
System.out.println("模板是否存在:" + exists );
} catch (IOException e) {
e.printStackTrace();
}
创建模板
try {
PutIndexTemplateRequest request = new PutIndexTemplateRequest("test-logs");
request.patterns(Arrays.asList("test-logs-*"));
request.alias(new Alias("test-logs-read"));
request.source("{\n" +
" \"settings\" : {\n" +
" \"number_of_shards\" : 5,\n" +
" \"number_of_replicas\" : 1,\n" +
"\"analysis\": {" +
"\"analyzer\": {" +
"\"my_analyzer\": {" +
"\"type\": \"pattern\"," +
"\"pattern\":[\"_\",\"-\"]" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"mappings\" : {\n" +
" \"properties\" : {\n" +
" \"file_name\" : { \"type\" : \"text\" },\n" +
" \"table\" : { \"type\" : \"text\",\"analyzer\": \"my_analyzer\" },\n" +
" \"update_time\" : { \"type\" : \"long\", \"index\": false}" +
" }\n" +
" }\n" +
" }\n" +
"}", XContentType.JSON);
// 3.发送请求
AcknowledgedResponse putTemplateResponse = client.indices().putTemplate(request, RequestOptions.DEFAULT);
System.out.println("创建模板成功!");
System.out.println("response:" + putTemplateResponse );
System.out.println("isAcknowledged:" + putTemplateResponse.isAcknowledged() );
} catch (IOException e) {
log.error("IOException:", e);
}
应用模板创建索引
try {
// 1.创建索引名
CreateIndexRequest request = new CreateIndexRequest("test-logs-100000");
request.alias(new Alias("test-logs-write"));
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println("创建索引库成功!");
System.out.println("response:" + response);
} catch (IOException e) {
log.error("IOException:{}", e);
} catch (ElasticsearchStatusException e) {
log.error("ElasticsearchStatusException:{}", e);
}
设置滚动索引
try {
RolloverRequest request = new RolloverRequest("test-logs-write", null);
request.addMaxIndexAgeCondition(new TimeValue(7, TimeUnit.DAYS));
request.addMaxIndexDocsCondition(2);
request.addMaxIndexSizeCondition(new ByteSizeValue(5, ByteSizeUnit.GB));
RolloverResponse rolloverResponse = client.indices().rollover(request, RequestOptions.DEFAULT);
System.out.println("设置滚动索引成功!");
System.out.println("isRolledOver:" + rolloverResponse.isRolledOver());
} catch (IOException e) {
log.error("IOException:{}", e);
}
添加文档,使用“写”别名
try {
// 1、创建索引请求
IndexRequest request = new IndexRequest("test-logs-write", "_doc");
// 2、准备文档数据
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("file_name", "财政部二月份文件");
jsonMap.put("table", "103_table-111");
jsonMap.put("update_time", System.currentTimeMillis());
request.source(jsonMap, XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
//3、发送请求 同步方式
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
System.out.println("添加数据成功!");
System.out.println("indexResponse:" + indexResponse);
System.out.println(indexResponse.status());
} catch (IOException e) {
log.error("出现异常:{}", e);
}
查询,使用“读”别名
try {
// 1、创建search请求
SearchRequest searchRequest = new SearchRequest("test-logs-read");
searchRequest.types("_doc");
// 2、用SearchSourceBuilder来构造查询请求体 ,请仔细查看它的方法,构造各种查询的方法都在这。
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.from(0);
sourceBuilder.size(10);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
sourceBuilder.sort(new FieldSortBuilder("update_time").order(SortOrder.DESC));
MultiMatchQueryBuilder matchQueryBuilder = QueryBuilders.multiMatchQuery("103", "table").fuzziness(Fuzziness.AUTO);
sourceBuilder.query(matchQueryBuilder);
//3. 高亮设置
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.requireFieldMatch(false).field("table").preTags("<font color=red>").postTags("</font>");
sourceBuilder.highlighter(highlightBuilder);
//4.将请求体加入到请求中
searchRequest.source(sourceBuilder);
//5、发送请求
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
System.out.println("检索数据成功!");
System.out.println(searchResponse);
System.out.println(hits);
System.out.println("num:" + searchHits.length);
for (SearchHit hit : searchHits) {
// String source = hit.getSourceAsString();
// System.out.println(source);
System.out.println(hit);
}
} catch (IOException e) {
log.error("出现异常:{}", e);
}
本人先关其他文章链接
1.ElasticSearch7.6.x 模板及滚动索引创建及注意事项
https://blog.csdn.net/a924382407/article/details/115082265
2.ElasticSearch的IK分词器
https://blog.csdn.net/a924382407/article/details/117255506
3.ElasticSearch核心概念:倒排索引
https://blog.csdn.net/a924382407/article/details/117255449
4.springboot集成ElasticSearch使用completion实现补全功能
https://blog.csdn.net/a924382407/article/details/115868167
5.ES Restful API讲解使用
https://blog.csdn.net/a924382407/article/details/115085022
6.ES API,使用Kibana的开发工具用例说明
https://blog.csdn.net/a924382407/article/details/115084549
- 点赞
- 收藏
- 关注作者
评论(0)