How to Index duplicate documents with the different routing id

My index settings:

{
    "aliases": {
        "alias_test_read": {}
    },
    "mappings": {
        "_routing": {
            "required": true
        },
        "properties": {
            "id": {
                "type": "text",
                "store": true
            },
            "name": {
                "type": "text",
                "store": true
            },
            "type": {
                "type": "text",
                "store": true
            }
        }
    },
    "settings": {
        "index": {
            "number_of_shards": "3",
            "number_of_replicas": "1"
        }
    }
}

inserting the recodes using the bulk API:
curl -xpost http://host:port/test/_bulk


{"index":{"_id":"1","routing":"ABC","version":"1","version_type":"external"}}
{"id": "1", "name" : "Raj", "type": "ABC"}
{"index":{"_id":"1","routing":"XYC","version":"1","version_type":"external"}}
{"id": "1", "name" : "Raj", "type": "XYC"}
{"index":{"_id":"1","routing":"PQR","version":"1","version_type":"external"}}
{"id": "1", "name" : "Raj", "type": "PQR"}

output:

{"took":9,"errors":true,"items":[{"index":{"_index":"test","_type":"_doc","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":0,"_primary_term":1,"status":201}},{"index":{"_index":"test","_type":"_doc","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":0,"_primary_term":1,"status":201}},{"index":{"_index":"test","_type":"_doc","_id":"1","status":409,"error":{"type":"version_conflict_engine_exception","reason":"[1]: version conflict, current version [1] is higher or equal to the one provided [1]","index_uuid":"VP024Sr9TRqsLBzsfEYoUA","shard":"1","index":"test"}}}]}

Not sure why i'm getting version conflict for third record alone not for the second. is their any way to keep same _id with different routing records in index ?

All documents with a specific routing id will end up in the same shard but documents with different routing ids do not necessary end up in different shards. It is common to have significantly more routing ids than there is shards in an index. You can therefore not have multiple documents with trhe same id in an index separate by routing id. An option might be to create a document id consisting of the id concatenated with the routing id.

1 Like

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