Spring boot implements Elasticsearch index creation in advance by day

There is such a scenario that Elasticsearch indexes need to be created day by day in advance.

Below is a basic Spring Boot application that automatically creates an Elasticsearch index when the application starts, and also automatically creates new indexes at a fixed time every day. The Elasticsearch version used in this example is 7.5.1.

 
  
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
@SpringBootApplication
@EnableScheduling
public class Application {
    @Autowired
    private RestHighLevelClient client;
    private static final String INDEX_PREFIX = "my-index-";
    private static final String INDEX_MAPPING = "{\n" +
            "  \"mappings\": {\n" +
            "    \"properties\": {\n" +
            "      \"id\": {\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"name\": {\n" +
            "        \"type\": \"text\"\n" +
            "      },\n" +
            "      \"date\": {\n" +
            "        \"type\": \"date\",\n" +
            "        \"format\": \"yyyy-MM-dd\"\n" +
            "      }\n" +
            "    }\n" +
            "  }\n" +
            "}";
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    @Scheduled(cron = "0 0 0 * * *") // 每天0点创建新的索引
    public void createIndex() throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String indexName = INDEX_PREFIX + sdf.format(new Date());
        CreateIndexRequest request = new CreateIndexRequest(indexName);
        request.mapping(INDEX_MAPPING, XContentType.JSON);
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        boolean acknowledged = response.isAcknowledged();
        if (acknowledged) {
            System.out.println("Index " + indexName + " created successfully.");
        } else {
            System.out.println("Index " + indexName + " creation failed.");
        }
    }
}

In the above example, the @EnableScheduling annotation enables Spring's scheduled task functionality.

The @Scheduled annotation specifies a Cron expression that executes the createIndex() method at 0:00 every day. 

In the createIndex() method, first use the SimpleDateFormat class to get the current date and generate a new index name based on the date.

Then create a CreateIndexRequest object and specify the index name and index mapping.

Finally, use the RestHighLevelClient object to send a request to create a new index. 

It is worth noting that the RestHighLevelClient object needs to be initialized when the application starts. The following configuration can be added to the configuration file of the Spring Boot application:

 
  
spring.elasticsearch.rest.uris=http://localhost:9200

It is assumed here that Elasticsearch is running locally on port 9200. Additionally, the following dependencies need to be added:

 
  
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.5.1</version>
</dependency>

This is a basic example, which can be modified and extended as needed.

Guess you like

Origin blog.csdn.net/X8i0Bev/article/details/130979441