# Leveraging Atlas Vector Search With HashiCorp Terraform: Empowering Semantic Search in Modern Applications

Last year, MongoDB announced the general availability of Atlas Vector Search, a new capability in Atlas that allows developers to search across data stored in MongoDB based on its semantic meaning using high dimensional vectors (i.e., “embeddings”) created by machine learning models. 

This allows developers to build intelligent applications that can understand and process human language in a way traditional, text-based search methods cannot since they will only produce an exact match for the query. 

For example, searching for “warm winter jackets” on an e-commerce website that only supports text-based search might return products with the exact match keywords "warm," "winter," and "jackets." Vector search, on the other hand, understands the semantic meaning of "warm winter jackets'' as apparel designed for cold temperatures. It retrieves items that are not only labeled as "winter jackets” but are specifically designed for warmth, including products that might be described with related terms like "insulated," giving users more helpful search results. 

Integrating Atlas Vector Search with infrastructure-as-code (IaC) tools like HashiCorp Terraform can then streamline and optimize your development workflows, ensuring that sophisticated search capabilities are built directly into the infrastructure deployment process. 

This guide will walk you through how to get started with Atlas Vector Search through our HashiCorp Terraform Atlas provider. Let’s get started! 

### Pre-requisites

 - Create a MongoDB Atlas account. 
 - Install HashiCorp Terraform on your terminal or sign up for a free Terraform Cloud account.
 - Create MongoDB Atlas programmatic API keys and associate them with Terraform. 
 - Select an IDE of your choice. For this tutorial, we will be using VS Code.

## Step 1: Deploy Atlas dedicated cluster with Atlas Search Nodes

First, we need to deploy basic Atlas resources to get started. This includes an Atlas project, an M10 dedicated Atlas cluster (which is pay-as-you-go, great for development and low-traffic applications), a database user, and an IP Access List Entry. 

**Note**: When configuring your MongoDB Atlas cluster with Terraform, it's important to restrict IP access to only the IP address from which the Terraform script will be deployed. This minimizes the risk of unauthorized access. 

In addition, as part of this tutorial, we will be using Atlas Search Nodes (optional). These provide dedicated infrastructure for Atlas Search and Vector Search workloads, allowing you to fully scale search independent of database needs. Incorporating Search Nodes into your Atlas deployment allows for better performance at scale and delivers workload isolation, higher availability, and the ability to optimize resource usage. 

Lastly, when using Terraform to manage infrastructure, it is recommended to maintain organized file management practices. Typically, your Terraform configurations/scripts will be written in files with the `.tf` extension, such as `main.tf`. This file, which we are using in this tutorial, contains the primary configuration details for deploying resources and should be located ideally in a dedicated project directory on your local machine or on Terraform Cloud. 

See the below Terraform script as part of our `main.tf` file: 

```
terraform {
  required_providers {
    mongodbatlas = {
      source = "mongodb/mongodbatlas"
    }
  }
  required_version = ">= 0.13"
}

resource "mongodbatlas_project" "exampleProject" {
  name   = "exampleProject"
  org_id = "63234d3234ec0946eedcd7da"
}

resource "mongodbatlas_advanced_cluster" "exampleCluster" {
  project_id   = mongodbatlas_project.exampleProject.id
  name         = "ClusterExample"
  cluster_type = "REPLICASET"

  replication_specs {
    region_configs {
      electable_specs {
        instance_size = "M10"
        node_count    = 3
      }
      provider_name = "AWS"
      priority      = 7
      region_name   = "US_EAST_1"
    }
  }
}

resource "mongodbatlas_search_deployment" "exampleSearchNode" {
  project_id   = mongodbatlas_project.exampleProject.id
  cluster_name = mongodbatlas_advanced_cluster.exampleCluster.name
  specs = 
    {
      instance_size = "S20_HIGHCPU_NVME"
      node_count    = 2
    }
  ]
}

resource "mongodbatlas_database_user" "testUser" {
  username           = "username123"
  password           = "password-test123"
  project_id         = mongodbatlas_project.exampleProject.id
  auth_database_name = "admin"

  roles {
    role_name     = "readWrite"
    database_name = "dbforApp"
  }
}

resource "mongodbatlas_project_ip_access_list" "test" {
  project_id = mongodbatlas_project.exampleProject.id
  ip_address = "174.218.210.1"
}
```

**Note**: Before deploying, be sure to store your MongoDB Atlas programmatic API keys created as part of the prerequisites as [environment variables. To deploy, you can use the below commands from the terminal: 

```
terraform init 
terraform plan
terraform apply 
```

## Step 2: Create your collections with vector data 

For this tutorial, you can create your own collection of vectorized data if you have data to use. 

Alternatively, you can use our sample data. This is great for testing purposes. The collection you can use is the "sample_mflix.embedded_movies" which already has embeddings generated by Open AI. 

To use sample data, from the Atlas UI, go into the Atlas cluster Overview page and select “Atlas Search” at the top of the menu presented. 

Then, click “Load a Sample Dataset.”

## Step 3: Add vector search index in Terraform configuration 

Now, head back over to Terraform and create an Atlas Search index with type “vectorSearch.” If you are using the sample data, also include a reference to the database “sample_mflix” and the collection “embedded_movies.” 

Lastly, you will need to set the “fields” parameter as per our example below. See our documentation to learn more about how to index fields for vector search and the associated required parameters. 

```
resource "mongodbatlas_search_index" "test-basic-search-vector" {
  name   = "test-basic-search-index"  
  project_id = mongodbatlas_project.exampleProject.id
  cluster_name = mongodbatlas_advanced_cluster.exampleCluster.name
  type = "vectorSearch"
  database = "sample_mflix"
  collection_name = "embedded_movies"
  fields = <<-EOF
    {
        "type": "vector",
        "path": "plot_embedding",
        "numDimensions": 1536,
        "similarity": "euclidean"
    }]
    EOF
}
```

To deploy again, you can use the below commands from the terminal: 
```
terraform init 
terraform plan
terraform apply 
```

If your deployment was successful, you should be greeted with “Apply complete!” 
![(Terraform in terminal showcasing deployment)

To confirm, you should be able to see your newly created Atlas Search index resource in the Atlas UI with Index Type “vectorSearch” and Status as “ACTIVE.” 

## Step 4: Get connection string and connect to the MongoDB Shell to begin Atlas Vector Search queries 

While still in the Atlas UI, go back to the homepage, click “Connect” on your Atlas cluster, and select “Shell.” 

This will generate your connection string which you can use in the MongoDB Shell to connect to your Atlas cluster. 

### All done

Congratulations! You have everything that you need now to run your first Vector Search queries.

With the above steps, teams can leverage Atlas Vector Search indexes and dedicated Search Nodes for the Terraform MongoDB Atlas provider to build a retrieval-augmented generation, semantic search, or recommendation system with ease. 

The HashiCorp Terraform Atlas provider is open-sourced under the Mozilla Public License v2.0 and we welcome community contributions. To learn more, see our contributing guidelines.

The fastest way to get started is to create a MongoDB Atlas account from the AWS Marketplace or Google Cloud Marketplace. To learn more about the Terraform provider, check out the documentation, solution brief, and tutorials, or get started today.  

Go build with MongoDB Atlas and the HashiCorp Terraform Atlas provider today!