Only Lakeside, only "504", only today's version
Filtered vector search finds the nearest vectors that also match a condition, such as one college. Hybrid search joins meaning with exact words. Upserts and deletes keep the store in step with the documents.
Real questions need all three. A store that gets them wrong returns too few results, misses an error code or shows a deleted page.
By the end of this lesson
Note
I can write filtered and hybrid searches that return the right number of results.
Key words
| Word | What it means |
|---|---|
| Filter | a condition on the payload a result must meet |
| Payload index | an index on a payload field for fast filters |
| Iterative scan | an index scan that keeps going until enough rows pass |
| Sparse vector | numbers only for the words a text holds |
| Hybrid search | dense and sparse results fused into one list |
| Upsert | insert, or replace when the id exists |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsA filter after the index can leave nothing
Module 77 showed that a filter must act before the top k is cut, not after. Real stores differ here.
pgvector's HNSW scan finds ef_search candidates, then the WHERE clause removes the rest. A college with 1 in 100 rows rarely survives. Since version 0.8, an iterative scan keeps searching until enough rows pass. Qdrant reads a payload index while it walks the graph, so it filters during the search.
Try it: filter after or while searching
# The index returned these ids, nearest first, with each one's college.
ranked = [7, 3, 9, 1, 4, 8, 2, 6, 5, 10]
college = {1: "lakeside", 2: "riverside", 3: "riverside", 4: "riverside", 5: "lakeside",
6: "riverside", 7: "riverside", 8: "riverside", 9: "riverside", 10: "lakeside"}
k = 4
after = []
for one in ranked[:k]:
if college[one] == "lakeside":
after.append(one)
print("filter after the top", k, ":", after)
before = []
for one in ranked:
if college[one] == "lakeside" and len(before) < k:
before.append(one)
print("filter while searching:", before)
# Change k to 2. How many rows does filtering after return now?
Make a free account to save your work and track your progress.
Ten nearest Lakeside documents, three ways
docker exec tools python3 steps/filtered.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
What filtered.py printed in the lab
| Search | Rows back | Right, of 10 |
|---|---|---|
| pgvector HNSW, iterative scan off | 0 | 0 |
| pgvector HNSW, iterative scan strict_order | 10 | about 9 |
| Qdrant HNSW with a payload filter | 10 | 10 |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsA common mistake
Warning
Many people think a SQL LIMIT 10 with a WHERE clause always returns 10 rows when 10 match. With an approximate index, the filter can run after the index has stopped. Turn on an iterative scan, or check the row count, whenever a filter is selective.
Hybrid search in one request
A dense vector says what a text is about. A sparse vector has a number only for each word the text holds, so it matches exact words such as an error code. Qdrant stores both on one point.
A hybrid search runs both searches and fuses the two lists, here with reciprocal rank fusion at k = 60. Module 77 wrote the fusion by hand; Qdrant does it in one request.
Dense, sparse and fused for one question
docker exec tools python3 steps/hybrid.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: "error 504 on the vpn"
| Search | First | Second | Third |
|---|---|---|---|
| Dense (meaning) | note 4, wifi slow | note 1, vpn drops | note 8, proxy error 502 |
| Sparse (words) | note 2, gateway error 504 | note 1, vpn drops | note 8, proxy error 502 |
| Hybrid (fused) | note 1, vpn drops | note 2, gateway error 504 | note 8, proxy error 502 |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsUpserts replace and deletes can filter
An upsert with a new id adds a point; with an id that exists, it replaces that point's vector and payload. So a changed document keeps its id, and the old version cannot linger.
A delete can name ids or use a filter, such as every point of a withdrawn document. Both stores mark deleted entries first and tidy the index later, which Qdrant's optimiser and PostgreSQL's VACUUM do.
Upsert twice, then delete by filter
docker exec tools python3 steps/upsert.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsAssignment: changes and filtered search
A store is a dictionary from an id to a point: {"vector": [...], "payload": {...}}. Write three functions.
apply(store, changes) makes each change in order and returns the store. A change is ["upsert", id, vector, payload] (add, or replace the whole point), ["delete", id] (nothing happens if the id is not there) or ["delete_where", field, value] (remove every point whose payload has that value). filtered_top(store, field, value, question, k) is the ids of the k points with that payload value and the largest dot product with the question, best first. post_filter(ranked, store, field, value, k) copies a weak index: it keeps only the first k ids of ranked, then drops those without the value, so it can return fewer than k.
def apply(store, changes):
return store
def filtered_top(store, field, value, question, k):
return []
def post_filter(ranked, store, field, value, k):
return ranked[:k]
Make a free account to save your work and track your progress.
Exit quiz
Have a go