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

# Now this is the main index with alias.
resource "opensearch_index" "global_participant_v03" {
  name                   = "global_participant.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_participant]

  aliases = jsonencode({
    "global_participant_write" : {
      "is_write_index" : true
    },
    "global_participant_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 this block will cause the index to be replaced on every apply
  lifecycle {
    ignore_changes = [mappings]
  }
}

