Yandex Cloud
Documentation
PresentationContact UsTry it for free
© 2026 Direct Cursus Technology L.L.C.
Tutorials
    • All tutorials
      • Creating a PostgreSQL cluster for 1C:Enterprise
      • Troubleshooting Managed Service for PostgreSQL cluster performance
      • Managed Service for PostgreSQL performance analysis and optimization
      • Logical replication in PostgreSQL
      • Migrating a database from a third-party PostgreSQL cluster to Managed Service for PostgreSQL
      • Migrating a database from Managed Service for PostgreSQL
      • Migrating a Managed Service for PostgreSQL cluster to a different version
      • Migrating a database from Managed Service for PostgreSQL to Object Storage
      • Migrating data from Managed Service for PostgreSQL to Managed Service for MySQL® using Data Transfer
      • Migrating data from Managed Service for PostgreSQL to Managed Service for OpenSearch using Data Transfer
      • Delivering data from Managed Service for PostgreSQL to Managed Service for Apache Kafka® using Data Transfer
      • Delivering data from Managed Service for PostgreSQL to Managed Service for Apache Kafka® using Debezium
      • Delivering data from Managed Service for PostgreSQL to Managed Service for YDB using Data Transfer
      • Migrating data from Object Storage to Managed Service for PostgreSQL via Data Transfer
      • PostgreSQL change data capture and delivery to YDS
      • Fixing string sorting issues in PostgreSQL after a glibc upgrade
      • Using a Yandex Lockbox secret in a PySpark job to connect to Yandex Managed Service for PostgreSQL
      • Configuring permissions for access to a secret created by Connection Manager for a Managed Service for PostgreSQL user
      • Executing analytical queries in Managed Service for PostgreSQL with processing in Managed Service for ClickHouse®
      • Running a vector search with `vector` in Yandex Managed Service for OpenSearch

In this article:

  • Getting started
  • Required paid resources
  • Set up your infrastructure
  • Prepare your test data
  • Run search queries
  • Create an index to make your search faster
  • HNSW
  • IVFFlat
  • Delete the resources you created
  1. Building a data platform
  2. PostgreSQL
  3. Running a vector search with `vector` in Yandex Managed Service for OpenSearch

Running a vector search with vector in Yandex Managed Service for OpenSearch

Written by
Yandex Cloud
Updated at November 16, 2026
View in Markdown
  • Getting started
    • Required paid resources
  • Set up your infrastructure
  • Prepare your test data
  • Run search queries
  • Create an index to make your search faster
    • HNSW
    • IVFFlat
  • Delete the resources you created

With the vector extension, you can store vector data and run a vector search in Yandex Managed Service for PostgreSQL.

The extension supports:

  • Exact and approximate nearest neighbor search.
  • Various vector types: single-precision, half-precision, binary, and sparse vectors.
  • Several vector comparison metrics: L2 (Euclidean distance), cosine distance, dot product, L1 (Manhattan distance), Hamming and Jaccard distance.
  • Any programming language that supports a connection to PostgreSQL.

Vector search can be combined with familiar PostgreSQL and SQL features.

By default, vector performs exact search where it compares all vectors. For large data volumes, you can create vector indexes and use approximate search. This search is faster, but the results may prove less accurate.

vector supports two index types:

  • HNSW, which organizes vectors into a data structure which allows you to go from one similar vector to the next and find nearest neighbors without going through the entire table. This structure is based on a multilayer graph.
  • IVFFlat, which breaks all vectors up into several groups (lists). The search does not scan all groups but only those that are closest to the target vector. Compared to HNSW, this index is built faster and uses less memory; however, it is inferior in terms of quality and search speed.

The vector extension is available in Managed Service for PostgreSQL clusters of version 13 and higher. For more information about vector, see this tutorial.

To run a vector search in Managed Service for PostgreSQL:

  1. Set up your infrastructure.
  2. Prepare your test data.
  3. Run search queries.
  4. Create an index to make your search faster.

If you no longer need the resources you created, delete them.

Getting startedGetting started

Sign up for Yandex Cloud and create a billing account:

  1. Navigate to the management console and log in to Yandex Cloud or create a new account.
  2. On the Yandex Cloud Billing page, make sure you have a billing account linked and it has the ACTIVE or TRIAL_ACTIVE status. If you do not have a billing account, create one and link a cloud to it.

If you have an active billing account, you can create or select a folder for your infrastructure on the cloud page.

Learn more about clouds and folders here.

Required paid resourcesRequired paid resources

  • Managed Service for PostgreSQL cluster: use of computing resources allocated to hosts, as well as the storage and backup size (see Managed Service for PostgreSQL pricing).
  • Public IP addresses if public access is enabled for cluster hosts (see Yandex Virtual Private Cloud pricing).

Set up your infrastructureSet up your infrastructure

  1. Create a cloud network named demo-network.

    When creating a network, three subnets in different availability zones are created automatically.

  2. In demo-network, create a security group named mpg-sg for the Managed Service for PostgreSQL cluster and add the following inbound rule to the group to allow internet access to the cluster:

    • Port range: 6432.
    • Protocol: TCP.
    • Destination name: Address range.
    • IPv4 CIDR: 0.0.0.0/0.
  3. Create a Managed Service for PostgreSQL cluster of any suitable configuration with the following settings:

    • Version: 13 or higher.

    • Network: demo-network.

    • Security group: mpg-sg

    • Database: pgdb.

    When creating your cluster, enable host public access.

    Note

    Public access to cluster hosts is required if you plan to connect to the cluster via the internet. This connection option is simpler and is recommended for the purposes of this guide. You can connect to non-public hosts as well but only from Yandex Cloud virtual machines located in the same cloud network as the cluster.

  4. In the Managed Service for PostgreSQL cluster, add the vector extension to the pgdb database.

Prepare your test dataPrepare your test data

  1. Connect to the Managed Service for PostgreSQL cluster.

  2. Create a column vector of size 3:

    CREATE TABLE items (
      id bigserial PRIMARY KEY, 
      embedding vector(3)
    );
    
  3. Add vectors:

    INSERT INTO items (embedding) 
    VALUES
    ('[1,2,3]'),
    ('[4,5,6]'),
    ('[7,8,9]'),
    ('[10,11,12]'),
    ('[13,14,15]'),
    ('[16,17,18]');
    

Run search queriesRun search queries

  1. Find the three nearest neighbors using L2 distance:

    SELECT *
    FROM items
    ORDER BY embedding <-> '[3,1,2]'
    LIMIT 3;
    

    To get a distance, the vector extension uses the following functions:

    • <->: L2 distance (Euclidean distance).
    • <#>: Negative dot product.
    • <=>: Cosine distance.
    • <+>: L1 distance (Manhattan distance).
    • <~>: Hamming distance (for binary vectors).
    • <%>: Jaccard distance (for binary vectors).
  2. Find three lines that bear the closest resemblance to the first one:

    SELECT *
    FROM items
    WHERE id != 1
    ORDER BY embedding <->
    (
    SELECT embedding
    FROM items
    WHERE id = 1
    )
    LIMIT 3;
    
  3. Find the lines that lie within the boundaries of the set distance:

    SELECT *
    FROM items
    WHERE embedding <-> '[3,1,2]' < 15;
    
  4. Get the distance between each vector and vector [3,1,2]:

    SELECT embedding <-> '[3,1,2]'
    AS distance
    FROM items;
    
  5. To calculate the dot product, multiply the result by -1, since the <#> returns the negative dot product:

    SELECT (embedding <#> '[3,1,2]') * -1 AS inner_product
    FROM items;
    
  6. To find out how similar each vector is to [3,1,2] using cosine similarity, use 1 - cosine distance:

    SELECT 1 - (embedding <=> '[3,1,2]') AS cosine_similarity
    FROM items;
    
  7. Find the mean vector:

    SELECT AVG(embedding)
    FROM items;
    

Create an index to make your search fasterCreate an index to make your search faster

Create an index of each type: HNSW and IVFFlat.

HNSWHNSW

  1. Create an HNSW index for L2 distance search:

    CREATE INDEX ON items
    USING hnsw (embedding vector_l2_ops)
    WITH (
    m = 16,
    ef_construction = 64
    );
    

    Where:

    • m: Maximum number of connections a graph node can have at each layer. The default value is 16.
    • ef_construction: Number of candidates considered when building the graph. The default value is 64. The higher the value, the greater the probability of finding the most relevant records; however, index build time increases, and insertion operations become slower.

    These parameters are set one time when creating an index. To modify their values, re-create the index.

    You need to create a dedicated HNSW index for each distance calculation function. To learn more, see the vector extension guide.

  2. Configure the search settings:

    SET hnsw.ef_search = 3;
    

    Where ef_search is the number of vectors the algorithm considers during the search. The default value is 40. The higher the value, the greater the probability of finding the most relevant records; however, the query runs slower.

    Unlike the m and ef_construction index parameters, the ef_search parameter can be modified before running queries.

  3. Run a search for the nearest neighbors:

    SELECT *
    FROM items
    ORDER BY embedding <-> '[3,1,2]'
    LIMIT 3;
    

    If the search results are not accurate enough, increase the ef_search value and repeat the query.

IVFFlatIVFFlat

  1. Create an IVFFlat index for L2 distance search:

    CREATE INDEX ON items
    USING ivfflat (embedding vector_l2_ops)
    WITH (
    lists = 3
    );
    

    Where lists is the number of groups (lists) into which the vectors will be divided. The recommended value depends on the amount of data. The more groups there are, the faster the search, but the search quality may decrease.

    This parameter is set one time when creating an index. To change its value, re-create the index.

    You need to create a dedicated IVFFlat index for each distance calculation function. To learn more, see the vector extension guide.

  2. Configure the search settings:

    SET ivfflat.probes = 1;
    

    Where probes is the number of groups (lists) viewed when running a search. The default value is 1. The higher the value, the greater the probability of finding the most relevant records; however, the query runs slower.

    Unlike the lists index parameter, the probes search parameter can be modified before running queries.

  3. Run a search for the nearest neighbors:

    SELECT *
    FROM items
    ORDER BY embedding <-> '[3,1,2]'
    LIMIT 3;
    

    If the search results are not accurate enough, increase the probes value and repeat the query.

Delete the resources you createdDelete the resources you created

If you no longer intend to use the Managed Service for PostgreSQL cluster, delete it to avoid paying for it.

Was the article helpful?

Previous
Executing analytical queries in Managed Service for PostgreSQL with processing in Managed Service for ClickHouse®
Next
Migrating a database from a third-party Valkey™ cluster to Yandex Managed Service for Valkey™
© 2026 Direct Cursus Technology L.L.C.