Search code examples
javaspring-bootelasticsearchelasticsearch-java-api

Indexing thousands of records into Elasticsearch using Index API synchronous is right approach?


I am trying to index more than 7000 records into elasticsearch. I will pick all those records from JSonarray based on it's length i will loop through the array and i will index records one by one into elasticsearch using Indexrequest API. Since i am new to Elastisearch i wanted to confirm is this right approach. I have given my code below.

            for (int i = 0; i < odsData.size(); i++) {
                IndexRequest request = new IndexRequest(ConstantsHelper.INDEX_NAME + strDate);
                request.id();
                String jsonString = odsData.get(i).toString();
                request.source(jsonString, XContentType.JSON);
                IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
            }

In this a right approach? Also i wanted to check No of records in array and no of records indexed in the Elasticsearch are matching once indexing gets completed?


Solution

  • Yes that should work fine, but it will be a slow process. There is also a bulk indexing API that you can use to index multiple documents at once which is very fast. Link