Spring / Spring AI interview questions
How does Spring AI's Document metadata filtering work with PgVector and what filter operators are available?
Spring AI's metadata filter API provides a provider-neutral expression builder that gets translated into native filter syntax for each VectorStore. For PgVector, Spring AI translates filter expressions into SQL WHERE clauses applied alongside the vector similarity search, so you can combine semantic search with structured attribute filters in a single database query.
The Filter.ExpressionBuilder supports the following operators:
| Operator | Method | Example |
|---|---|---|
| Equals | eq() | eq("status", "published") |
| Not Equals | ne() | ne("category", "draft") |
| Greater Than | gt() | gt("year", 2022) |
| Less Than | lt() | lt("page", 10) |
| In | in() | in("lang", List.of("en", "de")) |
| Not In | nin() | nin("type", List.of("image")) |
| And | and() | Composite of two expressions |
| Or | or() | Composite of two expressions |
Filter.Expression filter = new Filter.ExpressionBuilder() .and( new Filter.ExpressionBuilder().eq("source", "spring-ai-docs.pdf").build(), new Filter.ExpressionBuilder().gt("page", 5).build() ); List<Document> results = vectorStore.similaritySearch( SearchRequest.query(question) .withTopK(5) .withFilterExpression(filter));
Metadata must be stored in the Document at ingestion time for filters to work. Fields referenced in filter expressions that were not stored as metadata simply match nothing (no error is thrown). All metadata values are stored in PgVector's metadata JSONB column, and Spring AI generates the appropriate metadata->>'key' SQL syntax automatically.
More Related questions...