Search code examples
elasticsearchversioning

Elastic - prevent updating documents


Can I make documents immutable? As in not have them be rewritten when a document is submitted to the same id?

POST "localhost:9200/index001/_doc/1" // First time it is created
'
{
  "stuff": {
      
  }
}
'

POST "localhost:9200/index001/_doc/1"  // Fails
'
{
  "otherstuff": {
      
  }
}
'

Could elastic's _version field help with that?


Solution

  • If you use index API with a specific id, elasticsearch will update that document with the newer document. But if you use create APIwith specific id, you allow elasticsearch for "put-if-absent" behavior. it means, by using create, the document index operation will fail if a document by that id already exists in the index. This is how you can use create in elasticsearch lower 7:

    POST index001/_doc/1?op_type=create
    '
    {
      "stuff": {
          
      }
    }
    '
    
    

    and this is using for elasticsearch 7:

    POST index001/_create/1
    '
    {
      "stuff": {
          
      }
    }
    '