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.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
