Contents Menu Expand Light mode Dark mode Auto light/dark, in light mode Auto light/dark, in dark mode Skip to content
ZypherDB documentation
ZypherDB documentation

Tutorial

  • 1. Your first database
  • 2. People: nodes, labels and values
  • 3. Teams and rooms: relationships
  • 4. Asking precise questions: WHERE
  • 5. Badges and parameters
  • 6. The staff directory: shaping results
  • 7. Reports: counting and grouping
  • 8. Changing the lab: SET, REMOVE, DELETE, MERGE
  • 9. Onboarding a cohort: UNWIND and WITH
  • 10. Who is where: OPTIONAL MATCH and missing data
  • 11. Hierarchies: paths of any length
  • 12. May this badge open this door? EXISTS
  • 13. Documents: lists and maps as properties
  • 14. Time: windows, expiry and how long
  • 15. A month of swipes: loading data in bulk
  • 16. Errors, threads and processes
  • 17. Fast queries
  • 18. The door controller
  • 19. The monitor: rules that are checked, not enforced
  • 20. The safety manual: full-text search
  • 21. Access reviews: vector search

Examples

  • The smallest program
  • A day at Helix Biolabs

References

  • Cypher reference
  • C reference
  • Python reference
  • Replication

Technical

  • Architecture
  • Storage — the key encoding and the graph algebra
  • The language: lexer, parser, AST
  • Planning and execution
  • Performance: known risks
  • Testing — how ZypherDB tests itself
  • Contributing — how to extend the engine
Back to top
View this page

21. Access reviews: vector search¶

Every quarter, Biosafety reviews who goes where. The rules of step 14 say who may enter a room; the review asks something the rules cannot: is the way someone uses their access normal for their work? The usual answer is to compare people with their peers: a technician who goes where none of their teammates go is worth a look.

A person’s quarter, reduced to how often they entered each room, is a list of numbers: a vector. People who work alike have vectors that point the same way, and a vector index finds the closest vectors fast. The same index serves the embeddings that a machine-learning model makes of texts or images, compared by meaning; here the vectors come from the swipe log, so every number has a plain meaning.

Access profiles¶

The profile counts each person’s entries into each room last quarter, always in the same order of rooms:

ROOMS = ["Lobby", "Seminar Room", "Genomics Office", "Sequencing Lab",
         "Airlock", "BSL-3 Lab", "Animal Holding", "Vivarium Control"]
PROFILES = {
    "Ada Okafor":      [60, 10,  8, 10, 15, 15,  8,  8],
    "Bruno Lindqvist": [60, 12, 55, 30,  0,  0,  0,  0],
    "Chen Wei":        [60,  6, 20, 58,  0,  0,  0,  0],
    "Dalia Haddad":    [60,  8,  4,  2, 50, 50,  0,  0],
    "Emeka Nwosu":     [60,  2,  6, 62,  0,  0,  0,  0],
    "Fatima Rossi":    [60,  3,  0,  0,  0,  0, 55, 40],
    "Gustavo Pérez":   [60, 30, 20,  5,  0,  2,  0,  3],
    "Joon Park":       [60,  4,  2,  0, 58, 58,  0,  0],
    "Kemi Adeyemi":    [60,  2,  0,  0, 10, 10, 40, 30],
    "Lars Becker":     [60,  2,  0,  0,  0,  0, 50, 52],
}
db.query("""
    UNWIND keys($profiles) AS name
    MATCH (p:Person {name: name})
    SET p.access_profile = $profiles[name]
""", profiles=PROFILES)

In a real system the counts would come from the Swipe nodes of step 15, with one query per quarter. Hiro, the contractor, and Ines, the visitor, have no profile: the review is about staff.

A vector index¶

A vector index is on one label and one property, and needs to know how many numbers each vector has:

db.query("""
    CREATE VECTOR INDEX access_profiles FOR (p:Person) ON p.access_profile
    OPTIONS {indexConfig: {`vector.dimensions`: 8, `vector.similarity_function`: 'cosine'}}
""")
db.query("CALL db.awaitIndexes()")

Cosine similarity compares directions, not lengths: someone who came in half as often, to the same rooms in the same proportions, has the same profile. That suits a review, where a part-timer should not look unusual. The other choice, 'euclidean', compares the numbers themselves.

The index reads the existing profiles in the background, so that creating it on millions of nodes does not stop the writes meanwhile. On ten nodes it is done at once; db.awaitIndexes() waits for the population to end, the safe thing to do before the first search. Nodes without the property, like Hiro and Ines, are simply not in the index.

The closest profiles¶

The procedure db.index.vector.queryNodes takes the index, how many nodes to return and a vector, and returns the closest nodes, best first, each with its score, between 0 and 1: 1 for the same direction, lower the further apart.

for name, score in db.query("""
    MATCH (chen:Person {name: 'Chen Wei'})
    CALL db.index.vector.queryNodes('access_profiles', 4, chen.access_profile) YIELD node, score
    RETURN node.name, round(score, 3)
"""):
    print(f"{name:<16} {score}")
Chen Wei         1.0
Emeka Nwosu      0.992
Bruno Lindqvist  0.932
Ada Okafor       0.884

Chen comes first, closest to himself; then the people who work like him.

The closest among some¶

