Atlas Search
Configure the Atlas Search operator, compound/filter, score, highlight, sequence token, paging/count, and Driver-native extension paths based on version 1.0.0.
Basic Atlas Search
$search and $searchMeta must be the first stage of the aggregation pipeline, so they are separated from the general fields(...).end() flow. You can also use .search() without an index.
Search operator
• root builder: text, phrase, autocomplete, equals, exists, in, range, compound
• reusable clause: SearchOperators.text/phrase/autocomplete/equals/in/exists/range
• Driver-native root operator: operator(SearchOperator)
• named Driver wrapper: SearchOperators.driver(name, operator)
compound supports must, mustNot, should, filter, minimumShouldMatch, and score, and you can also directly pass a general AtlasSearchOperator.
Text / Phrase / Autocomplete
• text: path(s), query(ies), fuzzy, matchCriteria, synonyms, score
• phrase: path(s), query(ies), slop, synonyms, score
• autocomplete: single path, query(ies), tokenOrder, fuzzy, score
• Do not use fuzzy(...) and synonyms(...) simultaneously.
Difference between Search clause and post-search fields
search().fields(...) is a general $match after $search, not an internal filter of $search. To limit candidates at the search index stage, use compound.filter(...).
Score and Sorting
scoreDesc()/scoreAsc() and general sort determine priority in the order they are called. For stable pagination, it is advisable to include stable unique field sorting along with the score.
Highlight / score details / sequence token
The default aliases are highlights, scoreDetails, searchSequenceToken. addFieldsScoreDetails() also activates score details in the Search option.
Paging / sequence token / count
count().execute()
The final pipeline count reflecting post-search fields(...) and score threshold.
count().executeSearchMeta()
$searchMeta metadata count. Supports SearchCountType.TOTAL/LOWER_BOUND and does not execute post-search $match.
existsQuery().execute()
Checks if there is more than one result in the Search pipeline.
Driver-native extension
stage(Bson) is placed immediately after $search and is not applied to the $searchMeta metadata-count pipeline of executeSearchMeta(). If new Driver features are added before the DSL convenience API, prioritize using operator, driverOptions, stage, SortSpec.driver, and customizeAggregation.