【问题标题】:Rollover api is not creating indices automatically - Java rest high level clientRollover api 不会自动创建索引 - Java REST 高级客户端
【发布时间】:2021-10-09 18:47:30
【问题描述】:

我在我的项目中使用 Java 高级别的休息客户端,我想根据文档数量限制我的索引。我正在使用翻转 api,但它不会自动创建索引。 下面会给出代码。

我正在创建索引模式,以便我的自定义分析器将应用于遵循相应模式的所有其他索引。

PutIndexTemplateRequest request = new PutIndexTemplateRequest("testingtemplate");
                request.source("{\n" +
                "   \"index_patterns\":[\n" +
                "        \"test_log-*\"\n" +
                "  ],\n" +
                "       \"settings\": {\n" +
                "           \"analysis\": {\n" +
                "               \"analyzer\": { \n" +
                "                   \"my_analyzer\": {\n" +
                "                       \"type\": \"custom\",\n" +
                "                       \"tokenizer\": \"whitespace\",\n" +
                "                       \"filter\": []\n" +
                "                       }\n" +
                "                   }\n" +
                "               }\n" +
                "           },\n" +
                "           \"mappings\": {\n" +
                    "           \"properties\": {\n" +
                    "               \"fullLog\": {\n" +
                    "                   \"type\": \"text\",\n" +
                    "                   \"analyzer\": \"my_analyzer\"\n" +
                    "               }\n" +
                    "           }\n" +
                "           }\n" +
                "    }",XContentType.JSON);
                return client.indices().putTemplate(request,RequestOptions.DEFAULT).isAcknowledged();

我的翻转码。在这里,我想在索引获取一个或文档时翻转一个。

final RestHighLevelClient client = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http")));
            boolean isIndexTemplateCreated = createIndexTemplate(client);
            System.out.println(isIndexTemplateCreated);

            CreateIndexRequest request = new CreateIndexRequest("test_log-1");
            request.alias(new Alias("temp_alias_new"));  
            CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);

            RolloverRequest roll_req = new RolloverRequest("temp_alias_new",null); 
            roll_req.addMaxIndexAgeCondition(new TimeValue(7, TimeUnit.DAYS)); 
            roll_req.addMaxIndexDocsCondition(1); 
            roll_req.addMaxIndexSizeCondition(new ByteSizeValue(5, ByteSizeUnit.GB));
            RolloverResponse rolloverResponse = client.indices().rollover(roll_req, RequestOptions.DEFAULT);

            Map<String,Object> map = new HashMap<>();
            map.put("fullLog","Hi");
            client.index(new IndexRequest("temp_alias_new").source(map), RequestOptions.DEFAULT);
            map.put("fullLog","hello");
            client.index(new IndexRequest("temp_alias_new").source(map), RequestOptions.DEFAULT);

但是代码不工作并且翻转 api 没有自动创建索引。所有 2 个文档仅存储在 test_log-1 索引中。 我的代码有错误吗?

谢谢

【问题讨论】:

    标签: java elasticsearch elasticsearch-java-api resthighlevelclient


    【解决方案1】:

    注意:翻转不会自动发生。 Elasticsearch 会在收到翻转请求时尝试翻转索引。

    例如考虑以下序列:

    1. 您有一个新索引 test_log-1,它是空的,并由别名 temp_alias_new 指向。
    2. 如果您现在尝试翻转,则与翻转请求一起提到的条件都不适用,因为索引是新的且为空的。所以这次翻转失败了。
    3. 将一些文档添加到索引中。
    4. 现在尝试使用条件maxIndexDocsCondition(1) 进行翻转,它将翻转。因为条件很好。

    更新

    使用最新版本的弹性搜索,您可以使用 ILM(索引生命周期管理)来自动化滚动。 这是更多信息的文档链接:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/getting-started-index-lifecycle-management.html

    【讨论】:

    • 如何使翻转自动发生。在我的情况下,我正在索引很多文档,所以每次点击翻转 api 会很耗时,有没有办法自动翻转索引?
    • 我刚刚浏览了最新的弹性搜索文档。要做自动翻转,可以使用 ILM(索引生命周期管理) 这里是文档链接:elastic.co/guide/en/elasticsearch/reference/7.x/…
    • 我们可以使用java高级rest客户端来实现吗?
    • 是的,您可以在 RestHighLevelClient 中获取相应的 java API。我建议您正确阅读文档。 elastic.co/guide/en/elasticsearch/client/java-rest/current/…
    猜你喜欢
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 2019-04-13
    相关资源
    最近更新 更多