Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Filters, hybrid search, upserts and deletes

After this lesson: I can write filtered and hybrid searches that return the right number of results.

60 min lesson3 commandsVector database lab: Qdrant and PostgreSQL with pgvector

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it means
Filtera condition on the payload a result must meet
Payload indexan index on a payload field for fast filters
Iterative scanan index scan that keeps going until enough rows pass
Sparse vectornumbers only for the words a text holds
Hybrid searchdense and sparse results fused into one list
Upsertinsert, or replace when the id exists

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A 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

Code: python
# 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

Command
docker exec tools python3 steps/filtered.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

What filtered.py printed in the lab

SearchRows backRight, of 10
pgvector HNSW, iterative scan off00
pgvector HNSW, iterative scan strict_order10about 9
Qdrant HNSW with a payload filter1010

The cells students fill in are left blank in this preview.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A 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

Command
docker exec tools python3 steps/hybrid.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: "error 504 on the vpn"

SearchFirstSecondThird
Dense (meaning)note 4, wifi slownote 1, vpn dropsnote 8, proxy error 502
Sparse (words)note 2, gateway error 504note 1, vpn dropsnote 8, proxy error 502
Hybrid (fused)note 1, vpn dropsnote 2, gateway error 504note 8, proxy error 502

The cells students fill in are left blank in this preview.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Upserts 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

Command
docker exec tools python3 steps/upsert.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Assignment: 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.

Code: python
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

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.