Find data with vector search
At its core, a vector database is about efficient similarity search, which is also known as vector search. Vector search finds content that is similar to a given query.
Here’s how vector search works:
-
Generate vector embeddings for a collection of content, and then load the data, along with the embeddings, into a vector database.
-
Generate an embedding for a new piece of content outside the original collection. For example, you could generate an embedding from a text query submitted by a user to a chatbot.
Make sure you use the same embedding model for your original embeddings and your new embedding.
-
Use the new embedding to run a vector search on the collection and find data that is most similar to the new content.
Mechanically, a vector search determines the similarity between a query vector and the vectors of the documents in a collection. Each document’s resulting similarity score represents the closeness of the query vector and the document’s vector.
-
Use the returned content to produce a response or trigger an action in an LLM or GenAI application. For example, a support chatbot could use the result of a vector search to generate an answer to a user’s question.
Limitations of vector search
While vectors can replace or augment some functions of metadata filters, vectors are not a replacement for other data types. Vector search can be powerful, particularly when combined with keyword filters, but it is important to be aware of its limitations:
-
Vectors aren’t human-readable. They must be interpreted by an LLM, and then transformed into a human-readable response.
-
Vector search isn’t meant to directly retrieve specific data. By design, vector search finds similar data.
For example, assume you have a database for your customer accounts. If you want to retrieve data for a single, specific customer, it is more appropriate to use a filter to exactly match the customer’s ID instead of a vector search.
-
Vector search is a mathematical approximation. By design, vector search uses mathematical calculations to find data that is mathematically similar to your query vector, but this data may not be the most contextually relevant from a human perspective.
For example, assume you have database for a department store inventory. A vector search for
green hiking boots
could return a mix of hiking boots, other types of boots, and other hiking gear.Use metadata filters to narrow the context window and potentially improve the relevance of vector search results. For example, you can improve the
hiking boots
vector search by including a metadata filter likeproductType: "shoes"
. -
The embedding model matters. It’s important to choose an embedding model that is ideal for your data, your queries, and your performance requirements. Embedding models exist for different data types (such as text, images, or audio), languages, use cases, and more.
Using an inappropriate embedding model can lead to inaccurate or unexpected results from vector searches. For example, if your dataset is in Spanish, and you choose an English language embedding model, then your vector search results could be inaccurate because the embedding model attempts to parse the Spanish text in the context of the English words that it was trained on.
Additionally, you must use the same model for your stored vectors and query vectors because mismatched embeddings can cause a vector search to fail or produce inaccurate results.
Approximate nearest neighbor
Cassandra-based databases support only Approximate Nearest Neighbor (ANN) vector searches, not exact K-Nearest Neighbor (KNN) searches.
ANN search finds the most similar content within reason for efficiency, but it might not find the exact most similar match. Although precise, KNN is resource intensive, and it is not practical for applications that need quick responses from large datasets. ANN balances accuracy and performance, making it a better choice for applications that query large datasets or need to respond quickly.
To learn more about ANN and KNN, see What is the K-Nearest Neighbors (KNN) Algorithm.
Run a vector search with the Data API
If you’re new to the Data API, try the quickstart for collections or the quickstart for tables for a demo of some common operations. |
-
Collections
-
Tables
To support vector search, collections must be vector-enabled and contain vector data. Then, you can use the sort clause to run a vector search on your collection.
For more information and examples, see the following Data API documentation:
To support vector search, your table must have a vector column with a vector index. You can create a table with a vector column or add a vector column to an existing table.
Then, you can use the sort clause to run a vector search on your table based on the indexed vector column. For more information and examples, see the following Data API documentation:
If your table has multiple vector columns, you can only sort on one vector column at a time.
You can use filter and sort clauses together in the same Data API command.
For example, combining the keyword hiking
with a vector search based on the phrase green boots
might produce results that are more relevant than either search alone.
However, not all Data API commands allow both clauses, and some usage patterns can disallow one clause or the other.
For more information and examples, see the Data API reference documentation for the command you want to run, such as Find documents and Find rows.