Create_vector_search pymongo latest version returns command not found

How can i create a vector search index using pymongo. It returns command not found when i give this. I have already inserted the document field with the text embeddings

{ "definition":  { "mappings": {"dynamic": False,
                             "fields": [
                                         { "type": "vector",
                                            "path": "<field_name",
                                            "numDimensions": 768,
                                             "simiarity": "cosine"
                                           },
                                          {
                                               "type": "filter",
                                                "path": "<field_name>"
                                          }]}},
                           "name": "<index_name"
                          }

Hi @Binu_Ramachandran

Could you share what version and tier of Atlas is running?

If you are running on an M0 (free shared) cluster, creating search indexes via the pymongo driver is not yet supported, so it will need to be done via the Atlas webpage.

However, if you are operating on something higher than an M0, please ensure that you are running on the most recent minor release of Atlas 6.0, or 7.0. Otherwise, you can expect similar behavior to an M0.

I hope this helps.

1 Like

where is this function documented in pymongo ? I don’t see it in 4.6.2

@Matthieu_Mazzolini You can find the pymongo command for create_search_index right here.

Thank you for pointinf it out. It was misleading because OP mentioned create_vectore_search, which is actually what I am looking for. I tried to use this command to create a search index of type vector, but it only creates a vector search. Is this even supported (yet) ?

Thank you

        definition={
            "mappings": {
                "fields": [
                    {
                        "type": "vector",
                        "path": "embeddings.scenarios",
                        "numDimensions": SCENARIOS_VECTOR_LENGTH,
                        "similarity": "cosine",
                    }
                ],
            }
        },
        name="scenario_vector_index",
    )

    collection.create_vector_search(search_model)```

Hi @Matthieu_Mazzolini,

If I understand your question correctly, the configuration you provided should work when you store it in the pymongo.operations.SearchIndexModel object.

Also, the method create_vector_search does not exist.
The method you should use instead is collection.create_search_index this method supports what you are hoping to achieve with create_vector_search .

1 Like

Hi @Jib_Adegunloye , Thank you for helping. I indeed you the model from from pymongo.operations import SearchIndexModel
Also I noticed the method did not exists and I tried to use the one you mention instead. However this lead to creating a an index with the wrong type format (“search” instead of “vector”). As you can see in the picture attach. The first one is incorrect and creating with pymongo. The second one is correct and creating with the same parameters from the mongocloud GUI

from pymongo.operations import SearchIndexModel

    search_model = SearchIndexModel(
        definition={
            "mappings": {
                "fields": [
                    {
                        "type": "vector",
                        "path": "embeddings.scenarios",
                        "numDimensions": SCENARIOS_VECTOR_LENGTH,
                        "similarity": "cosine",
                    }
                ],
            }
        },
        name="scenario_vector_index",
    )
    collection.create_search_index(search_model)

Hey @Matthieu_Mazzolini , apologies for the delay here.
To execute the command via pymongo and have an type: vectorSearch index. For now, you will need to use the Database.command method and createSearchIndexes. Link to docs.

Here’s an example using your current configuration:

vs_index = {
    "definition": {
    	"fields": [
            {
                "numDimensions": SCENARIOS_VECTOR_LENGTH,
                "path": "embeddings.scenarios",
                "similarity": "cosine",
                "type": "vector"
            }
        ],
    },
    "name": "scenario_vector_index",
    "type": "vectorSearch",
}

c.index_db.command(
	{
		"createSearchIndexes": "test_vs_index", 
		"indexes": [vs_index]
	}
)

I am running into the same issue (command not found code 59), using pymongo 4.6.3 and motor 3.4.0. This is running against an M0 tier Atlas database, which is on MongoDB 7.0.8 right now.
I used @Jib_Adegunloye code to see if it would fix things, but it is the same error. Originally I was using pymongo’s create_search_index and SearchIndexModel with the same results.

I believe this is exactly the search index I have already created with the UI and it works in all my queries and such. I just need to create with code so it is more maintainable, and it works in all my scenarios (automation, new deployments, etc.).

What can cause “command not found” in pymongo?

Hi @WarpedPixel and @Matthieu_Mazzolini - What tier of Atlas cluster are you using? The create_search_index command mentioned above can be used with M10+ cluster version only currently-

Ref link

This command can only be run on a deployment hosted on MongoDB Atlas, and requires an Atlas cluster tier of at least M10.

One of my colleague just made me realized that the option is now release in pymongo 4.7

Added the type and kwargs arguments to SearchIndexModel to enable creating vector search indexes in MongoDB Atlas.

https://pymongo.readthedocs.io/en/stable/api/pymongo/operations.html#pymongo.operations.SearchIndexModel

This should solve the problem (not tested yet)

Thanks. The feature works and is implemented in the UI, and the Atlas M0 deployment is running the right version of MongoDB now (7+), but the API is limited. Hopefully it is a deployment lag issue, and not some business decision that will remain this way.

Perhaps the API error case is not implemented correctly to limit to 3 indices in an M0 tier? That limitation is in the UI for M0, and the easiest thing for the API was to just disable it completely for M0 until the 3 index limit could be done robustly in the API. That would make sense.