Azure Cosmos DB create index error cosmosSearchOptions code 197 InvalidIndexSpecificationOption

Andrés Meza Escallón 20 Reputation points
2024-05-14T20:54:19.7633333+00:00

I am following this tutorial about taking advantage of Azure Cosmos DB for Mongo DB vCore's vector similarity search functionality. To do so, I created a Cosmos DB resource using "Try Azure Cosmos DB" with a resource group located in East US.

I connected to the database using this connection string:

import urllib 
import pymongo
COSMOS_MONGO_USER = 'cosmosrgeastus3xxxxxxxxxxxxxxxxxxxxxb'
COSMOS_MONGO_PWD = 'zxxxxxxxxxxxxxxxxxxxxxxxxxxxxx='
COSMOS_MONGO_SERVER = 'cosmosrgeastus318282c5-ac03-48af-82f4db.mongo.cosmos.azure.com'
COSMOS_MONGO_PORT = '10255'

mongo_conn = "mongodb://"+urllib.parse.quote(COSMOS_MONGO_USER)+":"+urllib.parse.quote(COSMOS_MONGO_PWD)+"@"+COSMOS_MONGO_SERVER+':'+COSMOS_MONGO_PORT+"?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@cosmosrgeastus318282c5-ac03-48af-82f4db@"

mongo_client = pymongo.MongoClient(mongo_conn)

Despite a warning ("You appear to be connected to a CosmosDB cluster"), the client seems to be created successfully.

Then I created a database and a collection

# create a database called TutorialDB
db = mongo_client['TutorialDB']

# Create collection if it doesn't exist
COLLECTION_NAME = "CarrierManualCollection"

collection = db[COLLECTION_NAME]

if COLLECTION_NAME not in db.list_collection_names():
    # Creates a unsharded collection that uses the DBs shared throughput
    db.create_collection(COLLECTION_NAME)
    print("Created collection '{}'.\n".format(COLLECTION_NAME))
else:
    print("Using collection: '{}'.\n".format(COLLECTION_NAME))

Which results as expected printing Created collection 'CarrierManualCollection'.

Then, I try to create an IVF index, since "IVF is supported on all cluster tiers, including the free tier".

db.command({
  'createIndexes': COLLECTION_NAME,
  'indexes': [
    {
      'name': 'VectorSearchIndex',
      'key': {
        "contentVector": "cosmosSearch"
      },
      'cosmosSearchOptions': {
        'kind': 'vector-ivf',
        'numLists': 1,
        'similarity': 'COS',
        'dimensions': 1536
      }
    }
  ]
})

But I got this error message:

OperationFailure: cosmosSearchOptions, full error: {'ok': 0.0, 'errmsg': 'cosmosSearchOptions', 'code': 197, 'codeName': 'InvalidIndexSpecificationOption'}

The expected behavior is to get a success message that allows me to continue with the tutorial adding data to the collection.

What am I missing?

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,476 questions
{count} votes

Accepted answer
  1. Oury Ba-MSFT 16,901 Reputation points Microsoft Employee
    2024-05-14T23:00:16.16+00:00

    @Andrés Meza Escallón Thank you for reaching out.

    Did you follow the following tutorial when creating an Azure Cosmos DB Mongo DB Vcore not RU.?

    https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/vcore/quickstart-portal

    Creating a vector search IVF will only work on Azure Cosmos db mongo DB vcore not on Azure cosmos db Mongo DB RU.

    Regards,

    Oury

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Sajeetharan 1,646 Reputation points Microsoft Employee
    2024-05-16T05:51:07.6533333+00:00

    Just checked this, and it works. Can you try the below?

    db.runCommand({
     'createIndexes': "mytest",
      'indexes': [
        {
          'name': 'VectorSearchIndex',
          'key': {
            "contentVector": "cosmosSearch"
          },
          'cosmosSearchOptions': {
            'kind': 'vector-ivf',
            'numLists': 1,
            'similarity': 'COS',
            'dimensions': 1536
          }
        }
      ]
    })