Vector Search

Describes the manual/automated embedding query, ANN/ENN, pre/post filter, Driver 5.10 nested vector options, and Bson extension paths for Vector Search.


Basic Vector Search.

java
Flux<Article> results = dsl
    .executeEntity(Article.class, MongoTemplateName.FRONT)
    .vectorSearch("articles_vector_index")
    .path("embedding")
    .queryVector(embedding)
    .limit(20)
    .approximate(200)
    .findAll()
    .execute();

Vector Search requires an explicit index name, and $vectorSearch becomes the first stage of the aggregation.

Query vector / Automated Embedding.

queryVector(float[]/double[]/Collection<Double>)

Passes the vector generated by the application.

queryVector(BinaryVector).

Directly uses the compact binary vector representation of the MongoDB Driver.

query(String).

Passes the query text itself to the MongoDB Automated Embedding index.

query(VectorSearchQuery).

A future-compatible escape hatch for directly passing the driver-native query type.

java
.vectorSearch("articles_auto_embedding")
.path("content")
.query("reactive mongodb search")
.model("model-name")
.limit(20)
.approximate(200)
.findAll()
.execute();

The DSL core does not include the ability to call external embedding providers to generate vectors. Manual vectors are generated by the calling application and passed to queryVector(...).

ANN / ENN.

ANN.

Use approximate(numCandidates) or numCandidates(...). The candidate count is required for ANN.

ENN.

Use exact() or exact(true). Activating exact removes the existing numCandidates value.

Pre-filter / post-filter.

java
.vectorSearch("articles_vector_index")
.path("embedding")
.queryVector(embedding)
.limit(20)
.approximate(200)
.filter(filter -> filter
    .and(f -> f.fields(
        pair("status", "PUBLISHED"),
        pair("deleted", false)
    ))
)
.fields(pair("category", "TECH"))
.findAll()
.execute();

filter()/filterFields().

Pre-filter that affects candidate selection itself, which goes into $vectorSearch.filter.

fields(...).

General $match post-filter after $vectorSearch.

Nested / array embedding (Driver 5.10).

java
Flux<Article> result = dsl
    .executeEntity(Article.class, MongoTemplateName.FRONT)
    .vectorSearch("articles_vector_index")
    .path("chunks.embedding")
    .queryVector(embedding)
    .limit(20)
    .exact()
    .filter(f -> f.fields(
        pair("chunks.kind", "BODY")
    ))
    .parentFilter(f -> f.fields(
        pair("tenantId", tenantId)
    ))
    .nestedScoreMode(VectorSearchScoreMode.AVG)
    .findAll()
    .execute();

filter / filterFields.

Filter for the leaf document of nested embedding.

parentFilter / parentFilterFields.

This is the parentFilter applied to the root document in Driver 5.10.

nestedScoreMode

Combines multiple matching embedding scores of a document using AVG or MAX.

Score / projection / terminal

java
.vectorSearch("articles_vector_index")
.path("embedding")
.queryVector(embedding)
.limit(20)
.approximate(200)
.addFieldsVectorSearchScore("similarity")
.excludes("embedding")
.findAll()
.execute();

findAll / find

Returns the vector result as Flux/Mono.

count().execute()

Counts the number of results in the pipeline after the vector limit. It is not the total count of the corpus.

existsQuery().execute()

Checks if there are vector pipeline results.

Driver-native post stage / options

java
.vectorSearch("articles_vector_index")
.path("embedding")
.queryVector(vector)
.limit(20)
.exact()
.stage(Aggregates.score("$rating"))
.driverOptions(options -> options
    .parentFilter(Filters.eq("tenantId", tenantId))
    .nestedOptions(
        VectorSearchNestedOptions.vectorSearchNestedOptions()
            .scoreMode(VectorSearchScoreMode.AVG)
    )
)
.findAll()
.execute();

stage(Bson) is executed immediately after $vectorSearch, before post-filter/metadata/projection. driverOptions(...) is an escape hatch to use current/future Driver options that are not yet available in the convenience API.

© 2026 Byeolnaerim. All rights reserved.IntroductionPrivacy Policy