title: elasticsearch - Increment counter or create document if not exists
body: <p>I'm incrementing a counter in elasticsearch with this query:</p>
  
  <pre><code>{
     "script" : "ctx._source.value += 1"
  }
  </code></pre>
  
  <p>However, I get an error if the document does not exist. I'm using the Java API and this is my code:</p>
  
  <pre><code>UpdateResponse updateResponse = client.prepareUpdate("app", "counters", "message")
                                        .setScript("ctx._source.value += 1", ScriptService.ScriptType.INLINE)
                                        .setRetryOnConflict(5)
                                        .get();
  </code></pre>
  
  <p>I'm getting a DocumentMissingException: [app][1] [counters][message]: document missing</p>
  
  <p>What I want is update the document if exists, or create a document with "value"=0 if doesn't.</p>