0PricingLogin
Spring Boot 4 Complete Guide · Lesson

Embeddings and Vector Store Retrieval

Generate embeddings and query vector stores to power semantic search over your data.

Why Embeddings Power Semantic Search

Keyword search matches literal tokens. Semantic search matches meaning. The bridge between the two is an embedding: a fixed-length vector of floats that captures the semantic content of a piece of text.

  • Texts with similar meaning produce vectors that are close together in vector space.
  • Closeness is measured by cosine similarity or Euclidean distance.
  • A query like "how do I reset my password" can match a document titled "account recovery steps" even with zero shared keywords.

In Spring AI, you generate embeddings with an EmbeddingModel and store/query them with a VectorStore. This lesson wires both together into a Retrieval-Augmented pipeline.

The EmbeddingModel Abstraction

Spring AI exposes EmbeddingModel as a provider-agnostic interface. Add the starter (for example spring-ai-starter-model-openai) and Spring Boot auto-configures a bean you can inject.

  • embed(String) returns a single float[].
  • embed(List<String>) batches multiple texts efficiently.
  • dimensions() tells you the vector size (for example 1536 for text-embedding-3-small).

Configure the model in application.yml so the bean is ready for injection.

spring:
  ai:
    openai:
      api-key: ${OPENAI_API_KEY}
      embedding:
        options:
          model: text-embedding-3-small

All lessons in this course

  1. ChatClient, Prompts, and Structured Output
  2. Embeddings and Vector Store Retrieval
  3. Retrieval-Augmented Generation Pipelines
  4. Tool Calling and Agent Advisors
← Back to Spring Boot 4 Complete Guide