Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Neste documento, explicamos como realizar pesquisas de similaridade de vetores no AlloyDB para PostgreSQL
usando a extensão pgvector. A pesquisa de similaridade de vetores, também conhecida como pesquisa de vizinho
mais próximo, permite encontrar os pontos de dados mais
semelhantes a determinado vetor de consulta.
É possível consultar seu banco de dados do AlloyDB em busca de vetores semanticamente parecidos depois
de armazenar e indexar embeddings. Use recursos de consulta pgvector para encontrar os
vizinhos mais próximos de um vetor de embedding.
Fazer uma pesquisa de similaridade com uma entrada de vetor
Para fazer uma pesquisa de similaridade, especifique a tabela, a coluna de embedding,
a função de distância, o embedding de destino e o número de linhas a serem retornadas. Você também
pode usar a função embedding() para traduzir um texto em um vetor e
comparar o vetor com embeddings armazenados usando operadores pgvector.
Para encontrar os vizinhos semânticos mais próximos de um vetor de embedding, execute a
consulta de exemplo a seguir, em que você define a mesma função de distância usada
durante a criação do índice.
TABLE: a tabela que contém o embedding com o qual você vai comparar o
texto.
EMBEDDING_COLUMN: a coluna que contém os embeddings
armazenados.
DISTANCE_FUNCTION_QUERY: a função de distância a ser usada com essa
consulta. Escolha uma das seguintes opções com base na função de distância usada
ao criar o índice:
Distância de L2:<->
Produto interno:<#>
Distância do cosseno:<=>
EMBEDDING: o vetor de embedding para encontrar os vizinhos semânticos
armazenados mais próximos.
ROW_COUNT: o número de linhas que serão retornadas.
Especifique 1 se você quiser apenas a melhor correspondência.
Para mais exemplos de consultas, consulte
Consultas.
Fazer uma pesquisa de similaridade usando pgvector com uma entrada de texto
Você também pode usar a função embedding() para
traduzir o texto em um vetor e encontrar as linhas do banco de dados com os embeddings mais semanticamente
parecidas. A extensão pgvector do PostgreSQL é
personalizada para o AlloyDB e é chamada de vector. Você aplica
o vetor a um dos operadores de vizinho mais próximo do pgvector, por exemplo
, <=> para a distância do cosseno.
Como embedding() retorna uma matriz real, é necessário transmitir explicitamente a
chamada embedding() para vector para usar esses valores com
operadores pgvector.
Se você estiver usando o Model Garden
da Vertex AI, especifique
text-embedding-005 como o ID do modelo. Esses são os modelos baseados na nuvem que
o AlloyDB pode usar para embeddings de texto. Para mais informações, consulte
Embeddings de texto.
Opcional: VERSION_TAG: a tag da versão do modelo a
ser consultada. Adicione @ ao início da tag.
Se você estiver usando um dos modelos em inglês text-embedding-005 com
a Vertex AI, especifique uma das tags de versão, por exemplo, text-embedding-005,
listadas em Versões de modelo.
O Google recomenda que você especifique a tag de
versão. Se você não especificar a tag de versão, o AlloyDB
usará a versão mais recente do modelo, o que pode gerar resultados inesperados.
TEXT: o texto a ser traduzido em um embedding de vetor.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 2025-08-30 UTC."],[[["\u003cp\u003eThis guide demonstrates how to use the \u003ccode\u003epgvector\u003c/code\u003e extension in AlloyDB for PostgreSQL to perform vector similarity searches, also known as nearest neighbor searches.\u003c/p\u003e\n"],["\u003cp\u003eYou can query your AlloyDB database for semantically similar vectors using \u003ccode\u003epgvector\u003c/code\u003e operators after storing and indexing embeddings, with support for L2 distance, inner product, and cosine distance functions.\u003c/p\u003e\n"],["\u003cp\u003eTo conduct a similarity search, you must specify the table, embedding column, distance function, target embedding, and the number of rows to return in your query.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eembedding()\u003c/code\u003e function allows you to convert text into a vector, which can then be used with \u003ccode\u003epgvector\u003c/code\u003e operators to find semantically similar embeddings in the database, but the function call must be cast to \u003ccode\u003evector\u003c/code\u003e to work.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003egoogle_ml_integration\u003c/code\u003e extension must be installed and the \u003ccode\u003etext-embedding-005\u003c/code\u003e model ID can be specified for use with the \u003ccode\u003eembedding()\u003c/code\u003e function when using Vertex AI Model Garden, and specifying a model version tag is recommended.\u003c/p\u003e\n"]]],[],null,["# Run a vector similarity search\n\nThis document explains how to perform vector similarity searches in AlloyDB for PostgreSQL\nusing the `pgvector` extension. Vector similarity search, also known as nearest\nneighbor search, lets you find the data points in your data that are\nmost similar to a given query vector.\n\nYou can query your AlloyDB database for semantically similar vectors after\nstoring and indexing embeddings. Use `pgvector` query features to find the\nnearest neighbors for an embedding vector.\n\nFor more information about storing vector embeddings and creating an index, see\n[Store vector embeddings](/alloydb/docs/ai/store-embeddings) and [Create\nindexes](/alloydb/docs/ai/store-index-query-vectors) respectively.\n\nRun a similarity search with vector input\n-----------------------------------------\n\nTo run a similarity search, specify the table, embedding column,\ndistance function, target embedding, and the number of rows to return. You can\nalso use the `embedding()` function to translate text into a vector and then\ncompare the vector to stored embeddings using `pgvector` operators.\n| **Note:** You cannot run bulk search queries using the `alloydb_scann` extension.\n\nTo find the nearest semantic neighbors for an embedding vector, you can run the\nfollowing example query, where you set the same distance function that you used\nduring the index creation. \n\n SELECT * FROM \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-k\"\u003eTABLE\u003c/span\u003e\u003c/var\u003e\n ORDER BY \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eEMBEDDING_COLUMN\u003c/span\u003e\u003c/var\u003e \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eDISTANCE_FUNCTION_QUERY\u003c/span\u003e\u003c/var\u003e ['\u003cvar translate=\"no\"\u003eEMBEDDING\u003c/var\u003e']\n LIMIT \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-k\"\u003eROW_COUNT\u003c/span\u003e\u003c/var\u003e\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eTABLE\u003c/var\u003e: the table containing the embedding to compare the\n text to.\n\n- \u003cvar translate=\"no\"\u003eEMBEDDING_COLUMN\u003c/var\u003e: the column containing the stored\n embeddings.\n\n- \u003cvar translate=\"no\"\u003eDISTANCE_FUNCTION_QUERY\u003c/var\u003e: the distance function to use with this\n query. Choose one of the following based on the distance function used\n while creating the index:\n\n - **L2 distance:** `\u003c-\u003e`\n\n - **Inner product:** `\u003c#\u003e`\n\n - **Cosine distance:** `\u003c=\u003e`\n\n- \u003cvar translate=\"no\"\u003eEMBEDDING\u003c/var\u003e: the embedding vector you want to find the nearest stored\n semantic neighbors of.\n\n- \u003cvar translate=\"no\"\u003eROW_COUNT\u003c/var\u003e: the number of rows to return.\n\n Specify `1` if you want only the single best match.\n\nFor more information about other query examples, see\n[Querying](https://github.com/pgvector/pgvector?tab=readme-ov-file#querying).\n\nRun a similarity search using `pgvector` with text input\n--------------------------------------------------------\n\nYou can use also use the [`embedding()`](/alloydb/docs/ai/work-with-embeddings) function to\ntranslate the text into a vector, and to find the database rows with the most semantically\nsimilar embeddings. The stock `pgvector` PostgreSQL extension is\ncustomized for AlloyDB, and referred to as `vector`. You apply\nthe vector to one of the `pgvector` nearest-neighbor operators, for example\n`\u003c=\u003e` for cosine distance.\n\nBecause `embedding()` returns a `real` array, you must explicitly cast the\n`embedding()` call to `vector` in order to use these values with `pgvector`\noperators. \n\n CREATE EXTENSION IF NOT EXISTS vector;\n\n SELECT * FROM \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-k\"\u003eTABLE\u003c/span\u003e\u003c/var\u003e\n ORDER BY \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eEMBEDDING_COLUMN\u003c/span\u003e\u003c/var\u003e::vector\n \u003c=\u003e google_ml.embedding('\u003cvar translate=\"no\"\u003eMODEL_ID\u003c/var\u003e\u003cvar translate=\"no\"\u003eVERSION_TAG\u003c/var\u003e', '\u003cvar translate=\"no\"\u003eTEXT\u003c/var\u003e')\n LIMIT \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-k\"\u003eROW_COUNT\u003c/span\u003e\u003c/var\u003e\n\nReplace the following:\n\n\n- \u003cvar translate=\"no\"\u003eMODEL_ID\u003c/var\u003e: the ID of the model to query.\n\n If you are using the Vertex AI\n Model Garden, then specify\n `text-embedding-005` as the model ID. These are the cloud-based models that\n AlloyDB can use for text embeddings. For more information, see\n [Text embeddings](/vertex-ai/docs/generative-ai/model-reference/text-embeddings).\n- Optional: \u003cvar translate=\"no\"\u003eVERSION_TAG\u003c/var\u003e: the version tag of the model to\n query. Prepend the tag with `@`.\n\n If you are using one of the `text-embedding-005` English models with\n Vertex AI, then specify one of the version tags---for example, `text-embedding-005`,\n listed in [Model versions](/vertex-ai/docs/generative-ai/model-reference/text-embeddings#model_versions).\n\n Google strongly recommends that you always specify the version\n tag. If you don't specify the version tag, then AlloyDB\n uses the latest model version, which might lead to unexpected results.\n\n\u003cbr /\u003e\n\n- \u003cvar translate=\"no\"\u003eTEXT\u003c/var\u003e: the text to translate into a vector embedding.\n\nTo accelerate your filtered KNN search, you can use the AlloyDB\ncolumnar engine. For more information, see [Accelerate your filtered vector search](/alloydb/docs/ai/perform-vector-search#accelerate-filtered-vector-search) ([Preview](https://cloud.google.com/products#product-launch-stages))\nand [Configure the columnar engine](/alloydb/docs/columnar-engine/configure).\n\nWhat's next\n-----------\n\n- [Perform vector search tutorial](/alloydb/docs/ai/perform-vector-search)\n- [Tune vector query performance](/alloydb/docs/ai/store-index-query-vectors)\n- [Vector index metrics](/alloydb/docs/reference/vector-index-metrics)\n- [Learn how to build a smart shopping assistant with AlloyDB, pgvector, and model endpoint management](https://codelabs.developers.google.com/smart-shop-agent-alloydb#0)."]]