Search code examples
artificial-intelligencellamallama-indexvector-databasemilvus

How do i specify index_name in llama-index MilvusVectorStore


I'm working on a python rag project. where I'm using milvus as vector database and using llama-index for setting up the project. I have done setting up milvus database, wrote code for embedding the documents, in milvus collection i have created a schema and added 3 indexes among those 2 are scalar indexes and one is a vector index.

def _create_indexes(self, collection: Collection):
    """Create indexes on the collection"""
    try:
        collection.drop_index()  # Clear any existing indexes
    except Exception as e:
        logger.warning(f"No existing indexes to drop: {e}")

    collection.create_index(field_name="account_id", index_name="account_id_index")
    collection.create_index(field_name="file_id", index_name="file_id_index")

    index_params = {
        "index_type": "IVF_FLAT",
        "metric_type": "IP",
        "params": {"nlist": self.config.nlist},
    }
    collection.create_index(
        field_name="embedding",
        index_params=index_params,
        index_name="embedding_index",
    )
    collection.load()

Milvus is getting populated, all 3 indexes are also getting created. After this, for doing the search i was trying to create an instance of MilvusVectorStore in the following way.

self._store = MilvusVectorStore(
    uri=self.config.uri,
    collection_name=collection_name,
    dim=self.config.embedding_dim,
    similarity_metric="IP",
    embedding_field="embedding",
    index_name="embedding_index",
    search_config={
        "metric_type": "IP",
        "params": {"nprobe": self.config.nprobe},
        "index_name": "embedding_index",
    },
    index_config={
        "field_name": "embedding",
        "index_type": "IVF_FLAT",
        "metric_type": "IP",
        "params": {"nlist": self.config.nlist},
        "index_name": "embedding_index",
    },
)

but, since I have multiple indexes, I'm getting the following error

 pymilvus.decorators - ERROR - RPC error: [describe_index], <AmbiguousIndexName: (code=1, message=There are multiple indexes, please specify the index_name.)>, <Time:{'RPC start': '2024-12-17 17:24:17.551945', 'RPC error': '2024-12-17 17:24:17.584774'}>

It looks like I'm making some mistake in initializing the MilvusVectorStore instance, specifically the params index_config and search_config.

I don't know how should i specify the index_name during this process(that's what error says). can anybody help ?


Solution

  • I found a hacky way to specify index name. First, import the pymilvus config.

    from pymilvus.settings import Config
    

    and before creating the instance of MilvusVectorStore, set the index name to the pymilvus config so in my case it becomes,

    # set index name to the config
    Config.IndexName = "embedding_index"
    
    # create MilvusVectorStore instance
    self._store = MilvusVectorStore(
        uri=self.config.uri,
        collection_name=collection_name,
        dim=self.config.embedding_dim,
        similarity_metric="IP",
        embedding_field="embedding",
        index_name="embedding_index",
        search_config={
            "metric_type": "IP",
            "params": {"nprobe": self.config.nprobe},
            "index_name": "embedding_index",
        },
        index_config={
            "field_name": "embedding",
            "index_type": "IVF_FLAT",
            "metric_type": "IP",
            "params": {"nlist": self.config.nlist},
            "index_name": "embedding_index",
        },
    )