Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds type and fields attributes in resource and data sources for search_index #1605

Merged
merged 27 commits into from
Nov 16, 2023

Conversation

lantoli
Copy link
Member

@lantoli lantoli commented Nov 7, 2023

Description

Jira ticket: INTMDB-1260

Adds type and fields attributes in resource and data sources for search_index.

Type of change:

  • [] Bug fix (non-breaking change which fixes an issue). Please, add the "bug" label to the PR.
  • New feature (non-breaking change which adds functionality). Please, add the "enhancement" label to the PR.
  • Breaking change (fix or feature that would cause existing functionality to not work as expected). Please, add the "breaking change" label to the PR.
  • This change requires a documentation update
  • Documentation fix/enhancement

Required Checklist:

  • I have signed the MongoDB CLA
  • I have read the contribution guidelines
  • I have added tests that prove my fix is effective or that my feature works per HashiCorp requirements
  • I have added any necessary documentation (if appropriate)
  • I have run make fmt and formatted my code
  • If changes include deprecations or removals, I defined an isolated PR with a relevant title as it will be used in the auto-generated changelog.

Further comments

@lantoli lantoli changed the title feat: Add Update resource search_index feat: Adds type and fields attributes in resource search_index Nov 7, 2023
@lantoli lantoli changed the title feat: Adds type and fields attributes in resource search_index feat: Adds type and fields attributes in resource and data sources for search_index Nov 8, 2023
### Advanced (with custom analyzers)
### Basic vector index
```terraform
resource "mongodbatlas_search_index" "test-basic-search-vector" {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Zuhairahmed do we want a different example?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lantoli suggest for time being we leverage example that Atlas Search team has provided here for fields parameter: https://www.mongodb.com/docs/atlas/atlas-search/field-types/knn-vector/#std-label-fts-data-types-knn-vector

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"fields": {
  "plot_embedding": {
    "type": "knnVector",
    "dimensions": 1536,
    "similarity": "euclidean"
  }
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also believe you are missing index name parameter which is required:
image

to prevent confusion with cluster_name and collection_name, suggest we call this index_name

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. your example won't work directly but adapting the idea. current "name" attribute is the index name, that doesn't change.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

@@ -272,6 +293,10 @@ func resourceMongoDBAtlasSearchIndexRead(ctx context.Context, d *schema.Resource
return diag.Errorf("error setting `index_id` for search index (%s): %s", d.Id(), err)
}

if err := d.Set("type", searchIndex.Type); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the API always responds with a value for the type field? Would consider marking as computed or defining a default value in the schema to avoid inconsistencies in the state.

Copy link
Member Author

@lantoli lantoli Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the index was created withouth type, it won't return type. API responds same as in creation so I won't use computed.
as it's defined as optional, it has default value = "" (zero value for strings), this is what we want. When refreshing the state the "type" attribute is added to the state with "" but no plan change is shown to the users.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest to have this question/answer represented with a test, not necessarily blocking this PR (unless it's trivial creating it).

Copy link
Contributor

@kyuan-mongodb kyuan-mongodb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Collaborator

@marcosuma marcosuma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be good to add migration and acceptance tests to this change

@lantoli
Copy link
Member Author

lantoli commented Nov 14, 2023

it would be good to add migration and acceptance tests to this change

@marcosuma I added an acceptance test and planning to add more. About migration (and unit) I think the idea is doing it in the new FW resources, this is not migrated yet.

I did manual tests that showed a change in TF state (from unexisting Type and Fields to Type="", Fields=nil) but didn't trigger a plan change to the user.

@@ -156,6 +175,45 @@ func TestAccSearchIndexRS_importBasic(t *testing.T) {
})
}

func TestAccSearchIndexRS_withVector(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcosuma FYI basic acceptance test, planning to add more

@lantoli lantoli marked this pull request as ready for review November 16, 2023 10:38
@lantoli lantoli requested a review from a team as a code owner November 16, 2023 10:38
Copy link
Contributor

Code Coverage

Package Line Rate Health
github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas 2%
github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/framework/validator 68%
github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/testutils 37%
github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/util 17%
Summary 3% (279 / 10376)

@@ -272,6 +293,10 @@ func resourceMongoDBAtlasSearchIndexRead(ctx context.Context, d *schema.Resource
return diag.Errorf("error setting `index_id` for search index (%s): %s", d.Id(), err)
}

if err := d.Set("type", searchIndex.Type); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest to have this question/answer represented with a test, not necessarily blocking this PR (unless it's trivial creating it).

})
}

func TestAccSearchIndexRS_withSearchType(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcosuma FYI this tests checks that you can explicitly pass "search" as index type.

resource.TestCheckResourceAttr(datasourceName, "mappings_dynamic", "true"),
resource.TestCheckResourceAttr(datasourceName, "search_analyzer", searchAnalyzer),
resource.TestCheckResourceAttr(datasourceName, "name", indexName),
resource.TestCheckResourceAttr(datasourceName, "type", ""),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcosuma FYI this tests checks that type is empty when created in that way

Copy link
Member

@AgustinBettati AgustinBettati left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a small detail

Type: schema.TypeString,
Optional: true,
},
"fields": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have we considered adding a conflictsWith validation to ensure vectorSearch attributes are not defined together with regular search attributes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at the end I preferred that all those checks are done in the server. If we do them in client-side there is some risk that both logics differ now or in the future.

@lantoli lantoli merged commit 921c7b2 into master Nov 16, 2023
28 checks passed
@lantoli lantoli deleted the INTMDB-1260_resource_index branch November 16, 2023 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants