title: Duplicate documents in Elasticsearch index with the same _uid
body: <p>We've discovered some duplicate documents in one of our Elasticsearch indices and we haven't been able to work out the cause. There are two copies of each of the affected documents, and they have exactly the same <code>_id</code>, <code>_type</code> and <code>_uid</code> fields.</p>
  
  <p>A GET request to <code>/index-name/document-type/document-id</code> just returns one copy, but searching for the document with a query like this returns two results, which is quite surprising:</p>
  
  <pre><code>POST /index-name/document-type/_search
  {
    "filter": {
      "term": {
        "_id": "document-id"
      }
    }
  }
  </code></pre>
  
  <p>Aggregating on the <code>_uid</code> field also identifies the duplicate documents:</p>
  
  <pre><code>POST /index-name/_search
  {
    "size": 0,
    "aggs": {
      "duplicates": {
        "terms": {
          "field": "_uid",
          "min_doc_count": 2
        }
      }
    }
  }
  </code></pre>
  
  <p>The duplicates are all on different shards. For example, a document might have one copy on primary shard 0 and one copy on primary shard 1. We've verified this by running the aggregate query above on each shard in turn using the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-request-preference.html" rel="noreferrer">preference parameter</a>: it does not find any duplicates within a single shard.</p>
  
  <p>Our best guess is that something has gone wrong with the routing, but we don't understand how the copies could have been routed to different shards. According to the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-routing-field.html" rel="noreferrer">routing documentation</a>, the default routing is based on the document ID, and should consistently route a document to the same shard.</p>
  
  <p>We are not using custom routing parameters that would override the default routing. We've double-checked this by making sure that the duplicate documents don't have a <code>_routing</code> field.</p>
  
  <p>We also don't define any parent/child relationships which would also affect routing. (See <a href="https://discuss.elastic.co/t/duplicate-document-with-same--id-and--type/8631/2" rel="noreferrer">this question in the Elasticsearch forum</a>, for example, which has the same symptoms as our problem. We don't think the cause is the same because we're not setting any document parents).</p>
  
  <p>We fixed the immediate problem by reindexing into a new index, which squashed the duplicate documents. We still have the old index around for debugging.</p>
  
  <p>We haven't found a way of replicating the problem. The new index is indexing documents correctly, and we've tried rerunning an overnight processing job which also updates documents but it hasn't created any more duplicates.</p>
  
  <p>The cluster has 3 nodes, 3 primary shards and 1 replica (i.e. 3 replica shards). <code>minimum_master_nodes</code> is set to 2, which should prevent the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-node.html#split-brain" rel="noreferrer">split-brain</a> issue. We're running Elasticsearch 2.4 (which we know is old - we're planning to upgrade soon).</p>
  
  <p>Does anyone know what might cause these duplicates? Do you have any suggestions for ways to debug it?</p>