Tuesday, 9 September 2025

Understanding EmbeddingSearchRequest in LangChain4j: Filtering Smartly with Vector Search

In LangChain4j, the EmbeddingSearchRequest class represents a search request made to an EmbeddingStore. It is the bridge between your query embedding and the results you want from your vector store.

It allows you to define:

 

·      The query embedding you're searching against.

·      How many results you want back.

·      How similar the results must be (via a score threshold).

·      Any filters to restrict results based on metadata.

 

We can get an instance of EmbeddingSearchRequest using EmbeddingSearchRequest Builder like below.

EmbeddingSearchRequest.builder()
    .queryEmbedding(...)   // Mandatory
    .maxResults(...)       // Optional, default = 3
    .minScore(...)         // Optional, default = 0
    .filter(...)           // Optional, no filtering by default
    .build();

Here

·      queryEmbedding: The query embedding you're using to search. The store will return similar embeddings based on this.

·      maxResults (int): It is Optional. Controls how many matching embeddings to return. Default value is set to 3.

·      minScore (double): Ranges from 0.0 to 1.0. Only results with cosine similarity ≥ minScore will be returned. Default is set to 0.0, use this to filter out weak matches.

·      filter (Filter): This is Optional, and lets you filter based on metadata like tags, source, etc.

 

Find the below working application.

 

EmbeddingStoreDemo.java

 

package com.sample.app.embeddings;

import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.embedding.onnx.bgesmallenv15q.BgeSmallEnV15QuantizedEmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingSearchRequest;
import dev.langchain4j.store.embedding.EmbeddingSearchResult;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;

public class EmbeddingStoreDemo {

    public static void main(String[] args) {

        // Step 1: Initialize embedding model
        EmbeddingModel model = new BgeSmallEnV15QuantizedEmbeddingModel();

        // Step 2: Prepare text data
        String sentence1 = "The stock market surged as tech companies reported strong earnings.";
        String sentence2 = "Tech giants like Apple and Amazon saw record profits this quarter.";
        String sentence3 = "Heavy rain caused flooding in coastal towns yesterday.";

        try {
            // Step 3: Generate embeddings
            Embedding embedding1 = model.embed(sentence1).content();
            Embedding embedding2 = model.embed(sentence2).content();
            Embedding embedding3 = model.embed(sentence3).content();

            // Step 4: Initialize in-memory embedding store
            EmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();

            // Step 5: Add embeddings to the store
            embeddingStore.add(embedding1, TextSegment.from(sentence1));
            embeddingStore.add(embedding2, TextSegment.from(sentence2));
            embeddingStore.add(embedding3, TextSegment.from(sentence3));

            System.out.println("Embeddings added to the store successfully.");

            // Step 6: Perform a search
            Embedding queryEmbedding = model.embed("Which companies had strong earnings?").content();
            EmbeddingSearchRequest request = EmbeddingSearchRequest.builder().queryEmbedding(queryEmbedding)
                    .maxResults(2).minScore(0.5).build();

            EmbeddingSearchResult<TextSegment> embeddingSearchResult = embeddingStore.search(request);

            // Step 7: Display results
            System.out.println("\nTop relevant results:");
            embeddingSearchResult.matches().forEach(match -> System.out.printf("Score: %.3f | Text: %s%n",
                    match.score(), match.embedded() != null ? match.embedded().text() : "[No TextSegment stored]"));

        } catch (Exception e) {
            System.err.println("An error occurred while embedding or storing data: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Output

Embeddings added to the store successfully.

Top relevant results:
Score: 0.910 | Text: The stock market surged as tech companies reported strong earnings.
Score: 0.846 | Text: Tech giants like Apple and Amazon saw record profits this quarter.

 

In summary, with EmbeddingSearchRequest, you can:

 

·      Control quality using score filtering.

·      Limit performance impact using maxResults.

·      Narrow down scope using metadata filters.

·      It gives you precision and flexibility in building intelligent retrieval-based applications.

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment