Skip to content

Commit 8a4c451

Browse files
author
Jiahui Jiang
authored
Add similarity module config in index resource (#321)
* Add similarity module config in index resource
1 parent 0ed9423 commit 8a4c451

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

es/resource_elasticsearch_index.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var (
2525
"shard.check_on_startup",
2626
"sort.field",
2727
"sort.order",
28+
"index.similarity.default",
2829
}
2930
dynamicsSettingsKeys = []string{
3031
"number_of_replicas",
@@ -141,6 +142,13 @@ var (
141142
ForceNew: true,
142143
Optional: true,
143144
},
145+
"index_similarity_default": {
146+
Type: schema.TypeString,
147+
Description: "A JSON string describing the default index similarity config.",
148+
Optional: true,
149+
ForceNew: true, // To update index similarity config, the index must be closed, updated, and then reopened; we can't handle that here.
150+
ValidateFunc: validation.StringIsJSON,
151+
},
144152
// Dynamic settings that can be changed at runtime
145153
"number_of_replicas": {
146154
Type: schema.TypeString,
@@ -498,6 +506,17 @@ func resourceElasticsearchIndexCreate(d *schema.ResourceData, meta interface{})
498506
body["mappings"] = mappings
499507
}
500508

509+
// Decode index.similarity.default JSON
510+
if defaultIndexSimilarityJSON, ok := d.GetOk("index_similarity_default"); ok {
511+
var defaultIndexSimilarity map[string]interface{}
512+
bytes := []byte(defaultIndexSimilarityJSON.(string))
513+
err = json.Unmarshal(bytes, &defaultIndexSimilarity)
514+
if err != nil {
515+
return fmt.Errorf("fail to unmarshal: %v", err)
516+
}
517+
settings["index.similarity.default"] = defaultIndexSimilarity
518+
}
519+
501520
// if date math is used, we need to pass the resolved name along to the read
502521
// so we can pull the right result from the response
503522
var resolvedName string

es/resource_elasticsearch_index_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,19 @@ resource "elasticsearch_index" "test" {
273273
274274
depends_on = [elasticsearch_index_template.test]
275275
}
276+
`
277+
278+
testAccElasticsearchIndexWithSimilarityConfig = `
279+
resource "elasticsearch_index" "test_similarity_config" {
280+
name = "terraform-test-update-similarity-module"
281+
number_of_shards = 1
282+
number_of_replicas = 1
283+
index_similarity_default = jsonencode({
284+
"type" : "BM25",
285+
"b" : 0.25,
286+
"k1" : 1.2
287+
})
288+
}
276289
`
277290
)
278291

@@ -374,6 +387,22 @@ func TestAccElasticsearchIndex_dateMath(t *testing.T) {
374387
})
375388
}
376389

390+
func TestAccElasticsearchIndex_similarityConfig(t *testing.T) {
391+
resource.Test(t, resource.TestCase{
392+
PreCheck: func() { testAccPreCheck(t) },
393+
Providers: testAccProviders,
394+
CheckDestroy: checkElasticsearchIndexDestroy,
395+
Steps: []resource.TestStep{
396+
{
397+
Config: testAccElasticsearchIndexWithSimilarityConfig,
398+
Check: resource.ComposeTestCheckFunc(
399+
checkElasticsearchIndexExists("elasticsearch_index.test_similarity_config"),
400+
),
401+
},
402+
},
403+
})
404+
}
405+
377406
func TestAccElasticsearchIndex_doctype(t *testing.T) {
378407
provider := Provider()
379408
diags := provider.Configure(context.Background(), &terraform.ResourceConfig{})

0 commit comments

Comments
 (0)