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

本文涉及的产品
Elasticsearch Serverless通用抵扣包,测试体验金 200元
简介: ElasticSearch7.6.x 模板及滚动索引创建及注意事项

@[TOC]

声明:

==注意点1:滚动索引是设置索引,而非创建索引,且设置一次结果返回 "rolled_over" : true,则会按照设定规则创建新索引,名字递增,而非一次设置永久有效==

==注意点2:设置滚动索引会出现两个别名,一个读取别名(在模板中定义),一个写入别名(在创建index中指定)==
image.png
image.png
image.png

举例说明 创建模板+设置滚动索引+读写

判断模板是否存在

image.png

try {
   
    IndexTemplatesExistRequest request = new IndexTemplatesExistRequest("test-logs");
    boolean exists = client.indices().existsTemplate(request, RequestOptions.DEFAULT);
    System.out.println("模板是否存在:" + exists );
} catch (IOException e) {
   
    e.printStackTrace();
}

创建模板

image.png

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);
}

应用模板创建索引

image.png

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);
}

设置滚动索引

image.png

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);
}

添加文档,使用“写”别名

image.png

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);
}

查询,使用“读”别名

image.png

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

相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。 &nbsp;
目录
相关文章
|
自然语言处理 大数据 应用服务中间件
大数据-172 Elasticsearch 索引操作 与 IK 分词器 自定义停用词 Nginx 服务
大数据-172 Elasticsearch 索引操作 与 IK 分词器 自定义停用词 Nginx 服务
235 5
|
存储 分布式计算 大数据
大数据-169 Elasticsearch 索引使用 与 架构概念 增删改查
大数据-169 Elasticsearch 索引使用 与 架构概念 增删改查
182 3
|
存储 API 数据库
检索服务elasticsearch索引(Index)
【8月更文挑战第23天】
575 6
|
11月前
|
存储 缓存 监控
优化Elasticsearch 索引设计
优化Elasticsearch 索引设计
208 5
|
11月前
|
存储 JSON 关系型数据库
Elasticsearch 索引
【11月更文挑战第3天】
232 4
|
JSON 自然语言处理 数据库
ElasticSearch基础1——索引和文档。Kibana,RestClient操作索引和文档+黑马旅游ES库导入
概念、ik分词器、倒排索引、索引和文档的增删改查、RestClient对索引和文档的增删改查
ElasticSearch基础1——索引和文档。Kibana,RestClient操作索引和文档+黑马旅游ES库导入
|
存储 搜索推荐 数据建模
Elasticsearch 的数据建模与索引设计
【9月更文第3天】Elasticsearch 是一个基于 Lucene 的搜索引擎,广泛应用于全文检索、数据分析等领域。为了确保 Elasticsearch 的高效运行,合理的数据建模和索引设计至关重要。本文将探讨如何为不同的应用场景设计高效的索引结构,并分享一些数据建模的最佳实践。
491 2
|
JSON 自然语言处理 数据库
Elasticsearch从入门到项目部署 安装 分词器 索引库操作
这篇文章详细介绍了Elasticsearch的基本概念、倒排索引原理、安装部署、IK分词器的使用,以及如何在Elasticsearch中进行索引库的CRUD操作,旨在帮助读者从入门到项目部署全面掌握Elasticsearch的使用。
|
5月前
|
JSON 安全 数据可视化
Elasticsearch(es)在Windows系统上的安装与部署(含Kibana)
Kibana 是 Elastic Stack(原 ELK Stack)中的核心数据可视化工具,主要与 Elasticsearch 配合使用,提供强大的数据探索、分析和展示功能。elasticsearch安装在windows上一般是zip文件,解压到对应目录。文件,elasticsearch8.x以上版本是自动开启安全认证的。kibana安装在windows上一般是zip文件,解压到对应目录。elasticsearch的默认端口是9200,访问。默认用户是elastic,密码需要重置。
2483 0
|
6月前
|
安全 Java Linux
Linux安装Elasticsearch详细教程
Linux安装Elasticsearch详细教程
1003 1