Spring / Spring AI interview questions
What is PgVector and how do you configure it as a VectorStore in Spring AI?
PgVector is an open-source PostgreSQL extension that adds a vector column type and approximate nearest-neighbour index operators to Postgres. Spring AI's PgVectorStore uses it to store document embeddings and run similarity searches directly inside your existing Postgres database — no separate vector database service required.
Setup requires three things: the pgvector extension enabled in Postgres, the Spring AI PgVector starter, and connection properties.
<!-- Dependency --> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-pgvector-store-spring-boot-starter</artifactId> </dependency>
# application.properties spring.ai.vectorstore.pgvector.index-type=HNSW spring.ai.vectorstore.pgvector.distance-type=COSINE_DISTANCE spring.ai.vectorstore.pgvector.dimensions=1536 # must match your embedding model spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
On startup, Spring AI auto-creates the vector_store table with the correct schema if it does not exist (configurable). The index type is important for performance: HNSW (Hierarchical Navigable Small World) gives fast approximate search with slightly slower inserts; IVFFlat is cheaper to build but slower to search. For most production use cases HNSW is the better default.
The dimensions property must exactly match the output dimensions of your embedding model. OpenAI text-embedding-3-small is 1536, text-embedding-3-large is 3072, Ollama nomic-embed-text is 768. A mismatch causes a startup exception or runtime SQL error.
More Related questions...