How to use Oracle Database 23ai for fast AI vector search
With the release of Oracle Database 23ai and the introduction of vector support, Oracle Database now enables fast AI Vector Search. This new capability of Oracle Database allows for efficient and accurate similarity searches within the database, leveraging the power of AI to enhance data retrieval and analysis.
In this post, we’ll discuss what similarity search is, how it can be used, and walk through a Java code sample that implements similarity search on top of Oracle Database 23ai.
What is similarity search?
Similarity search is data retrieval technique for finding closely related items within a dataset. This method employs a vector distance function to calculate the similarity between an input vector and the elements of the dataset. By measuring the distance in vector space, we can identify the most relevant items, facilitating advanced data retrieval and analysis in AI applications.
Similarity search makes use of the following concepts:
- Data is converted into numerical vectors (embeddings) that capture their unique characteristics using an embedding model (LLM).
- A similarity score is calculated using a distance metric, which is used to quantify how close (semantically similar) two vectors are. Common distance metrics include cosine similarity, Euclidean distance, and others.
- When a similarity search query is processed by the database, the query’s search vector should be embedded using the same method as the indexed embedding vectors. This ensures the dimensions and semantics of the search vector will be comparable to the indexed embedding vectors.
- The database uses an approximate nearest neighbor algorithm to find the closest vectors in the index according to the chosen distance metric (e.g., cosine similarity).
What can similarity search be used for?
Similarity search can be used any time you need to search based on contextual meaning, rather than keyword search or other methods.
The use of similarity search has surged in popularity, particularly with AI applications using Retrieval Augmented Generation (RAG). RAG enriches LLM prompts by using similarity search to dynamically retrieve documents from a database, adding document context to the prompt. RAG + similarity search amplifies the capabilities of AI systems, enabling more up-to-date, accurate, and context-aware responses.
Java Sample using Oracle Database 23ai
Now we get into the fun part… the code to implement similarity search in your Java application!
The provided Java code sample loads an Oracle Database Free container with embeddings, and runs similarity searches against the containerized database. You can find the full sample code implementing similarity search here. The code sample uses of Java 21, Maven, and Docker for a test environment.
Prerequisites
- Java 21+
- Maven
- A docker host to run the sample
Code Overview
The OracleVectorSample class implements an abstraction layer over an Oracle Database JDBC connection to add and query vector embeddings. This provides a vector store implementation, including similarity search.
The OracleVectorSampleIT class implements an integration test that inserts embeddings and executes similarity searches is provided. The test can be run with “mvn integration-test”.
The OracleDataAdapter helper class provides datatype conversions for vectors, and vector normalization for cosine similarity.
The SearchRequest and Embedding data classes are used for search queries and results.
Creating a vector table and associated index
To initialize the vector store, we need to create a table and an associated vector index. The OracleVectorStore.createTableIfNotExists method does this the following way:
- Create a table with three columns: id, content, and embedding, the latter of which is of the VECTOR type. You must know the dimension of your embedding vectors at the time of table creation, as the dimension is used when defining the embedding vector column.
- Create an Inverted File (IVF) index on the table’s embedding column with cosine similarity. The IVF index allows for quick retrieval of vectors similar to a query vector, using an approximate nearest neighbor algorithm. The use of cosine similarity is particularly suitable for tasks like fast semantic search of text embeddings. You may choose an accuracy value here that suits your needs for accuracy vs. query speed (95% accuracy is given in this sample).
Create vector table and vector index
Insert embeddings into the database
Before we run similarity searches, we need a method of inserting embedding vectors into the vector table. The OracleVectorSample.addAll method takes a list of Embeddings, and performs a batch upsert into the vector table.
The upsert makes use of the Oracle VECTOR datatype in a prepared statement to upsert embedding vectors.
Batch insert of vector embeddings into the vector table
Similarity search
Similarity search is implemented by the OracleVectorSample.search method, using the vector_distance function to calculate a similarity score. Records are excluded from the result if their similarity score does is less than the minimum threshold specified in the SearchRequest.
Similarity search implementation
Integration Test
Using the OracleVectorSample implementation, we can now construct an integration test, presented here in its entirety. This test uses Testcontainers to spin up an Oracle Database using the 23ai Free container for testing.
The test then performs the following operations:
- Initialize a vector store, creating a vector table and vector index.
- Embed a series of text inputs, storing them in the vector table. For test purposes, we use the Langchain4j AllMiniLmL6V2QuantizedEmbeddingModel as a lightweight, in-memory embedding model.
- Run a similarity search against the vector table and verify the result.
You can run the test from the repository’s root directory using “mvn integration-test”
Vector store integration test
Questions? Let me know in the comments, or reach out directly!
if you’re interested in using Oracle technologies to build your AI applications, reach out directly out for code samples, patterns, and idea-sharing.