create_vector_index
Generate a vector index on a specified field within a table to enable efficient vector similarity searches in Baidu Vector Database. Supports HNSW, HNSWPQ, HNSWSQ index types and L2, COSINE, IP metrics.
Instructions
Create a vector index on a vector type field in the Mochow instance.
Args:
table_name (str): Name of the table.
index_name (str): Name of the index.
field_name (str): Name of the vector field.
index_type (str): Type of vector index. Supported values are "HNSW", "HNSWPQ", "HNSWSQ".
metric_type (str): Distance metric. Supported values are "L2", "COSINE", "IP".
params (Optional[dict[str, Any]]): Additional vector index parameters.
Returns:
str: A message indicating the success of index creation.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| field_name | Yes | ||
| index_name | Yes | ||
| index_type | No | HNSW | |
| metric_type | No | L2 | |
| params | No | ||
| table_name | Yes |
Implementation Reference
- src/mochow_mcp_server/server.py:564-589 (handler)MCP tool handler for 'create_vector_index', decorated with @mcp.tool(), which delegates to the MochowConnector's method.@mcp.tool() async def create_vector_index( table_name: str, index_name: str, field_name: str, index_type: str = "HNSW", metric_type: str = "L2", params: Optional[dict[str, Any]] = None, ctx: Context = None) -> str: """ Create a vector index on a vector type field in the Mochow instance. Args: table_name (str): Name of the table. index_name (str): Name of the index. field_name (str): Name of the vector field. index_type (str): Type of vector index. Supported values are "HNSW", "HNSWPQ", "HNSWSQ". metric_type (str): Distance metric. Supported values are "L2", "COSINE", "IP". params (Optional[dict[str, Any]]): Additional vector index parameters. Returns: str: A message indicating the success of index creation. """ connector = ctx.request_context.lifespan_context.connector await connector.create_vector_index(table_name, index_name, field_name, index_type, metric_type, params) return f"Vector index '{index_name}' created successfully"
- Core helper method in MochowConnector class that implements the logic to create a vector index using the pymochow client, including validation and parameter mapping.async def create_vector_index( self, table_name: str, index_name: str, field_name: str, index_type: str = "HNSW", metric_type: str = "L2", params: Optional[dict[str, Any]] = None, ) -> bool: """ Create a vector index on a given vector field. Args: table_name (str): Name of the table. index_name (str): Name of the index. field_name (str): Name of the vector field. index_type (str): Type of vector index. Supported values are "HNSW", "HNSWPQ", "PUCK". metric_type (str): Distance metric. Supported values are "L2", "COSINE", "IP". params (Optional[dict[str, Any]]): Additional vector index parameters. Returns: bool: True if the index is created successfully, False otherwise. """ if self.database is None: raise ValueError("Switch to the database before create vector index") # check vector index index_existed = True try: self.database.table(table_name).describe_index(index_name) except ServerError as e: if e.code == ServerErrCode.INDEX_NOT_EXIST: index_existed = False else: raise ValueError(f"Failed to get index detail: {str(e)}") # index already existed with same name if index_existed: raise ValueError(f"Index already existed with same name '{index_name}'") # create vector index index_metric_type = None for k, v in MetricType.__members__.items(): if k == metric_type: index_metric_type = v if index_metric_type is None: raise ValueError("Only the three metric types of L2, COSINE, and IP are supported.") indexes = [] if index_type == "HNSW": indexes.append( VectorIndex( index_name=index_name, index_type=IndexType.HNSW, field=field_name, metric_type=index_metric_type, auto_build=False, params=HNSWParams(m=params.get("M", 16), efconstruction=params.get("efConstruction", 200)))) elif index_type == "HNSWPQ": indexes.append( VectorIndex( index_name=index_name, index_type=IndexType.HNSW, field=field_name, metric_type=index_metric_type, auto_build=False, params=HNSWPQParams( m=params.get("M", 16), efconstruction=params.get("efConstruction", 200), NSQ=params.get("NSQ", 8), samplerate=params.get("sampleRate", 1.0)))) elif index_type == "PUCK": indexes.append( VectorIndex( index_name=index_name, index_type=IndexType.HNSW, field=field_name, metric_type=index_metric_type, auto_build=False, params=PUCKParams( coarseClusterCount=params.get("coarseClusterCount", 5), fineClusterCount=params.get("fineClusterCount", 5)))) else: raise ValueError("Only the three vector index types of HNSW, HNSWPQ, PUCK are supported.") try: self.database.table(table_name).create_indexes(indexes) return True except Exception as e: raise ValueError(f"Failed to create vector index: {str(e)}")
- src/mochow_mcp_server/server.py:564-565 (registration)The @mcp.tool() decorator registers the create_vector_index function as an MCP tool.@mcp.tool() async def create_vector_index(
- Docstring providing input schema and description for the tool parameters.""" Create a vector index on a vector type field in the Mochow instance. Args: table_name (str): Name of the table. index_name (str): Name of the index. field_name (str): Name of the vector field. index_type (str): Type of vector index. Supported values are "HNSW", "HNSWPQ", "HNSWSQ". metric_type (str): Distance metric. Supported values are "L2", "COSINE", "IP". params (Optional[dict[str, Any]]): Additional vector index parameters. Returns: str: A message indicating the success of index creation. """