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
Flux<Article> results = dsl
.executeEntity(Article.class, MongoTemplateName.FRONT)
.search("articles_default")
.text(text -> text
.path("title")
.query("reactive mongo dsl")
)
.findAll()
.execute();$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)
.search("articles_default")
.compound(compound -> compound
.mustText("title", text -> text.query("mongodb"))
.shouldText("summary", text -> text.query("mongodb"))
.filterEquals("status", eq -> eq.value("PUBLISHED"))
.mustNotExists("deletedAt")
.minimumShouldMatch(0)
)
.findAll()
.execute();compound supports must, mustNot, should, filter, minimumShouldMatch, and score, and you can also directly pass a general AtlasSearchOperator.
Text / Phrase / Autocomplete
.text(text -> text
.paths("title", "summary")
.query("reactive mongo dsl")
.matchCriteria(SearchMatchCriteria.ALL)
.fuzzy(1, 1, 50)
.score(SearchScoreSpec.boost(3.0))
)• 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("articles_default")
.text(text -> text.path("title").query("mongodb"))
.fields(
pair("deleted", false),
pair("status", "PUBLISHED")
)
.findAll()
.execute();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(...).
$search
-> stage(Bson) if configured
-> $match (fields)
-> metadata addFields / score match
-> paging
-> projectionScore and Sorting
.search("articles_default")
.text(text -> text.path("title").query("mongodb"))
.addFieldsScore("searchScore")
.matchScoreGte(1.5)
.scoreDesc()
.sorts(sort -> sort.desc("publishedAt").desc("id"))
.findAll()
.execute();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
.search("articles_default")
.text(text -> text.path("title").query("mongodb"))
.highlight(highlight -> highlight
.path("title")
.maxCharsToExamine(100_000)
.maxNumPassages(3)
)
.addFieldsHighlights()
.addFieldsScoreDetails()
.addFieldsSequenceToken()
.findAll()
.execute();The default aliases are highlights, scoreDetails, searchSequenceToken. addFieldsScoreDetails() also activates score details in the Search option.
Paging / sequence token / count
Mono<PageResult<Article>> page = dsl
.executeEntity(Article.class, key)
.search("articles_default")
.text(text -> text.path("title").query("mongodb"))
.paging(0, 20)
.findAll()
.executePage();
// sequence token
.searchAfter(token)
// or
.searchBefore(token)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
.search("search-index")
.operator(driverSearchOperator)
.driverOptions(options -> options.option("someNewOption", value))
.stage(Aggregates.score("$rating"))
.findAll()
.execute();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.