M3-S4 — Indexing and search
Section titled “M3-S4 — Indexing and search”| Module | M3 — The read side |
| After this session you can | Say which index answers a search, trace how a renamed record’s title reaches the records that point to it, and diagnose stale or missing results. |
| Time | Facilitated: 53 min · On your own: 53 min |
| Before you start | M3-S3: read models kept current from events. Larkspur Lab with the logs, bus-tap and record-store windows (workbench). |
| Go deeper | Engine: indexing and search · Entity indexes |
◆ Orientation (1 min) — “I renamed it, and search still finds the old name”
Section titled “◆ Orientation (1 min) — “I renamed it, and search still finds the old name””In the placement check of the course page you answered why a renamed supplier is not found by its new name for a minute, and the answer was “search is updated afterwards, from an event”. That was the operator’s answer. The master’s answer is precise: which index, which event, which service, which queue, how long, and what to look at when the minute becomes an hour. This session follows one rename, Kestrel Seals becoming Kestrel Sealing Systems, through every place its name is copied.
▤ Concept (7 min) — Three indexes, one pipeline
Section titled “▤ Concept (7 min) — Three indexes, one pipeline”Three things called “index”
Section titled “Three things called “index””The word means three different things in the platform, built and used independently. Mixing them up is the commonest mistake in a search diagnosis.
| Index | What it is | Who builds it | What it answers |
|---|---|---|---|
| Database indexes | MongoDB’s own indexes on each entity’s collection: the implicit ones (store, order, sharing), up to ten custom ones a builder defines, and one text index | Declared on the entity’s definition by the data service; created by the indexing service | Fast filters and sorts (M3-S2); the text index makes search possible |
| The search text | A field on every record, searchIndex.fullTextSearch: one string joining the values of the record’s searchable fields, with references and categories replaced by their titles and labels | The data service on write; the indexing service when something the record shows changes | What a free-text search actually matches |
| The title index | A separate collection, one document per record (and per page and list), holding its resolved title, picture, its store, its state and the records it references | The indexing service, from record events | Titles of referenced records in lists and charts (M3-S2’s resolve pass, M3-S3’s bucket names), and who refers to whom |
A searchable field is a builder’s choice per field. An encrypted field is never searchable, whatever the setting, because its value must never reach the search text.
Two services, one pipeline
Section titled “Two services, one pipeline”The data service owns definitions and runs the searches. The indexing service is a worker with no API of its own: it consumes events from its queue on the bus (M2-S2) and runs jobs from Redis (the BullMQ jobs of M2-S2) for anything large. Its handlers:
- A record is created or updated. The record’s own search text is recomputed at once, but only if a searchable field actually changed; its title-index document is created or updated.
- A record’s title changes. The title-index documents of records whose title is that reference are updated in one write. And a background job finds every record that references the changed one through a searchable reference field, groups them by entity, cuts them into chunks of a thousand, and recomputes their search text inside the database, chunk by chunk.
- A definition changes. The entity’s database indexes are synchronised with its definition, as a queued job (since 1.10, off the bus thread), and a job rewrites every record’s search text if the change requires it (for example a field became searchable). A change of which field is the title rewrites the title index for the whole entity.
- A record is archived or deleted. Archiving marks its title-index document archived; only a permanent delete removes it.
flowchart LR
W["data service:<br/>Kestrel renamed"] --> B[("bus")]
B --> P["data service handler:<br/>refresh proxies<br/>(Approved, Audited Suppliers)"]
P --> B
B --> I["indexing service"]
I --> T["title index:<br/>Kestrel's title"]
I --> J["job: records that<br/>reference Kestrel"]
J --> S["search text of purchase orders,<br/>non-conformances, in chunks"]
What to notice: the rename travels in hops. Purchase orders do not point at the supplier itself but at Procurement’s proxy, so the proxy record must be refreshed first, and its own update event is what starts the purchase orders’ reindex.
Searching
Section titled “Searching”A free-text search is a database text match on the entity’s text index, which covers the search text. It runs as the first stage of the list-read pipeline (M3-S2), so it narrows rows before anything else. Three properties explain most surprises:
- No language processing. The text index is built with no language: no stemming and no stop words. seal does not find sealing; a search finds whole words as they were stored.
- Labels and titles, not ids. Because references and categories enter the search text as their titles and labels, searching purchase orders for Kestrel works, but only after the reindex of the previous section has reached them.
- A view can turn search off. A view that does not allow search has any text condition removed before the query runs.
Consistency: what you may assume
Section titled “Consistency: what you may assume”The pipeline is eventually consistent. The written record is correct at once; its own search text follows within its handler; everything copied elsewhere (titles in the title index, search text of referencing records) follows after events and jobs, with no transaction across services. The guarantees that make that safe:
- Re-delivery is harmless. Index writes are upserts or checked creates, so a replayed event converges on the same result (M2-S2’s “at least once”).
- No ordering between handlers. The title handler and the search-text handler for the same event run independently; neither waits for the other.
- Some failures are swallowed on purpose. Title propagation and index clean-up log their failure and let the main flow continue, so a failed propagation leaves stale titles without retrying. The log is the only evidence; you will look for it in the lab.
- Big changes are slow by design. A job runs one chunk at a time per queue, so a rename of a supplier referenced by 80,000 purchase orders takes many chunks, during which search is partly old, partly new.
Database indexes: custom ones and their status
Section titled “Database indexes: custom ones and their status”Since 1.10 a builder can define up to ten custom indexes on an entity, alongside the implicit ones.
Each has a status on the entity’s definition (building, ready, failed, with the error),
written by the indexing service as it synchronises. An index stays building while the database builds
it, which on a large collection takes minutes, and a failed one (for example a unique index over data
that is not unique) is reported there, not as an error to the builder who saved the entity. Read the
status before you conclude that an index “does not work”.
▶ Guided demo (12 min) — A scripted rename
Section titled “▶ Guided demo (12 min) — A scripted rename”In Larkspur Lab. The demo renames a supplier and reads every copy of its name before and after. Use
the correlation id lk-m3s4-rename for the save, so its whole trail can be found.
-
Before: read the supplier’s title-index document and one purchase order’s search text.
// mongosh, lk-dataconst k = db.suppliers.findOne({"value.supplier_name": "Kestrel Seals"}, {id: 1})db.DataEntryIndex.findOne({entry: k.id}, {"meta.mainTitle": 1})db.purchase_orders.findOne({"searchIndex.fullTextSearch": /Kestrel Seals/}, {"value.po_number": 1, "searchIndex.fullTextSearch": 1}) -
Rename through the API’s write path as Rami Aziz would in the portal: open Supplier Management › Suppliers › Kestrel Seals, set Supplier Name to Kestrel Sealing Systems, and save. (To tag the save with your id, use the browser’s network tab as in M2-S1 and note the id it was given instead.)
-
Search the logs for the save’s id and keep only the indexing service’s lines:
… | grep "<id>" | grep -i index. You see the supplier’s own handlers, then the proxy refresh in the data service, then the indexing service handling the proxies’ updates and scheduling the referencing-records job. -
Re-run step 1’s two reads after ten seconds. The title index already shows the new name; count the purchase orders whose search text has the new name:
db.purchase_orders.countDocuments({"searchIndex.fullTextSearch": /Kestrel Sealing Systems/}). -
Repeat the count every ten seconds until it stops growing, and note the time it took.
-
In the portal, search Procurement › Purchase Orders for Sealing: the orders appear.
-
Search for Seal: nothing new appears, because there is no stemming.
Screenshot pending The Purchase Orders list in the portal searched for Sealing, showing orders whose supplier column reads Kestrel Sealing Systems What to notice: the supplier column (from the title index) and the search (from the search text) are two different copies of the name, kept by two different handlers.
⛏ Lab (23 min) — Follow a rename to every copy
Section titled “⛏ Lab (23 min) — Follow a rename to every copy”Goal. A second rename traced to every place the name is copied, with the time each copy took, and one stale copy found and explained.
Before you start. Larkspur Lab; the logs, bus-tap and record-store windows. A file
m3-s4-rename.md with a table: copy, where it lives, before, after, how long.
Before the rename
- Pick Orvane Castings. List every place its name should appear: the supplier itself, Procurement’s Approved Suppliers, Quality’s Audited Suppliers, purchase orders, non-conformances, supplier certifications. For each, find where the name is stored (search text, title index, a mapped field).
- Check which reference fields are searchable: read
fields[] | {normalizedName, searchable: .options.searchable}forpurchase_orders,non_conformancesandsupplier_certificationsthrough the API (as in M2-S3’s demo). - Record each copy’s current value in your table.
The rename
- Empty the tap and switch the tracer on (workbench). Rename Orvane Castings to Orvane Foundry in the portal and note the time.
- Read the tap: list the published messages’ names in order. Find the supplier’s update, the two proxies’ updates, and any job-related message.
- Every fifteen seconds for two minutes, re-read each copy in your table. Note when each changed.
- Search for Foundry in Procurement › Purchase Orders, Quality and Compliance › Non-conformances and Supplier Management › Supplier Certifications.
Explain
- For each copy, say which handler or job changed it, from the Concept.
- One copy did not change, or one search did not find the new name. Find out why, from step 2.
- Read the indexing service’s log for the job’s completion report (records and total time):
kubectl -n larkspur logs deploy/<indexing deployment> --since=10m | grep -i -E "report|search text". - Switch the tracer off.
- Estimate the time for the same rename in production, where Orvane has 12,000 purchase orders, and write what you would tell Sara Qasim’s team before renaming a big supplier at 10:00.
Check.
- Your table lists every copy with where it lives, before, after and the time it took.
- The tap shows the supplier’s update followed by both proxies’ updates.
- Searches for Foundry find purchase orders and non-conformances after the reindex.
- You explained the copy or search that did not follow, from a field’s searchable setting.
- Your production estimate is derived from the job’s report, not guessed.
Pass condition. All five checks hold.
If something goes wrong
| What you see | What to do |
|---|---|
| The tap holds no proxy update | The proxies may copy only some fields; check that Supplier Name is one of Approved Suppliers’ fields. If it is not, the proxy does not change and nothing downstream is reindexed: that is a finding. |
| No copy changes for minutes | Check the indexing service’s queue on the bus (rabbitmqctl list_queues -p lk) for a backlog, and its pod for restarts. |
| The completion report is not in the log | The job may log at another level; search for the correlation id instead, or for chunk. |
| The search for Foundry finds nothing anywhere | The view you searched may not allow search; try the entity’s default list, or the API with search=Foundry. |
DataEntryIndex is not found | List collections with db.getCollectionNames().filter(n => /index/i.test(n)) and use the title-index collection your release has. |
Model answer
Copies of the name. The supplier’s own Supplier Name and its search text (the data service, inline); its title-index document (the indexing service, seconds); Approved Suppliers and Audited Suppliers records (the data service’s proxy refresh, from the supplier’s update event, a second or two); their own title-index documents (the indexing service, from the proxies’ update events); the search text of purchase orders and non-conformances (background jobs, chunk by chunk, seconds to a minute in the lab); supplier certifications: not changed, because their Supplier reference is not searchable, so no job reindexes them, and a search for Foundry there finds nothing. Their supplier column still shows the new name, because that comes from the title index, not the search text.
The tap. …/data-entry-updated for the supplier, then two more for the proxy records, then the
updates the reindex itself causes, if any.
Estimate. If the lab’s report says, for example, 400 purchase orders in 3 s across one chunk, then 12,000 is twelve chunks run one after another, roughly half a minute to a minute more, during which some orders are found by the old name and some by the new. For Sara’s team: “Rename big suppliers outside working hours, or warn buyers that search will show a mix for a few minutes; titles in lists change at once.”
◇ Worked scenario (3 min) — Search lost a whole entity
Section titled “◇ Worked scenario (3 min) — Search lost a whole entity”On Monday, Tariq Saleh reports that searching non-conformances finds nothing at all, for any word, since the weekend. Lists and filters work. Reason it through before reading each step.
Step 1: which index? Lists and filters work, so the records and the database’s ordinary indexes are fine. Only free text fails, so the suspects are the text index and the search text.
Step 2: what changed? The change log shows that on Saturday a Quality builder renamed the entity’s
normalized name from nc_records to non_conformances while tidying up. The implicit indexes,
including the one text index a collection may have, are declared on the entity under names built from
the normalized name (IX_<normalized name>…). After a rename the definition can carry copies under the
old names beside the new ones, and the database refuses a second text index on one collection, so the
new one fails to build. Index synchronisation matches the text index by its name, and treats one
under an unexpected name as not the platform’s: once it is gone, the entity has no text index, and
every free-text search on it fails, while filters, which use other indexes, keep working.
Step 3: confirm before you report. You read the entity’s index status on the affected system
(M3-S4’s custom-index section): the new text index is failed, and the collection’s indexes show no
text index under the expected name. Repeating the rename in Larkspur Lab does not reproduce it: from
1.10.0 a rename drops the old-named copies and builds the text index under the new name, which already
points to the fix. Evidence, not a theory.
Step 4: the fix. The 1.10 line ships migrations for exactly this: one drops the stale index copies a renamed entity kept under its old name, and one heals a missing full-text index by creating it, or recreating an equivalent one, under the expected name. In production you apply the release that carries them, or ask the operator to follow its release notes; you never create or drop indexes by hand in the platform’s database.
The lesson. A normalized name is a contract (M2-S3), and here the contract was with an index. The rename was a Class B change that nobody recognised as one. Your standard (M10-S1) gains a rule: “never rename an entity’s normalized name in production; if a rename is unavoidable, rehearse it in Larkspur Lab and check search afterwards.”
✓ Practice (6 min) — Diagnose search
Section titled “✓ Practice (6 min) — Diagnose search”A list of purchase orders shows the supplier’s new name at once, but a search for that new name finds the orders only a minute later. Why are the two different?
Check your answer
The correct answer is B. Open the explanation under each option to see why.
A builder marks Bank Account as encrypted on Suppliers, then also ticks it as searchable. What can a search find afterwards?
Check your answer
The correct answer is C. Open the explanation under each option to see why.
A technician searches work orders for bearings and finds nothing, although several titles contain bearing. What explains it?
Check your answer
The correct answer is A. Open the explanation under each option to see why.
A builder adds a unique custom index on PO Number and saves; purchase orders still filter slowly an hour later. What do you check first?
Check your answer
The correct answer is B. Open the explanation under each option to see why.
The title-propagation step fails once because of a brief database error. What happens to the titles it should have updated?
Check your answer
The correct answer is C. Open the explanation under each option to see why.
↺ Recap (1 min) — Every copy of a name
Section titled “↺ Recap (1 min) — Every copy of a name”You can now tell the three indexes apart, trace a rename through the proxy refresh, the title index and the reindex jobs, explain what search can and cannot match, read a custom index’s status, and diagnose stale or missing results with evidence from the logs, the bus and the record store.
Next. M3-S5 closes the read side with the engines that turn all of this into screens: how the web portal and the mobile app draw what a builder configured, and where they differ.