# Upsert is throwing DocumentMissingException

**URL:** https://discuss.elastic.co/t/upsert-is-throwing-documentmissingexception/199307
**Category:** Elasticsearch
**Created:** [September 12, 2019, 6:17pm UTC](https://discuss.elastic.co/t/upsert-is-throwing-documentmissingexception/199307 "2019-09-12T18:17:17Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ygoel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ygoel/32/54098_2.png) [@ygoel](https://discuss.elastic.co/u/ygoel)
#### Post date: [September 12, 2019, 6:17pm UTC](https://discuss.elastic.co/t/upsert-is-throwing-documentmissingexception/199307/1 "2019-09-12T18:17:17Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [October 10, 2019, 6:17pm UTC](https://discuss.elastic.co/t/upsert-is-throwing-documentmissingexception/199307/2 "2019-10-10T18:17:18Z")

</div>

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