ElasticSearch7.6.x 模板及滚动索引创建及注意事项

声明:

注意点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

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘大猫.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值