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]
	}
)