Skip to content

Commit c1cb031

Browse files
authored
fix: fix for a bug with Elastic index (#125)
1 parent 3a69906 commit c1cb031

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

modules/elastic-index/main.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ locals {
77
resource "elasticstack_elasticsearch_index" "sample_index" {
88
name = var.elastic_index_name
99
deletion_protection = false
10+
mappings = jsonencode({ properties = {
11+
for key, value in jsondecode(var.elastic_index_mapping) : value => { fields = { keyword = { ignore_above = 256, type = "keyword" } }, type = "text" }
12+
} })
1013
}
1114

1215
## Upload sample data entries
@@ -18,7 +21,7 @@ resource "elasticstack_elasticsearch_index" "sample_index" {
1821

1922
resource "shell_script" "elastic_index_entries" {
2023
count = var.elastic_index_entries_file != null ? 1 : 0
21-
depends_on = [elasticstack_elasticsearch_index.sample_index] # shell_script.elastic_index
24+
depends_on = [elasticstack_elasticsearch_index.sample_index]
2225
lifecycle_commands {
2326
create = <<-EOT
2427
set -e
@@ -35,7 +38,8 @@ resource "shell_script" "elastic_index_entries" {
3538
environment = {
3639
ELASTIC_URL = var.elastic_service_binding.url
3740
ELASTIC_CACERT = var.elastic_service_binding.ca_data_base64
38-
ELASTIC_INDEX_NAME = urlencode(elasticstack_elasticsearch_index.sample_index.name) # shell_script.elastic_index.output.index_name)
41+
ELASTIC_INDEX_NAME = urlencode(elasticstack_elasticsearch_index.sample_index.name)
42+
ELASTIC_INDEX_ID = elasticstack_elasticsearch_index.sample_index.id # Add dependency to force upload on index recreate
3943
ELASTIC_ENTRIES_FILE = var.elastic_index_entries_file
4044
MODULE_PATH = path.module
4145
}

modules/elastic-index/scripts/elastic-index-utils.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ function delete_index_entries() {
7979
--data '{"query": {"match_all": {}}}' \
8080
--cacert <(cat <<< "$ELASTIC_CACERT" | base64 -d)
8181
)
82-
check_error "$OUTPUT"
82+
check_error "$OUTPUT" "index_not_found_exception"
83+
if [[ ! $(echo "$OUTPUT" | jq -r '.error?.type') == "index_not_found_exception" ]]; then
84+
echo "Index already deleted"
85+
exit 0
86+
fi
87+
8388
if [[ ! $(echo "$OUTPUT" | jq -r '. | .failures | length') == "0" ]]; then
8489
echo "$OUTPUT"
8590
exit 1
@@ -95,7 +100,9 @@ function check_error() {
95100
fi
96101

97102
local error
98-
error=$(echo "$1" | jq '.error')
103+
local ignore_error_type
104+
ignore_error_type="${2:-none}"
105+
error=$(echo "$1" | jq --arg ignore "$ignore_error_type" '.error | select(.type != $ignore)')
99106
if [[ -n "$error" && ! "$error" == "null" ]]; then
100107
echo "$error" | jq
101108
exit 1

modules/elastic-index/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ variable "elastic_index_name" {
1414
default = "sample-rag-app-content"
1515
}
1616

17+
variable "elastic_index_mapping" {
18+
description = "Mapping configuration for the Elastic index"
19+
type = string
20+
default = "{}"
21+
}
22+
1723
variable "elastic_index_entries_file" {
1824
description = "Path to JSON file with content entries"
1925
type = string

solutions/banking/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ module "configure_elastic_index" {
173173
source = "../../modules/elastic-index"
174174
elastic_service_binding = local.elastic_service_binding
175175
elastic_index_name = local.elastic_index_name
176+
elastic_index_mapping = jsonencode(jsondecode(file("${path.module}/artifacts/watsonx.Assistant/elastic-search-skill.json")).search_settings.schema_mapping)
176177
elastic_index_entries_file = var.elastic_upload_sample_data ? "./artifacts/watsonx.Assistant/bank-loan-faqs.json" : null
177178
depends_on = [data.ibm_iam_auth_token.tokendata]
178179
}

tests/pr_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func TestRunUpgradeExample(t *testing.T) {
170170

171171
options.IgnoreDestroys = testhelper.Exemptions{
172172
List: []string{
173-
"null_resource.discovery_file_upload",
173+
"module.configure_discovery_project[0].null_resource.discovery_file_upload",
174174
},
175175
}
176176

0 commit comments

Comments
 (0)