Hologres V4.0 provides upgraded vector search capabilities. It supports the HGraph vector search algorithm to deliver high-performance, high-precision, and low-latency vector search. For more information, see HGraph index usage guide (Recommended). This topic describes how to use the Hologres software development kit (SDK) for Python, `holo-search-sdk`, to perform vector search.
Prerequisites
Create an AccessKey pair. For more information, see Create an AccessKey pair.
A Python environment with Python 3.8 or later is installed.
Grant the required permissions to your account. For more information, see Grant permissions to a RAM user.
Install the SDK
Run the following pip command to install the Python SDK. For more information, see holo-search-sdk.
pip install holo-search-sdkSteps
Connect to Hologres
import holo_search_sdk as holo
# Connect to the database.
client = holo.connect(
host="<HOLO_HOST>",
port=<HOLO_PORT>,
database="<HOLO_DBNAME>",
access_key_id="<ACCESS_KEY_ID>",
access_key_secret="<ACCESS_KEY_SECRET>",
schema="public" # Modify the schema as needed.
)
# Establish the connection.
client.connect()
The following table describes the variables.
Variable | Description |
HOLO_HOST | The network address of the Hologres instance. In the Hologres management console, navigate to Hologres > to obtain the network address of the instance. |
HOLO_PORT | The port of the Hologres instance. Go to the Hologres Management Console and navigate to Hologres > to obtain the instance port. |
HOLO_DBNAME | The name of the database created in Hologres. |
ACCESS_KEY_ID | The AccessKey ID of your Alibaba Cloud account. Go to the AccessKey Management page to get the AccessKey ID. |
ACCESS_KEY_SECRET | The AccessKey secret of your Alibaba Cloud account. |
Create a vector table
You can create the table using a Data Definition Language (DDL) statement. The following code provides an example:
create_table_sql = """
CREATE TABLE IF NOT EXISTS <TABLE_NAME> (
id BIGINT PRIMARY KEY,
content TEXT,
vector_column FLOAT4[] CHECK (array_ndims(vector_column) = 1 AND array_length(vector_column, 1) = 3)
);
"""
_ = client.execute(create_table_sql, fetch_result=False)Replace <TABLE_NAME> with the actual name of your table.
Open the vector table
columns = {
"id": ("INTEGER", "PRIMARY KEY"),
"content": "TEXT",
"vector_column": "FLOAT4[]"
}
table = client.open_table("<TABLE_NAME>")Import vector data
data = [
[1, "Hello world", [0.1, 0.2, 0.3]],
[2, "Python SDK", [0.4, 0.5, 0.6]],
[3, "Vector search", [0.7, 0.8, 0.9]]
]
table.insert_multi(data, ["id", "content", "vector_column"])Set a vector index
table.set_vector_index(
column="vector_column",
distance_method="Cosine",
base_quantization_type="rabitq",
max_degree=64,
ef_construction=400
)Query vector data
Basic vector search
# Vector search query_vector = [0.2, 0.3, 0.4] # Limit the number of results. results = table.search_vector( vector=query_vector, column="vector_column", distance_method="Cosine" ).limit(10).fetchall() # Set a minimum distance. results = table.search_vector( vector=query_vector, column="vector_column", distance_method="Cosine" ).min_distance(0.5).fetchall() # Search with an output alias. results = table.search_vector( vector=query_vector, column="vector_column", output_name="similarity_score", distance_method="Cosine" ).fetchall()Exact query by primary key
# Query a single record by primary key. result = table.get_by_key( key_column="id", key_value=1, return_columns=["id", "content", "vector_column"] # Optional. If you do not specify this parameter, all columns are returned. ).fetchone() # Batch query records by a list of primary keys. results = table.get_multi_by_keys( key_column="id", key_values=[1, 2, 3], return_columns=["id", "content"] # Optional. If you do not specify this parameter, all columns are returned. ).fetchall()
Close the connection
# Close the connection.
client.disconnect()FAQ
Q: I received the following error when I import `holo_search_sdk`:
import holo_search_sdk as holo racevack (most recent call last): File "<stdin›" , line 1, in ‹module› File "/usr/local/lib/python3.8/site-packages/holo_search_sdk/__init__.py", line 9, in ‹module> from .client import Client, connect File "/usr/local/lib/python3.8/site-packages/holo_search_sdk/client.py", line 9, in <module> from psycopg. abc import Query File "/usr/local/lib/python3.8/site-packages/psycopg/__init__.py", line 9, in <module> from. import pa # noqa: F401 import early to stabilize side effects File "/usr/local/lib/python3.8/site-packages/psycopg/pq/__init__.py", line 116, in ‹module› import_from_libpqO) File"/usr/local/lib/python3.8/site-packages/psycopg/pq/__init__.py",line 108, in import_from_libpa raise ImportError( ImportError: no pa wrapper available. Attempts made: - couldn't import psycopg 'c' implementation: No module named 'psycopg_c' - couldn't import psycopg 'binary' implementation: No module named 'psycopg_binary' - couldn't import psycopg 'python' implementation: libpa library not foundA: Your Python environment is missing `psycopg-binary`. Run the following command to install it:
pip install psycopg-binary