[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-25。"],[[["\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)."]]