Rank search results

This page describes how to rank your search results for applications using the Vertex AI ranking model endpoint.

The Vertex AI ranking API takes a list of documents and ranks those documents based on how relevant the documents are to a given query (a search string). When you use the ai.rank() function, it returns scores for how well a document answers a given query.

To use instructions on this page, you must have an understanding of AlloyDB for PostgreSQL and be familiar with generative AI concepts.

AlloyDB reserves the ai schema, and tries to create this schema when you install the google_ml_integration extension. If the schema creation fails, then use the functions with the same name in the google_ml schema.

Before you begin

Before you rank search results, make sure that you meet the following prerequisites.

Enable the Discovery Engine API

Console

  1. Enable the API
  2. In the Confirm project step, click Next to confirm the name of the project you are going to make changes to.
  3. In the Enable APIs step, click Enable to enable the Discovery Engine API. If you already enabled this API, you won't see it listed here.

gcloud

To use ranking models, you must enable the Discovery Engine API.
Replace PROJECT_ID with your Google Cloud project ID and PROJECT_NUMBER with your corresponding project number.

    # Enable Discovery Engine API
    gcloud services enable discoveryengine.googleapis.com --project=PROJECT_ID
    gcloud projects add-iam-policy-binding PROJECT_ID \
    --member="serviceAccount:service-PROJECT_NUMBER@gcp-sa-alloydb.iam.gserviceaccount.com" \
    --role="roles/discoveryengine.viewer"

Model registration for ranking isn't required for Vertex AI models. You can use the Vertex AI model name as the model_id, which is shown in the following example.

    SELECT index, score
    FROM
      ai.rank(
        model_id => 'semantic-ranker-default-003',
        search_string => 'Affordable family-friendly vacation spots in Southeast Asia?',
        documents =>
    ARRAY[
      'Luxury resorts in South Korea',
      'Family vacation packages for Vietnam: Ha Long Bay and Hoi An',
      'Budget-friendly beaches in Thailand perfect for families',
      'A backpacker guide to solo travel in India'])

A common use case for the semantic ranker is to rerank the results returned by vector search for better query ordering. The following example shows how to use the semantic ranking model for this use case. The example retrieves an initial result set for the query personal fitness equipment using vector search. These results are then re-ranked to return the top five results.

    WITH initial_ranking AS (
      SELECT id, description, ROW_NUMBER() OVER () AS ref_number
      FROM product
      ORDER BY
        embedding <=> google_ml.embedding(
          'text-embedding-005', 'personal fitness equipment')::vector
      LIMIT 10
    ), reranked_results AS (
      SELECT index, score
      FROM ai.rank(
          model_id => 'semantic-ranker-default-003',
          search_string => 'personal fitness equipment',
          documents => (SELECT ARRAY_AGG(description ORDER BY ref_number) FROM initial_ranking),
          top_n => 5)
    )
    SELECT id, description
    FROM initial_ranking, reranked_results
    WHERE initial_ranking.ref_number = reranked_results.index
    ORDER BY reranked_results.score DESC;
  

For a list of available models and use cases, see Supported models.

Integrate with Vertex AI and install the extension

  1. Integrate with Vertex AI.
  2. Ensure that the latest version of google_ml_integration is installed.
    1. To check the installed version, run the following command:

              SELECT extversion FROM pg_extension WHERE extname = 'google_ml_integration';
              extversion 
              ------------
              1.4.3
              (1 row)
            
    2. If the extension isn't installed or if the installed version is earlier than 1.4.3, update the extension by running the following commands:

              CREATE EXTENSION IF NOT EXISTS google_ml_integration;
      
              ALTER EXTENSION google_ml_integration UPDATE;
            

      If you experience issues when you run the preceding commands, or if the extension isn't updated to version 1.4.3 after you run the preceding commands, contact AlloyDB support.

    3. After you ensure that the version is current, install the preview functionality by running the upgrade_to_preview_version procedure:

              CALL google_ml.upgrade_to_preview_version();
      
              SELECT extversion FROM pg_extension WHERE extname = 'google_ml_integration';
              extversion 
              ------------
              1.4.4
              (1 row)
            

Required roles

To get the permissions that you need to use ranking models from Discovery Engine, ask your administrator to grant you the Discovery Engine Viewer (roles/discoveryengine.viewer) Identity and Access Management (IAM) role on your project. For more information about granting roles, see Manage access to projects, folders, and organizations.

You might also be able to get the required permissions through custom roles or other predefined roles.

Rank your search results

The following SQL query shows how to rank your search results :

SELECT
  ai.rank(
    model_id => 'MODEL_ID',
    search_string => 'SEARCH_STRING',
    documents => ARRAY['DOCUMENT_1', 'DOCUMENT_2', 'DOCUMENT_3']);

Replace the following:

Parameter Description
MODEL_ID A unique ID for the model endpoint that you define.
SEARCH_STRING A search string against which the records are ranked.
DOCUMENTS A unique string that identifies the record.

For a list of the supported Vertex AI ranking models, see Supported models.

Examples

To rank search results using a Vertex AI ranking model, run the following query:

SELECT index, score
FROM
  ai.rank(
    model_id => 'semantic-ranker-default-003',
    search_string => 'AlloyDB is a PostgreSQL compatible AI database that is ready for production.',
    documents =>
      ARRAY[
        'Alloys are made from combination of metals',
        'The best enterprise-ready PostgreSQL database.',
        'You can feel the heat in Alloy apartments.']);

The response is a table that shows each document and the score based on relevance to the search query. The following is the sample response:

 index | score
-------+------------
     2 |  0.33
     1 |  0.28
     3 |  0.16
(3 rows)

Consider an example AlloyDB database with a list of review descriptions that are converted to embeddings. The following sample code snippet shows how to use the ranking model to retrieve the name of the top-ranked products based on their review descriptions' semantic similarity to a query.

WITH
  initial_results AS (
    SELECT product_id, name, review, review_id,
            ROW_NUMBER() OVER () AS ref_number
    FROM user_reviews
    ORDER BY
      review_desc_embedding <=> google_ml.embedding(
        'text-embedding-005', 'good desserts')
    LIMIT 100
  ),
reranked AS (
    SELECT
      ai.rank(
        model_id => 'semantic-ranker-default-003',
        search_string => 'good desserts',
        documents => ARRAY_AGG(review ORDER BY ref_number))
    FROM initial_results
  )
SELECT product_id, name
FROM initial_results, reranked
WHERE initial_results.ref_number = reranked.index
ORDER BY reranked.score DESC;

What's next