Can i create a vectorSearch index with createSearchIndex command?

I’m trying to create a vectorSearch index programmatically. I can create a search index with a knnVector, using createSearchIndex command but for a vector index defined like this:

fields: [{
path: ‘embedding’,
numDimensions: 1536,
similarity: ‘cosine’,
type: ‘vector’
}]
I couldn’t coerce the createSearchIndex command to create such an index. I’m developing a RAG AI application and need to create vectorSearch indexes on the fly. Is there a way to create this sort of index outside of the UI?

1 Like

Hi Guy,

Sorry for the delay in response here. We are working on updating our docs to mention this as the change landed very recently, which should allow you to use the createSearchIndex command specifying type: vectorSearch , which is a sibling field to the index definition.

This is supported in the Atlas Admin API as well as mongosh 2.1.2 as well as the Atlas CLI 1.14.3, both running MongoDB v7.0.5.

I hope this helps!

2 Likes

OK, great. This works:

db.xx.createSearchIndex(
“vector1”,
“vectorSearch”,
{
fields: [{
path: “embedding”,
numDimensions: 1536,
similarity: “cosine”,
type: “vector”
}]

 }

)

3 Likes

Hey Henry!
Any chance to get a nodejs snippet for creating the index?
Tried it with the specified versions of mongosh and MongoDB but I could not make it work.

Thanks!

Hi @Omri_Keret!

I’m sorry you couldn’t get it working with mongosh. I’m. curious to know what error you were seeing if you don’t mind sharing.

For node.js you should be able to create that same index with the following syntax:

const index1 = {
    name: "vector1",
    type: "vectorSearch",
    definition: {
        fields: [{
            path: “embedding”,
            numDimensions: 1536,
            similarity: “cosine”,
            type: “vector”
        }]
    }
}
await collection.createSearchIndex(index1);

You can also find more details about the node.js search index management commands here.

Hey! I use Mongoose and for some reason, the client I receive does not have the createSearchIndex function on it.
I managed to sort it out with the createSearchIndexes function.

const client = mongoose.connection.getClient();
const vectorSearchIndexCommand = {
  createSearchIndexes: collectionName,  // Name of   our collection
  indexes: [
    {
      name: searchIndexName,
      type: 'vectorSearch',
      definition: {
        fields: [
          {
            path: "embedding",
            type: 'vector',
            numDimensions: 1536,
            similarity: 'cosine',
          },
        ],
      },
    },
  ],
};

await client.db().command(vectorSearchIndexCommand);

Does the Atlas Admin API work for locally hosted Atlas. If so do you know where I can find the key? Thanks. Does local Atlas have a web UI?

Unsure why I cannot run the above, I get this error:
MongoServerError: command not found

I am running the latest mongodb version 6.5 with latest mongoose

Any thoughts?

I found out why, mongodb cli or its nodejs driver is restricted to the ‘expensive’ M10+ clusters. Why? I wish I knew.
Apparently you can create indexes even with an M0 cluster via interface or atlas api, but if you want to do in nodejs via mongodb driver, nope. Need to pay. I’d love to understading some reasoning behind this.

On local or cloud Atlas instance you get 3 vector search indexes for free on M0 (free account)