# Index for global_sound_recording search
resource "opensearch_index_template" "global_sound_recording" {
  name = "global_sound_recording"
  body = file("global_sound_recording_template.json")
}

# New index to get reindexed documents.
resource "opensearch_index" "global_sound_recording_v03" {
  name                   = "global_sound_recording.v03"
  number_of_replicas     = "1"
  number_of_shards       = "3"
  refresh_interval       = "60s"
  routing_partition_size = "1"
  force_destroy          = "true"

  depends_on = [opensearch_index_template.global_sound_recording]

  aliases = jsonencode({
    "global_sound_recording_write" : {
      "is_write_index" : true
    },
    "global_sound_recording_read" : {
      "is_write_index" : false
    }
  })

  # This is a workaround to prevent the index from being replaced when the resource is updated
  # mappings are managed at the index template level
  # https://github.com/opensearch-project/terraform-provider-opensearch/issues/175
  # Removing the lifecycle block will cause the index to be destroyed and recreated on updates.

  lifecycle {
    ignore_changes = [mappings]
  }
}
