0

I'm using ES 7.5.1 for storing data, with Rest API (Spring-boot). It's working fine with a single object but when I tried to pass an array of objects it's throwing bad request or not acceptable error(406).

Code:

List<UserBean> objectList;
String response = restTemplate.postForObject("http://localhost:9200/employeedata/users", objectList, String.class);
4
  • this information provided is not sufficient for helping, please give share more information. Commented Jan 2, 2020 at 12:32
  • Thanks for your quick response, I'm new to elastic search please specify what information is required. Commented Jan 2, 2020 at 13:02
  • Which version of elastic are you using? Commented Jan 2, 2020 at 16:09
  • ES version is 7.5.1 Commented Jan 6, 2020 at 7:23

2 Answers 2

1

You need to use Bulk API. Docs This API accepting arrays of objects in JSON (NDJSON) structure.

Performs multiple indexing or delete operations in a single API call. This reduces overhead and can greatly increase indexing speed.

Sign up to request clarification or add additional context in comments.

Comments

0

We can index bulk data into elastic using RestHighLevelClient

Maven dependencies:

<dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>${elastic.version}</version>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-client</artifactId>
        <version>${elastic.version}</version>
    </dependency>

Configuration :

@Bean
protected RestHighLevelClient createInstance() throws Exception
{
    try
    {
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", "9200", "http")));
    }
    catch (Exception e)
    {
        LOG.error("Error while creating RestHighLevelClient", e);
    }
    return restHighLevelClient;
}

Usage :

@Autowired
private RestHighLevelClient restHighLevelClient;

@Override
public void storeBulkDataInElastic(List<UserBean> dataBeanList) throws IOException
{
    BulkRequest bulkRequest = new BulkRequest();
    ObjectMapper objectMapper = new ObjectMapper();
    dataBeanList.forEach(data -> {
        IndexRequest indexRequest = new IndexRequest("ElasticIndex", "ElasticType", "ElasticId").source(objectMapper.convertValue(data, Map.class));
        bulkRequest.add(indexRequest);
    });

    restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.