Function-Based Vector Indexes

Create a function-based HNSW vector index to search for JSON documents with embeddings similar to a query vector.

Sample JSON Collection

The following examples insert documents with vector embeddings into the bookings collection table described in Sample JSON Collection. The table stores each document in a JSON-type column named data. The embeddings are JSON arrays of numbers.

INSERT INTO bookings VALUES
('{
   "_id": "507f192e811c19709de86101",
   "traveler": {
       "first": "Michael",
       "last": "Chen",
       "status": "gold"
   },
   "reservations": [
       {
           "type": "flight",
           "flightNumber": "UA218",
           "departureCity": "San Francisco",
           "arrivalCity": "Tokyo",
           "seat": "18C"
       }
   ],
   "notes": "Traveler prefers aisle seats and quiet accommodations.",
   "notes_embedding": [0.07, 0.03, 0.06, -0.07, 0.04, -0.08, 0.09, 0.03]
}');

INSERT INTO bookings VALUES
('{
   "_id": "507f192e811c19709de86102",
   "traveler": {
       "first": "Priya",
       "last": "Sharma",
       "status": "platinum"
   },
   "reservations": [
       {
           "type": "hotel",
           "hotelName": "Paris Central Hotel",
           "location": "Paris",
           "roomType": "Deluxe Room"
       }
   ],
   "notes": "Traveler prefers upper-floor rooms and vegan meals.",
   "notes_embedding": [0.07, 0.03, 0.06, -0.07, 0.04, -0.08, 0.09, 0.03]
}');

Because data is a JSON-type column, you can insert the JSON text directly without using a JSON constructor.

Verify Vector Dimensions and Element Type

Use VECTOR_DIMS and VECTOR_DIMENSION_FORMAT to verify the dimensions and element type of a vector extracted from a JSON document.

SELECT VECTOR_DIMS(JSON_VALUE(data, '$.notes_embedding' RETURNING VECTOR)) AS dimensions,
       VECTOR_DIMENSION_FORMAT(JSON_VALUE(data, '$.notes_embedding' RETURNING VECTOR)) AS element_type
FROM bookings
WHERE JSON_EXISTS(data, '$.notes_embedding');

The JSON_EXISTS condition limits the results to documents with an embedding. These vectors have eight dimensions and use the FLOAT32 element type:

DIMENSIONS  ELEMENT_TYPE
----------  ------------
         8  FLOAT32
         8  FLOAT32

Create a Function-Based Vector Index

Create a function-based HNSW vector index on an expression that extracts a vector from a JSON document. Use JSON_VALUE when you want to specify the vector dimension and element type.

The following index definitions are alternatives. Create the index that matches the expression used in your queries.

CREATE VECTOR INDEX bookings_vec_idx
ON bookings (
  JSON_VALUE(data, '$.notes_embedding' RETURNING VECTOR(8, FLOAT32))
)
ORGANIZATION INMEMORY NEIGHBOR GRAPH
DISTANCE COSINE
WITH TARGET ACCURACY 90
ONLINE;

You can omit the dimension and element type when you do not know them ahead of time:

CREATE VECTOR INDEX bookings_vec_idx
ON bookings (
  JSON_VALUE(data, '$.notes_embedding' RETURNING VECTOR)
)
ORGANIZATION INMEMORY NEIGHBOR GRAPH
DISTANCE COSINE
WITH TARGET ACCURACY 90
ONLINE;

You can also use dot-path notation to extract the vector:

CREATE VECTOR INDEX bookings_vec_idx
ON bookings (data.notes_embedding.vector())
ORGANIZATION INMEMORY NEIGHBOR GRAPH
DISTANCE COSINE
WITH TARGET ACCURACY 90
ONLINE;

Dot-path notation does not let you specify the vector dimension or element type. Use JSON_VALUE when you need to specify those attributes.

You can use a conditional expression to index vectors only for selected rows. In this example, the index contains vectors for bookings where the traveler is Michael or Priya.

CREATE VECTOR INDEX bookings_vec_idx
ON bookings (
  CASE
    WHEN JSON_VALUE(data, '$.traveler.first' RETURNING VARCHAR2)
         IN ('Michael', 'Priya')
    THEN JSON_VALUE(data, '$.notes_embedding' RETURNING VECTOR(8, FLOAT32))
    ELSE NULL
  END
)
ORGANIZATION INMEMORY NEIGHBOR GRAPH
DISTANCE COSINE
WITH TARGET ACCURACY 90
ONLINE;

Query with a Function-Based Vector Index

Use VECTOR_DISTANCE to find JSON documents with embeddings similar to a query vector. The query vector represents the characteristics of the content being searched for.

The query must use the same vector expression as the index definition. This query uses the JSON_VALUE expression from the first index example:

SELECT JSON_VALUE(data, '$.traveler.first') AS traveler
FROM bookings
ORDER BY VECTOR_DISTANCE(
  JSON_VALUE(data, '$.notes_embedding' RETURNING VECTOR(8, FLOAT32)),
  TO_VECTOR('[0.12,-1.98,3.14,-2.71,0.57,-0.69,1.41,-0.30]')
)
FETCH APPROX FIRST 2 ROWS ONLY;

Use EXPLAIN PLAN to check whether Oracle AI Database uses the vector index.

EXPLAIN PLAN FOR
SELECT JSON_VALUE(data, '$.traveler.first') AS traveler
FROM bookings
ORDER BY VECTOR_DISTANCE(
  JSON_VALUE(data, '$.notes_embedding' RETURNING VECTOR(8, FLOAT32)),
  TO_VECTOR('[0.12,-1.98,3.14,-2.71,0.57,-0.69,1.41,-0.30]')
)
FETCH APPROX FIRST 2 ROWS ONLY;

SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'ALL'));

When Oracle AI Database uses the index, the execution plan includes a VECTOR INDEX HNSW SCAN operation and the index name. For example:

| Id  | Operation                       | Name             |
|-----|---------------------------------|------------------|
|   0 | SELECT STATEMENT                |                  |
|   1 |  COUNT STOPKEY                  |                  |
|   2 |   VIEW                          |                  |
|   3 |    SORT ORDER BY STOPKEY        |                  |
|   4 |     TABLE ACCESS BY INDEX ROWID | BOOKINGS         |
|   5 |      VECTOR INDEX HNSW SCAN     | BOOKINGS_VEC_IDX |

Notes