Module 6 · Tools and agents
Lesson 6.5
Routing questions
A router reads the question and picks the way to find sources, such as a lookup or a search.
The problem
Why it matters
Without it
One search method handles every question. Exact ids miss, error codes match the wrong text, and counts are never computed.
With it
The router sends each question to the method that suits it. Each method finds the sources it does best.
How it works
A rule is a pattern that matches text
A regular expression is a pattern that matches text. This pattern reads the word order, a space, a capital letter, a dash, and digits. It matches an order id such as A-1042, and the test method returns true when it matches.
const pattern = /order [A-Z]-\d+/;console.log(pattern.test("Where is order A-1042?"));console.log(pattern.test("Can I return an opened item?"));Output
true false
The first matching rule wins
Rules are checked from top to bottom, and the first match decides the route. Narrow rules sit at the top, so a broad rule cannot take their questions. The last route has no rule, so it catches every question left over.
Order Rule matches Example Route 1 an order id like A-1042 Where is order A-1042? exactId 2 the word error and a number What does error 4031 mean? keyword 3 how many or total How many orders shipped? count 4 no rule matches Can I return an opened item? meaning The rules in the order the router checks them. The router as code
The route function holds the rules in order. The keyword rule takes the error question, because it is the first rule that matches. The sample prints the route name for that question.
function route(question: string): string { if (/order [A-Z]-\d+/.test(question)) return "exactId"; if (/error \d+/i.test(question)) return "keyword"; return "meaning";}console.log(route("What does error 4031 mean?"));Output
keyword
The default route catches the rest
A question that matches no rule takes the default route, which is the meaning search. The sample sends four questions through the router, and each one gets a route name.
- Where is order A-1042?exactId
- What does error 4031 mean?keyword
- How many orders shipped last week?count
- Can I return an opened item?meaning, default
Four questions and the route each one takes.
Common mistakes
What goes wrong
- Writing one broad rule first. It takes questions that a narrow rule should have caught.
- Routing without a test set. Keep questions with known routes and check each rule against them.
- Sending every question to a model route. Rules are cheaper and easier to check.
Use it when and skip it when
Is it the right tool?
Use it when
- Your sources sit in different stores, such as a table and a document index.
- Some questions need one exact record and others need meaning.
Skip it when
- All questions go to one source, so one search is enough.
Words to know
Terms in this lesson
- Router
- Code that picks the route for each question.
- Route
- One way to find sources, such as a lookup or a search.
- Regular expression
- A pattern that matches a piece of text, such as an order id.
- Default
- The route used when no other rule matches.