Backend Development 5 min read

How to Bulk Insert Data into Elasticsearch with Spring Boot and Index Templates

This guide shows how to configure a Spring Boot 2.3.12 application with Elasticsearch 7.8, create index templates, implement a reusable bulk operator service, and test bulk insertion of task data into dynamically named indices, ensuring automatic index creation and proper mapping.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How to Bulk Insert Data into Elasticsearch with Spring Boot and Index Templates

Environment

Spring Boot 2.3.12 RELEASE with Elasticsearch 7.8.0.

The task is executed manually, and each execution should add generated data to a different index.

Configuration

<code>&lt;dependencies&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;org.elasticsearch.client&lt;/groupId&gt;
    &lt;artifactId&gt;elasticsearch-rest-high-level-client&lt;/artifactId&gt;
    &lt;version&gt;7.8.0&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-data-elasticsearch&lt;/artifactId&gt;
  &lt;/dependency&gt;
&lt;/dependencies&gt;</code>
<code>spring.elasticsearch.index.task=t_task_
spring.elasticsearch.rest.uris=http://localhost:9200</code>

Create Index Template

Use the following template (PUT /_template/t_task_tml) to define index patterns, settings, and mappings for fields taskId, taskName, and createTime.

<code>{
    "index_patterns": ["t_task_*"],
    "settings": {
      "number_of_shards": 3,
      "number_of_replicas": 0,
      "index": {
        "refresh_interval": "60s",
        "translog": {"sync_interval": "20s"}
      }
    },
    "mappings": {
      "_source": {"enabled": true},
      "properties": {
        "taskId": {"type": "keyword"},
        "taskName": {"type": "keyword"},
        "createTime": {"type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}
      }
    }
}</code>

Creating the template allows each task to create a separate index without repeating mapping and setting information.

Bulk Operation

Define an abstract

BulkOperator

class and a concrete

TaskBulkService

that extends it.

<code>public abstract class BulkOperator<T> {
    @Resource
    protected RestHighLevelClient client;
    public abstract void bulkWriter(List<T> datas, String suffix);
}</code>
<code>@Service
public class TaskBulkService extends BulkOperator<Task> {
    private static final Logger logger = LoggerFactory.getLogger(TaskBulkService.class);
    @Value("${spring.elasticsearch.index.task:t_task_}")
    private String index_name;

    @Override
    public void bulkWriter(List<Task> datas, String suffix) {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout(TimeValue.timeValueMinutes(1));
        ObjectMapper objectMapper = new ObjectMapper();
        datas.forEach(task -> {
            try {
                bulkRequest.add(new IndexRequest(index_name + suffix)
                    .id(task.getId())
                    .source(objectMapper.writeValueAsString(task), XContentType.JSON));
            } catch (JsonProcessingException e) {
                logger.error("Object Task to String error: {}", e);
            }
        });
        try {
            BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
            if (response.status() != RestStatus.OK) {
                logger.error("Bulk save failed");
            }
        } catch (IOException e) {
            logger.error("Bulk save error: {}", e);
        }
    }
}
</code>

Test Endpoint

<code>@GetMapping("/es/bulk")
public Object bulk(String suffix) {
    List<Task> datas = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        Task task = new Task();
        task.setTaskId("taskId " + i);
        task.setTaskName("taskName " + i);
        task.setCreateTime("createTime " + i);
        datas.add(task);
    }
    bulkOperator.bulkWriter(datas, suffix);
    return "success";
}
</code>

The index will be created automatically.

Done!

JavaElasticsearchSpring BootBulk InsertIndex Template
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.