I am using Spring Boot and ElasticSearch. When I am trying to upsert using Spring, it is throwing DocumentMissingException
when there is no document present in the ElasticSearch. The same code works fine when there is a document present in the ElasticSearch.
Exception Stacktrace:
org.springframework.data.elasticsearch.ElasticsearchException: Bulk indexing has failures. Use ElasticsearchException.getFailedDocuments() for detailed messages [{U65929AR1978SGC001748=[company/vteSxfKoRF-k4g982vissw][[company][2]] DocumentMissingException[[_doc][U65929AR1978SGC001748]: document missing], U45309AR2000PTC006288=[company/vteSxfKoRF-k4g982vissw][[company][3]] DocumentMissingException[[_doc][U45309AR2000PTC006288]: document missing],...
Code:
public <S extends Company> void saveAllCustom(Iterable<S> companies) {
List<UpdateQuery> updateQueries = new ArrayList<UpdateQuery>();
ObjectMapper oMapper = new ObjectMapper();
for (S company : companies) {
Map<String, Object> companyJsonMap = (Map<String, Object>) oMapper.convertValue(company, Map.class);
Map<String, Object> paramsDocument = new HashMap<String, Object>();
paramsDocument.put("doc", companyJsonMap);
Script script = new Script(
ScriptType.INLINE
, "painless"
, "if (ctx._source.dataAsOf < params.doc.dataAsOf) {ctx._source = params.doc}"
, paramsDocument);
UpdateRequest updateRequest = new UpdateRequest()
.index("company")
.type("_doc")
.id(company.cin)
.script(script) // Conditional Update
.upsert(companyJsonMap);
UpdateQuery updateQuery = new UpdateQueryBuilder()
.withIndexName("company")
.withType("_doc")
.withId(company.cin)
.withDoUpsert(true)
.withClass(Company.class)
.withUpdateRequest(updateRequest)
.build();
updateQueries.add(updateQuery);
}
elasticsearchTemplate.bulkUpdate(updateQueries);
}
But a similar upsert command is working using CURL:
curl -X POST "localhost:9200/company/_doc/10001/_update" -H 'Content-Type: application/json' -d'
{
"script": {
"lang": "painless",
"source": "ctx._source = params.doc",
"params": {
"doc": {
"name": "XYZ LIMITED - updated",
"cin": "10001"
}
}
},
"upsert" : {
"name": "XYZ LIMITED - newly created",
"cin": "10001"
}
}'
As per my understanding, when there is no document present in the ElasticSearch, it shouldn't throw a DocumentMissingException
because I have added upsert(...)
as well in the query.