Skip to content

Commit

Permalink
add fields field
Browse files Browse the repository at this point in the history
  • Loading branch information
lantoli committed Nov 8, 2023
1 parent 5cff810 commit caeba29
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions mongodbatlas/resource_mongodbatlas_search_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ func returnSearchIndexSchema() map[string]*schema.Schema {
Type: schema.TypeString,
Optional: true,
},
"fields": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: validateSearchIndexMappingDiff,
},
}
}

Expand Down Expand Up @@ -222,6 +227,10 @@ func resourceMongoDBAtlasSearchIndexUpdate(ctx context.Context, d *schema.Resour
searchIndex.Mappings.Fields = unmarshalSearchIndexMappingFields(d.Get("mappings_fields").(string))
}

if d.HasChange("fields") {
searchIndex.Mappings.Fields = unmarshalSearchIndexMappingFields(d.Get("fields").(string))
}

if d.HasChange("synonyms") {
searchIndex.Synonyms = expandSearchIndexSynonyms(d)
}
Expand Down Expand Up @@ -334,6 +343,17 @@ func resourceMongoDBAtlasSearchIndexRead(ctx context.Context, d *schema.Resource
}
}

if len(searchIndex.Fields) > 0 {
fields, err := marshalSearchIndex(searchIndex.Fields)
if err != nil {
return diag.FromErr(err)
}

if err := d.Set("fields", fields); err != nil {
return diag.Errorf("error setting `fields` for for search index (%s): %s", d.Id(), err)
}
}

return nil
}

Expand Down Expand Up @@ -369,6 +389,7 @@ func resourceMongoDBAtlasSearchIndexCreate(ctx context.Context, d *schema.Resour
Dynamic: &dynamic,
Fields: unmarshalSearchIndexMappingFields(d.Get("mappings_fields").(string)),
},
Fields: unmarshalSearchIndexFields(d.Get("fields").(string)),
Name: d.Get("name").(string),
SearchAnalyzer: stringPtr(d.Get("search_analyzer").(string)),
Status: stringPtr(d.Get("status").(string)),
Expand Down Expand Up @@ -443,9 +464,11 @@ func validateSearchIndexMappingDiff(k, old, newStr string, d *schema.ResourceDat

if err := json.Unmarshal([]byte(old), &j); err != nil {
log.Printf("[ERROR] cannot unmarshal old search index mapping json %v", err)
return false
}
if err := json.Unmarshal([]byte(newStr), &j2); err != nil {
log.Printf("[ERROR] cannot unmarshal new search index mapping json %v", err)
return false
}
if diff := deep.Equal(&j, &j2); diff != nil {
log.Printf("[DEBUG] deep equal not passed: %v", diff)
Expand Down Expand Up @@ -492,6 +515,18 @@ func unmarshalSearchIndexMappingFields(mappingString string) map[string]any {
return fields
}

func unmarshalSearchIndexFields(fieldsStr string) []map[string]any {
if fieldsStr == "" {
return nil
}
var fields []map[string]any
if err := json.Unmarshal([]byte(fieldsStr), &fields); err != nil {
log.Printf("[ERROR] cannot unmarshal search index fields: %v", err)
return nil
}
return fields
}

func unmarshalSearchIndexAnalyzersFields(mappingString string) []admin.ApiAtlasFTSAnalyzers {
if mappingString == "" {
return nil
Expand Down

0 comments on commit caeba29

Please sign in to comment.