Joon will be away for a month, and someone outside Virology must cover his rounds of the containment suite. That is not a question for the whole index: the two closest profiles to Joon’s are his own and Dalia’s, both in Virology. The question is: the closest among the people outside his team. The SEARCH clause asks exactly that. It follows a MATCH, and keeps the closest of the entities that the MATCH gives:

for name, team, score in db.query("""
    MATCH (joon:Person {name: 'Joon Park'})-[:MEMBER_OF]->(virology:Team)
    MATCH (p:Employee)-[:MEMBER_OF]->(team:Team)
    WHERE team <> virology
    SEARCH p IN (VECTOR INDEX access_profiles FOR joon.access_profile LIMIT 2) SCORE AS score
    RETURN p.name, team.name, round(score, 3)
"""):
    print(f"{name:<16} {team:<12} {score}")
Ada Okafor       Biosafety    0.898
Gustavo Pérez    Research     0.771

The pattern and its WHERE choose the candidates, and the search keeps the two closest of them, so two come out whenever at least two people match. Searching first and removing the Virology people afterwards would have left none. The vector can come from any earlier variable, here Joon’s own profile, or from a parameter.

Outliers¶

Now the review itself: for each employee, the teammate with the closest profile. SEARCH runs once for each row of the clauses before its MATCH, here once per employee, with that employee’s profile as the vector:

for name, team, mate, score in db.query("""
    MATCH (p:Employee)-[:MEMBER_OF]->(team:Team)
    MATCH (mate:Employee)-[:MEMBER_OF]->(team)
    WHERE mate <> p
    SEARCH mate IN (VECTOR INDEX access_profiles FOR p.access_profile LIMIT 1) SCORE AS score
    RETURN p.name, team.name, mate.name, round(score, 3) ORDER BY score, p.name
"""):
    print(f"{name:<16} {team:<12} {mate:<16} {score}")
Kemi Adeyemi     Virology     Dalia Haddad     0.812
Chen Wei         Sequencing   Emeka Nwosu      0.992
Emeka Nwosu      Sequencing   Chen Wei         0.992
Fatima Rossi     Animal Care  Lars Becker      0.995
Lars Becker      Animal Care  Fatima Rossi     0.995
Dalia Haddad     Virology     Joon Park        0.998
Joon Park        Virology     Dalia Haddad     0.998

The people alone in their team have no row: their MATCH finds no teammate. (With OPTIONAL MATCH, they would have one row, with null for the teammate.)

A WHERE written after SCORE AS is different from the one before SEARCH: it runs after the search, on its results, and can use the score. It keeps only the employees whose closest teammate is not close:

print(db.query("""
    MATCH (p:Employee)-[:MEMBER_OF]->(team:Team)
    MATCH (mate:Employee)-[:MEMBER_OF]->(team)
    WHERE mate <> p
    SEARCH mate IN (VECTOR INDEX access_profiles FOR p.access_profile LIMIT 1) SCORE AS score
    WHERE score < 0.9
    RETURN p.name, mate.name
"""))
[['Kemi Adeyemi', 'Dalia Haddad']]

Kemi is a Virology technician who spends her days in the vivarium. Maybe she supports the infection studies with animals and all is well; maybe she has access she no longer needs. Either way, the review now has a name to ask about, found without writing a rule for it.

A wrong vector is an error¶

The index checks every write to the property. A profile with the wrong number of rooms is not a vector the index takes, and the write fails, so the mistake shows up at once, not as missing search results later:

from zypherdb import ConstraintViolationError

try:
    db.query("MATCH (p:Person {name: 'Kemi Adeyemi'}) SET p.access_profile = [60, 2, 0]")
except ConstraintViolationError as error:
    print(error)
node 51 cannot have this access_profile: vector index 'access_profiles' takes a list of 8 numbers, and this one has 3 elements (code 10)

A correct write, on the other hand, moves the node in the index in the same transaction, as with every other index. SHOW VECTOR INDEXES lists the vector indexes, with three more columns than SHOW INDEXES: the state, how much of the population is done, and why it failed, if it did:

for name, kind, entity, labels, properties, owning_constraint, options, state, percent, failure in db.query("SHOW VECTOR INDEXES"):
    print(name, labels, properties, state, percent, options["indexConfig"]["vector.dimensions"])
access_profiles ['Person'] ['access_profile'] ONLINE 100.0 8

What you have built¶

Over twenty-one steps, Helix Biolabs got:

  • a graph of its people, teams, buildings, rooms and doors (1–3);

  • the queries to explore it, report on it and keep it current (4–10);

  • access rules as data — team grants with hours, management reach, personal exceptions, bans, training — and the one query that applies them all (11–14);

  • an event log at volume, used safely from many threads and processes (15–17);

  • a door controller that decides, records and tracks in one transaction (18), and a monitor for the rules no door can enforce (19);

  • a searchable safety manual, tied to the rooms it applies to (20);

  • access reviews that compare each person with their peers.

From here, the Cypher reference lists everything the language can do, the Python reference every method, and the technical guides explain how ZypherDB works inside.

Next
The smallest program
Previous
20. The safety manual: full-text search
Copyright ©
Made with Sphinx and @pradyunsg's Furo
On this page
  • 21. Access reviews: vector search
    • Access profiles
    • A vector index
    • The closest profiles
    • The closest among some
    • Outliers
    • A wrong vector is an error
    • What you have built