How to make an index In azure cosmos DB just Like Azure AI Search?
I want to create an Index inside my azure cosmos DB just like an azure AI Search Index, So that I can use that Index with Azure Open AI Rest API?
Azure Cosmos DB
-
Adithya Prasad K • 90 Reputation points • Microsoft External Staff
2025-03-11T12:32:21.87+00:00 Hi Ahmad Shafiq,
Greetings!
To create an index in Azure Cosmos DB similar to an Azure AI Search Index, you can use the Azure portal or REST APIs to configure an indexer that automates data import and indexing. This allows you to make your Cosmos DB data searchable and compatible with Azure OpenAI REST API for enhanced querying capabilities. Steps to Create an Index in Azure Cosmos DB for Azure AI Search
Define the Data Source- Create a data source that connects to your Azure Cosmos DB.
- Use the following structure for the data source definition:
POST https://[service name].search.windows.net/datasources?api-version=2024-05-01-preview Content-Type: application/json api-key: [Search service admin key] { "name": "[my-cosmosdb-ds]", "type": "cosmosdb", "credentials": { "connectionString": "AccountEndpoint=https://[cosmos-account-name].documents.azure.com;AccountKey=[cosmos-account-key];Database=[cosmos-database-name];ApiKind=Gremlin;" }, "container": { "name": "[cosmos-db-collection]", "query": "g.V()" }, "dataChangeDetectionPolicy": { "@odata.type": "#Microsoft.Azure.Search.HighWaterMarkChangeDetectionPolicy", "highWaterMarkColumnName": "_ts" }, "dataDeletionDetectionPolicy": { "@odata.type": "#Microsoft.Azure.Search.SoftDeleteColumnDeletionDetectionPolicy", "softDeleteColumnName": "isDeleted", "softDeleteMarkerValue": "true" } }
Create the Search Index
- Define the fields in your search index that will store the data from your Cosmos DB.
- Use the following structure for the index definition:
POST https://[service name].search.windows.net/indexes?api-version=2024-05-01-preview Content-Type: application/json api-key: [Search service admin key] { "name": "mysearchindex", "fields": [ { "name": "rid", "type": "Edm.String", "key": true, "retrievable": true, "searchable": true }, { "name": "label", "type": "Edm.String", "searchable": true, "retrievable": true } ] }
Create the Indexer
- Set up an indexer that connects the data source to the search index.
- Use the following structure for the indexer definition:
POST https://[service name].search.windows.net/indexers?api-version=2024-05-01-preview Content-Type: application/json api-key: [Search service admin key] { "name": "[my-cosmosdb-indexer]", "dataSourceName": "[my-cosmosdb-ds]", "targetIndexName": "[mysearchindex]", "parameters": { "batchSize": null, "maxFailedItems": 0, "maxFailedItemsPerBatch": 0 }, "fieldMappings": [] }
- Run the Indexer
- The indexer will run automatically upon creation. You can also run it on demand or schedule it for regular intervals.
- Monitor Indexer Status
- Check the status of the indexer to ensure it is processing data correctly. Use the following request to get the status:
GET https://[service name].search.windows.net/indexers/[my-cosmosdb-indexer]/status?api-version=2024-05-01-preview Content-Type: application/json api-key: [admin key]
- Check the status of the indexer to ensure it is processing data correctly. Use the following request to get the status:
Ensure that your Azure Cosmos DB is set up with the appropriate indexing policy and permissions.
Use the same region for both Azure AI Search and Azure Cosmos DB to minimize latency.
Make sure to handle any potential errors during the indexing process by checking the execution history and status notifications.
I would request you to refer the below mentioned links for more informationhttps://learn.microsoft.com/en-us/azure/search/search-how-to-create-indexers?tabs=portal
https://learn.microsoft.com/en-us/azure/search/search-howto-index-cosmosdb?tabs=portal-check-indexerI hope this information helps. Please do let us know if you have any further queries.
-
Ahmad Shafiq • 150 Reputation points
2025-03-11T15:51:24.3933333+00:00 Actually I want to add azure cosmos Db for Mongodo vcore instead of Azure Ai search inside Azure openAi
Like this
For this purpose I want to create An index inside azure cosmos db so that i can harness its abilities
-
Ahmad Shafiq • 150 Reputation points
2025-03-11T15:53:29.4733333+00:00 I want to create a vector Index inside the azure cosmos db for mongodb vcore so that i can harness the capability of azure open ai For RAG i dont want to use AI Search
-
Adithya Prasad K • 90 Reputation points • Microsoft External Staff
2025-03-12T09:16:36.7166667+00:00 Hi Ahmad Shafiq
Greetings!
To create a vector index in Azure Cosmos DB for MongoDB vCore and harness the capabilities of Azure OpenAI for Retrieval-Augmented Generation (RAG), follow these steps:Setting Up the Database and Collection
- Establish a Connection: Use the
pymongo
library to connect to your Azure Cosmos DB for MongoDB vCore instance. Replace<USERNAME>
,<PASSWORD>
, and<VCORE_CLUSTER_NAME>
with your actual credentials and cluster name.
import pymongo mongo_conn = "mongodb+srv://<USERNAME>:<PASSWORD>@<VCORE_CLUSTER_NAME>.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000" mongo_client = pymongo.MongoClient(mongo_conn)
- Create a Database and Collection: Define the database and collection names, then create them within your Cosmos DB instance.
DATABASE_NAME = "AdgenDatabase" COLLECTION_NAME = "AdgenCollection" mongo_client.drop_database(DATABASE_NAME) db = mongo_client[DATABASE_NAME] collection = db[COLLECTION_NAME] if COLLECTION_NAME not in db.list_collection_names(): db.create_collection(COLLECTION_NAME) print(f"Created collection '{COLLECTION_NAME}'.") else: print(f"Using collection: '{COLLECTION_NAME}'.")
Creating a Vector Index
- Create the Vector Index: To perform efficient vector similarity searches within your collection, create a vector index. Azure Cosmos DB supports different types of vector indexes, such as IVF and HNSW.
# Example for creating a vector index (specific commands may vary) collection.create_index([("vector_field", pymongo.ASCENDING)], name="vector_index")
Additional Resources
For more detailed information and examples, you can refer to the following resources:
- Integrated vector store - Azure Cosmos DB for MongoDB vCore1
- Indexes on Azure Cosmos DB for MongoDB vCore2
- Indexing Best Practices in Azure Cosmos DB for MongoDB vCore3
By following these steps, you can effectively create a vector index in Azure Cosmos DB for MongoDB vCore and leverage the capabilities of Azure OpenAI for RAG without using AI Search. If you have any further questions or need additional assistance, feel free to ask!
- Establish a Connection: Use the
-
Ahmad Shafiq • 150 Reputation points
2025-03-12T10:43:08.7433333+00:00 Thanks I have already implemented this thing,
Thanks it works!
Sign in to comment