0

I'm using Java High level rest client in my project and i want to limit my indices based on document count. I'm using rollover api but its not creating indices automatically. The code will be given below.

I'm creating index pattern so that my custom analyzer will be applied to all other indices that follow the respective pattern.

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

My Rollover code. Here i want to rollover one when index gets one or document.

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

But the code is not working and the rollover api is not creating indices automatically. All the 2 documents are stored only in test_log-1 index. Is there any mistake in my code?

Thanks

1 Answers1

0

Note: Rollover will not happen automatically. Elasticsearch tries to rollover the index on getting a rollover request.

For example consider the following sequences:

  1. You have a new index test_log-1 which is empty and is being pointed by the alias temp_alias_new.
  2. If you try to rollover now, none of the conditions mentioned along with rollover request holds good as the index is new and empty. So this time rollover fails.
  3. Add some documents to the index.
  4. Now try the rollover with the condition maxIndexDocsCondition(1), it will rollover. Because the condition holds good.

Update

With the latest release of elastic search, you can use ILM (Index Life cycle Management) to automate the rollover. Here is doc link for more info: https://www.elastic.co/guide/en/elasticsearch/reference/7.x/getting-started-index-lifecycle-management.html

Kashinath
  • 166
  • 4
  • how to make the rollover happen automatically.In my case i'm indexing lot of documents, so hitting rollover api everytime will be time consuming, Is there any way to rollover the index automatically? – Ajay Venkatesh Aug 05 '21 at 06:28
  • I just went through latest docs of elastic search. To do automatic rollover, you can use the ILM (Index Lifecycle Management) Here is the document link: https://www.elastic.co/guide/en/elasticsearch/reference/7.x/getting-started-index-lifecycle-management.html – Kashinath Aug 05 '21 at 06:47
  • Can we able to implement this using java high level rest client? – Ajay Venkatesh Aug 05 '21 at 07:11
  • Yes, you can get the respective java API in RestHighLevelClient. I suggest you to read the documentation properly. https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_index_lifecycle_management_apis.html – Kashinath Aug 06 '21 at 04:33