JAVA Rest Client Bulk Data insert not working


(Ankur Singla) #1

I am trying to upload bulk data(2Lacs) on my elastic cloud using JAVA REST API. In response am getting 200 status and index is created but data not shown.

final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials("elastic", "xxxxxxxx"));

            RestClient client = RestClient.builder(
                    new HttpHost("xxxxxxxx.us-east-1.aws.found.io", 9243, "https"))
                    .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                        @Override
                        public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                            httpClientBuilder.disableAuthCaching();
                            return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                        }
                    })
                    .build();

            System.out.println("Inside Main" + client);

            //String actionMetaData = String.format("{ \"index\" : { \"_index\" : \"%s\", \"_type\" : \"%s\" } }%n", "searchable", "data");
            StringBuilder bulkRequestBody = new StringBuilder();

            for(int i=0; i<dataList.length(); ++i) {

                String actionMetaData = String.format("{ \"index\" : { \"_index\" : \"%s\", \"_type\" : \"%s\", \"_id\" : \"%s\"  } }%n", "searchable", "doc", i+"");
                JSONObject dataObj = dataList.getJSONObject(i);
                bulkRequestBody.append(actionMetaData);

                String jsonData = "{" +
                     "\"search\":"+"\""+dataObj.optString("search", "search")+"\""+","+  
                     "\"searchType\":"+"\""+dataObj.optString("searchType", "searchType")+"\""+","+ 
                     "\"code    \":"+"\""+i+"\""+","+                       
                    "}";

                bulkRequestBody.append(jsonData);
                bulkRequestBody.append("\n");
            }

            bulkRequestBody.append("\n");

            HttpEntity entity = new NStringEntity(bulkRequestBody.toString(), ContentType.APPLICATION_JSON);
            Response bulkResponse = client.performRequest("POST", "/searchable/doc/_bulk?pretty", Collections.emptyMap(), entity);

In bulkResponse I am getting 200 status searchable index is created but still there is no data in the index.


(David Pilato) #2

Try to remove ?pretty.
That's not how parameters should be passed.


(system) #3

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.