Google Cloud Vector Search Client Library

0.9.0 · active · verified Sat Apr 11

The `google-cloud-vectorsearch` Python client library provides access to Google Cloud's Vector Search service (part of Vertex AI), enabling users to store, manage, and query large-scale vector embeddings for similarity search. As of version 0.9.0, it is in a pre-GA state, with frequent updates released as part of the broader `google-cloud-python` monorepo.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate the `VectorSearchServiceClient` and perform a basic vector similarity search (`match`). It assumes you have an existing Vector Search index endpoint with a deployed index. Remember to set the environment variables or replace the placeholder strings for `project_id`, `location`, `index_endpoint_id`, and `deployed_index_id`.

import os
from google.cloud import vectorsearch

# Set your GCP Project ID and Location
project_id = os.environ.get("GCP_PROJECT_ID", "your-gcp-project-id")
location = os.environ.get("GCP_REGION", "us-central1") # e.g., 'us-central1'

# Set your Vector Search Index Endpoint ID and Deployed Index ID
# These must point to an existing, deployed index endpoint in your project.
index_endpoint_id = os.environ.get("VECTORSEARCH_INDEX_ENDPOINT_ID", "your-index-endpoint-id")
deployed_index_id = os.environ.get("VECTORSEARCH_DEPLOYED_INDEX_ID", "your-deployed-index-id")

# Initialize the client
try:
    client = vectorsearch.VectorSearchServiceClient()
    index_endpoint_name = client.index_endpoint_path(
        project=project_id, location=location, index_endpoint=index_endpoint_id
    )

    # Define a query vector (replace with your actual embedding)
    # The dimension must match the index's dimension. Example for 1536 dimensions:
    query_vector = [0.1] * 1536 # Placeholder: adjust dimensions as needed
    num_neighbors = 5

    # Perform a vector search (match)
    print(f"Searching for {num_neighbors} neighbors in index endpoint: {index_endpoint_name}...")
    response = client.match(
        index_endpoint=index_endpoint_name,
        deployed_index_id=deployed_index_id,
        queries=[
            vectorsearch.MatchQuery(
                vector=query_vector,
                # Optional: Add filters if your index supports them
                # restrict_filters=[vectorsearch.RestrictFilter(namespace="color", allow_tokens=["red"])],
                # numeric_filters=[vectorsearch.NumericFilter(value_int=vectorsearch.IntFilter(value=10), field_name="price")],
            )
        ],
        # Optional: Set to True to retrieve full datapoint metadata
        # return_full_datapoint=True
    )

    if response.results:
        print(f"Found {len(response.results[0].matches)} matches for the first query:")
        for match in response.results[0].matches:
            print(f"  ID: {match.id}, Distance: {match.distance:.4f}")
            if match.datapoint:
                print(f"    Datapoint ID: {match.datapoint.datapoint_id}")
                # Access other datapoint fields like match.datapoint.metadata, match.datapoint.restricts, etc.
    else:
        print("No results found. Check your query, index, and endpoint configuration.")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure you have authenticated to GCP (e.g., `gcloud auth application-default login`),")
    print("your project, location, index endpoint, and deployed index IDs are correct, and the index is deployed.")

view raw JSON →