42 Using Vectors in Your Application

This chapter explains how to add vector capabilities to your application, including creating and storing embeddings and running similarity search (for example, find_nearest) to retrieve the top‑K nearest results.

42.1 Similarity Search

Similarity search is what allows you to use the vectors to find similar items. You give the database a query vector, and it gives you back the documents whose stored vectors are most similar.

The SDK exposes this through findNearest. You pass it the column to search, the query vector, and a few options:

  • metric — how to measure similarity. Has to match the metric the index was built with. Supported values: COSINE, EUCLIDEAN, DOT

  • topK — how many results to return.

  • threshold — optional minimum similarity score. Results below this score are excluded.

Note:

An index is mandatory for this API to run.

import { collection, query, findNearest, getDocs } from "fusabase/oracledb";

const reviewCol = collection(db, "users", "Scott", "reviews");

// Get the embedding for the text you want to search for.
const query_vector = getQueryVectorFrom3rdParty();

const q = query(
  reviewCol,
  findNearest("review_emb", { vector: query_vector }, { metric: "COSINE", topK: 10, threshold: 0.4 })
);
const snap = await getDocs(q);
// Get the embedding for the text you want to search for.
const query_vector = getQueryVectorFrom3rdParty();

await db
  .collection("users", "Scott", "reviews")
  .findNearest("review_emb", { vector: query_vector }, { metric: "COSINE", topK: 10, threshold: 0.4 })
  .get();
CollectionReference collection = db.collection("users", "Scott", "reviews");
FindNearestQuery query = FindNearestQuery.dense(
    Arrays.asList(0.12, -0.44, 0.91, 0.03)
);
FindNearestOptions options = FindNearestOptions.builder()
    .metric(VectorMetric.COSINE)
    .topK(10)
    .threshold(0.25)
    .build();

collection
    .findNearest("review_emb", query, options)
    .get()
    .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
        @Override
        public void onSuccess(QuerySnapshot snapshot) {
            System.out.println("Matches: " + snapshot.getDocuments().size());
        }
    })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
            e.printStackTrace();
        }
    });
import FusabaseOracledb

let db = FusabaseOracledb.oracledb()

let reviewRef = db
    .collection("users")
    .document("Scott")
    .collection("reviews")
    .document("cookies-id-1")

// Add a review for the recipe Cookies.
reviewRef.setData([
    "recipe_id": "Cookies",
    "comment": "The Cookies recipe was easy to follow, and was super delicious. Easy to bake and produces a great munching sound.",
    "review_emb": try denseVector([0.12, -0.91, 0.44]),
    "recipe_emb": try sparseVector(
                       dimension: 1000,
                       indices: [2, 7, 900],
                       values: [0.9, 0.3, 0.5]
                      )
])
import 'package:fusabase/oracledb.dart';

final db = FusabaseOracledb.instance();
final query = db.collection('users', 'Scott', 'reviews').findNearest(
  'review_emb',
  {
    'vector': [0.10, -0.40, 0.88],
  },
  metric: 'COSINE',
  topK: 5,
  threshold: 0.75,
);
final snapshot = await query.get();