title: ElasticSearch - Spring Boot - Upsert is throwing DocumentMissingException
body: <p>I am using Spring Boot and ElasticSearch. When I am trying to upsert using Spring, it is throwing <code>DocumentMissingException</code> when there is no document present in the ElasticSearch. The same code works fine when there is a document present in the ElasticSearch.</p>
  <p><strong>Exception Stacktrace:</strong></p>
  <blockquote>
  <p><strong>org.springframework.data.elasticsearch.ElasticsearchException: Bulk indexing has failures.</strong> 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],...</p>
  </blockquote>
  <p><strong>Code:</strong></p>
  <pre><code>public &lt;S extends Company&gt; void saveAllCustom(Iterable&lt;S&gt; companies) {
  
      List&lt;UpdateQuery&gt; updateQueries = new ArrayList&lt;UpdateQuery&gt;();
  
      ObjectMapper oMapper = new ObjectMapper();
  
      for (S company : companies) {
  
          Map&lt;String, Object&gt; companyJsonMap = (Map&lt;String, Object&gt;) oMapper.convertValue(company, Map.class);
      
          Map&lt;String, Object&gt; paramsDocument = new HashMap&lt;String, Object&gt;();
          paramsDocument.put(&quot;doc&quot;, companyJsonMap);
      
          Script script = new Script(
              ScriptType.INLINE
              , &quot;painless&quot;
              , &quot;if (ctx._source.dataAsOf &lt; params.doc.dataAsOf) {ctx._source = params.doc}&quot;
              , paramsDocument);
      
          UpdateRequest updateRequest = new UpdateRequest()
              .index(&quot;company&quot;)
              .type(&quot;_doc&quot;)
              .id(company.cin)
              .script(script) // Conditional Update
              .upsert(companyJsonMap);
  
          UpdateQuery updateQuery = new UpdateQueryBuilder()
              .withIndexName(&quot;company&quot;)
              .withType(&quot;_doc&quot;)
              .withId(company.cin)
              .withDoUpsert(true)
              .withClass(Company.class)
              .withUpdateRequest(updateRequest)
              .build();
      
          updateQueries.add(updateQuery);
          
      }
  
      elasticsearchTemplate.bulkUpdate(updateQueries);
  }
  </code></pre>
  <p>But a similar upsert command is working using CURL:</p>
  <pre><code>curl -X POST &quot;localhost:9200/company/_doc/10001/_update&quot; -H 'Content-Type: application/json' -d'
  {
      &quot;script&quot;: {
          &quot;lang&quot;: &quot;painless&quot;,
          &quot;source&quot;: &quot;ctx._source = params.doc&quot;,
          &quot;params&quot;: {
              &quot;doc&quot;: {
                  &quot;name&quot;: &quot;XYZ LIMITED - updated&quot;,
                  &quot;cin&quot;: &quot;10001&quot;
              }
          }
      },
      &quot;upsert&quot; : {
          &quot;name&quot;: &quot;XYZ LIMITED - newly created&quot;,
          &quot;cin&quot;: &quot;10001&quot;
      }
  }'
  </code></pre>
  <p>As per my understanding, when there is no document present in the ElasticSearch, it shouldn't throw a <code>DocumentMissingException</code> because I have added <code>upsert(...)</code> as well in the query.</p>
  <p><strong>Elasticsearch version:</strong> Version: 6.4.3, Build: default/tar/fe40335/2018-10-30T23:17:19.084789Z, JVM: 1.8.0_191</p>
  <p><strong>Plugins installed:</strong> None</p>
  <p><strong>JVM version:</strong> java version &quot;1.8.0_191&quot;
  Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
  Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)</p>
  <p><strong>OS version:</strong> Darwin Kernel Version 18.5.0: Mon Mar 11 20:40:32 PDT 2019; root:xnu-4903.251.3~3/RELEASE_X86_64 x86_64</p>
  <p><strong>Spring Boot Version:</strong> 2.1.7.RELEASE</p>
  <p><strong>Spring Data ElasticSearch Version:</strong> 3.2.0.RC2</p>