liveBook · Manning

Text operator

As explained in MongoDB 8.0 in Action, Third Edition

The Atlas Search text operator performs full-text matching against indexed text using the analyzer configured for the search index.

The Atlas Search text operator performs full-text matching on an indexed field using the analyzer configured for the search index. Put it inside a $search stage when an application needs to find documents by words in fields such as a business name. section 11.5.1

To run a text search:

  • Connect to the Atlas cluster with mongosh, select the database, and use an existing Atlas Search index. section 11.5.1
  • Make $search the first pipeline stage. Set its index name, then give text a query and the field path to search. section 11.5.1 section 11.8
  • If several fields must match, put a text condition for each field in compound.must. Add $meta: "searchScore" to inspect each document’s relevance score. section 11.5.1

Match an inspection business name

Run db.inspections.aggregate([{ $search: { index: "MongoDB-in-Action", text: { query: "BUILDING", path: "business_name" } } }, { $set: { score: { $meta: "searchScore" } } }]). The text condition searches business_name; the added score lets you inspect relevance. Scores are dynamic, have no fixed upper limit, and are more meaningful as indicators of stronger matches than as fixed ratings. section 11.5.1

For misspelled input, add fuzzy settings to text. A search for "BUILNG TO SERV INC" with maxEdits: 2 and prefixLength: 1 returns "BUILDING TO SERVE INC." among its results. Check the returned names and scores: fuzzy matching can also return less relevant candidates. The Atlas Search Playground offers a way to test searches without an Atlas account or cluster, but its environment does not persist unless you create a snapshot URL. section 11.5.1 section 11.7

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Defines the operator and works through field matching, compound conditions, scores, and fuzzy settings without detailing analyzer internals.
Examples
Searches inspection business names for BUILDING, combines three required field matches, and tests a misspelled business name.
What it assumes
Assumes an Atlas Search index for cluster queries, while offering account-free experimentation in Atlas Search Playground.
What you can do after
Apply it via Atlas Search $search to match indexed fields, combine required conditions, and inspect relevance scores.
How it connects
Connects the text operator to $search, search indexes, analyzers, compound queries, fuzzy matching, search scores, and aggregation facets.

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.