Skip to content

Commit 8c1a4d6

Browse files
author
Kanji Yomoda
authored
Add missing analysis_char_filter field (#158)
* Add missing analysis_char_filter field * Fix elasticstack_elasticsearch_index example * Fix index test * Remove ForceNew from analysis settings
1 parent 295755e commit 8c1a4d6

File tree

4 files changed

+52
-48
lines changed

4 files changed

+52
-48
lines changed

docs/resources/elasticsearch_index.md

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ resource "elasticstack_elasticsearch_index" "my_index" {
2525
}
2626
2727
alias {
28-
name = "my_alias_2"
28+
name = "my_alias_2"
2929
filter = jsonencode({
3030
term = { "user.id" = "developer" }
3131
})
@@ -35,27 +35,18 @@ resource "elasticstack_elasticsearch_index" "my_index" {
3535
properties = {
3636
field1 = { type = "keyword" }
3737
field2 = { type = "text" }
38-
field3 = { properties = {
39-
inner_field1 = { type = "text", index = false }
40-
inner_field2 = { type = "integer", index = false }
41-
} }
38+
field3 = {
39+
properties = {
40+
inner_field1 = { type = "text", index = false }
41+
inner_field2 = { type = "integer", index = false }
42+
}
43+
}
4244
}
4345
})
4446
45-
settings {
46-
setting {
47-
name = "index.number_of_shards"
48-
value = "1"
49-
}
50-
setting {
51-
name = "index.number_of_replicas"
52-
value = "2"
53-
}
54-
setting {
55-
name = "index.search.idle.after"
56-
value = "20s"
57-
}
58-
}
47+
number_of_shards = 1
48+
number_of_replicas = 2
49+
search_idle_after = "20s"
5950
}
6051
```
6152

@@ -70,6 +61,7 @@ resource "elasticstack_elasticsearch_index" "my_index" {
7061

7162
- `alias` (Block Set) Aliases for the index. (see [below for nested schema](#nestedblock--alias))
7263
- `analysis_analyzer` (String) A JSON string describing the analyzers applied to the index.
64+
- `analysis_char_filter` (String) A JSON string describing the char_filters applied to the index.
7365
- `analysis_filter` (String) A JSON string describing the filters applied to the index.
7466
- `analysis_normalizer` (String) A JSON string describing the normalizers applied to the index.
7567
- `analysis_tokenizer` (String) A JSON string describing the tokenizers applied to the index.

examples/resources/elasticstack_elasticsearch_index/resource.tf

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ resource "elasticstack_elasticsearch_index" "my_index" {
1010
}
1111

1212
alias {
13-
name = "my_alias_2"
13+
name = "my_alias_2"
1414
filter = jsonencode({
1515
term = { "user.id" = "developer" }
1616
})
@@ -20,25 +20,16 @@ resource "elasticstack_elasticsearch_index" "my_index" {
2020
properties = {
2121
field1 = { type = "keyword" }
2222
field2 = { type = "text" }
23-
field3 = { properties = {
24-
inner_field1 = { type = "text", index = false }
25-
inner_field2 = { type = "integer", index = false }
26-
} }
23+
field3 = {
24+
properties = {
25+
inner_field1 = { type = "text", index = false }
26+
inner_field2 = { type = "integer", index = false }
27+
}
28+
}
2729
}
2830
})
2931

30-
settings {
31-
setting {
32-
name = "index.number_of_shards"
33-
value = "1"
34-
}
35-
setting {
36-
name = "index.number_of_replicas"
37-
value = "2"
38-
}
39-
setting {
40-
name = "index.search.idle.after"
41-
value = "20s"
42-
}
43-
}
32+
number_of_shards = 1
33+
number_of_replicas = 2
34+
search_idle_after = "20s"
4435
}

internal/elasticsearch/index/index.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,32 +376,36 @@ func ResourceIndex() *schema.Resource {
376376
Description: "Set the number of characters of the `_source` to include in the slowlog lines, `false` or `0` will skip logging the source entirely and setting it to `true` will log the entire source regardless of size. The original `_source` is reformatted by default to make sure that it fits on a single log line.",
377377
Optional: true,
378378
},
379+
// To change analyzer setting, the index must be closed, updated, and then reopened but it can't be handled in terraform.
380+
// We raise error when they are tried to be updated instead of setting ForceNew not to have unexpected deletion.
379381
"analysis_analyzer": {
380382
Type: schema.TypeString,
381383
Description: "A JSON string describing the analyzers applied to the index.",
382384
Optional: true,
383-
ForceNew: true, // To add an analyzer, the index must be closed, updated, and then reopened; we can't handle that here.
384385
ValidateFunc: validation.StringIsJSON,
385386
},
386387
"analysis_tokenizer": {
387388
Type: schema.TypeString,
388389
Description: "A JSON string describing the tokenizers applied to the index.",
389390
Optional: true,
390-
ForceNew: true, // To add a tokenizer, the index must be closed, updated, and then reopened; we can't handle that here.
391+
ValidateFunc: validation.StringIsJSON,
392+
},
393+
"analysis_char_filter": {
394+
Type: schema.TypeString,
395+
Description: "A JSON string describing the char_filters applied to the index.",
396+
Optional: true,
391397
ValidateFunc: validation.StringIsJSON,
392398
},
393399
"analysis_filter": {
394400
Type: schema.TypeString,
395401
Description: "A JSON string describing the filters applied to the index.",
396402
Optional: true,
397-
ForceNew: true, // To add a filter, the index must be closed, updated, and then reopened; we can't handle that here.
398403
ValidateFunc: validation.StringIsJSON,
399404
},
400405
"analysis_normalizer": {
401406
Type: schema.TypeString,
402407
Description: "A JSON string describing the normalizers applied to the index.",
403408
Optional: true,
404-
ForceNew: true, // To add a normalizer, the index must be closed, updated, and then reopened; we can't handle that here.
405409
ValidateFunc: validation.StringIsJSON,
406410
},
407411
"alias": {
@@ -695,6 +699,14 @@ func resourceIndexCreate(ctx context.Context, d *schema.ResourceData, meta inter
695699
}
696700
analysis["tokenizer"] = tokenizer
697701
}
702+
if charFilterJSON, ok := d.GetOk("analysis_char_filter"); ok {
703+
var filter map[string]interface{}
704+
bytes := []byte(charFilterJSON.(string))
705+
if err = json.Unmarshal(bytes, &filter); err != nil {
706+
return diag.FromErr(err)
707+
}
708+
analysis["char_filter"] = filter
709+
}
698710
if filterJSON, ok := d.GetOk("analysis_filter"); ok {
699711
var filter map[string]interface{}
700712
bytes := []byte(filterJSON.(string))

internal/elasticsearch/index/index_test.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ func TestAccResourceIndexSettings(t *testing.T) {
8282
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index.test_settings", "routing_allocation_enable", "primaries"),
8383
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index.test_settings", "routing_rebalance_enable", "primaries"),
8484
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index.test_settings", "gc_deletes", "30s"),
85-
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index.test_settings", "analysis_analyzer", `{"text_en":{"filter":["lowercase","minimal_english_stemmer"],"tokenizer":"standard","type":"custom"}}`),
85+
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index.test_settings", "analysis_analyzer", `{"text_en":{"char_filter":"zero_width_spaces","filter":["lowercase","minimal_english_stemmer"],"tokenizer":"standard","type":"custom"}}`),
86+
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index.test_settings", "analysis_char_filter", `{"zero_width_spaces":{"mappings":["\\u200C=\u003e\\u0020"],"type":"mapping"}}`),
8687
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index.test_settings", "analysis_filter", `{"minimal_english_stemmer":{"language":"minimal_english","type":"stemmer"}}`),
8788
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index.test_settings", "settings.0.setting.0.name", "number_of_replicas"),
8889
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index.test_settings", "settings.0.setting.0.value", "2"),
@@ -240,11 +241,11 @@ resource "elasticstack_elasticsearch_index" "test_settings" {
240241
routing_allocation_enable = "primaries"
241242
routing_rebalance_enable = "primaries"
242243
gc_deletes = "30s"
243-
analysis_analyzer = jsonencode({
244-
text_en = {
245-
type = "custom"
246-
tokenizer = "standard"
247-
filter = ["lowercase", "minimal_english_stemmer"]
244+
245+
analysis_char_filter = jsonencode({
246+
zero_width_spaces = {
247+
type = "mapping"
248+
mappings = ["\\u200C=>\\u0020"]
248249
}
249250
})
250251
analysis_filter = jsonencode({
@@ -253,6 +254,14 @@ resource "elasticstack_elasticsearch_index" "test_settings" {
253254
language = "minimal_english"
254255
}
255256
})
257+
analysis_analyzer = jsonencode({
258+
text_en = {
259+
type = "custom"
260+
tokenizer = "standard"
261+
char_filter = "zero_width_spaces"
262+
filter = ["lowercase", "minimal_english_stemmer"]
263+
}
264+
})
256265
257266
settings {
258267
setting {

0 commit comments

Comments
 (0)