title: Elastic Search Unique Constraint
body: <p>I am trying to make a function using elasticsearch v 1.4.4 in node.js to respond with an error </p>
  
  <blockquote>
    <p>ERROR :  duplicate _id </p>
  </blockquote>
  
  <p>like in mongoDB when we try to insert a document in a index with the duplicate _Id.
  Elastic search didn't provide such an error in spite of that it will <strong>modify</strong> the doc exist with that _id.</p>
  
  <hr>
  
  <p>I have made a function which first make a query on elastic to find a record with that id throw an error.
  But it doesn't work on real time.By real time i mean that if i try to insert docs with a new id then they will all insert and increases the version of doc.I have tried <strong>_refresh</strong> but doesn't work.  </p>
  
  <p>my function is something like..</p>
  
  <pre><code>function checkDuplicate(index,type,id,body){
      client.exists({index: index, type: type, id: id}, function (err, res) {
         if (res === false) {
              client.index({index: index, type: type,id:id, body: json}, function (err, response) {
                  resp.send(JSON.stringify(response));
              })
          }
          else
              resp.send("Error : Duplicate index doc &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;");
      });
  };
  </code></pre>