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
