Atlas 搜索
根據 1.0.0 配置 Atlas Search 操作符、compound/filter、score、highlight、sequence token、paging/count 和 Driver-native 擴展路徑。
基本 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 和 $searchMeta 必須是聚合管道的第一個階段,因此與一般的 fields(...).end() 流程分開。也可以使用省略 index 的 .search()。
Search 操作符
• 根構建器:text、phrase、autocomplete、equals、exists、in、range、compound
• 可重用條件:SearchOperators.text/phrase/autocomplete/equals/in/exists/range
• Driver-native 根操作符:operator(SearchOperator)
• 命名的 Driver 包裝器: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 支持 must、mustNot、should、filter、minimumShouldMatch、score,也可以直接傳遞一般的 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:路徑、查詢、模糊、匹配標準、同義詞、分數
• phrase:路徑、查詢、slop、同義詞、分數
• autocomplete:單一路徑、查詢、tokenOrder、模糊、分數
• fuzzy(...) 和 synonyms(...) 不會同時使用。
Search 條件和後搜索字段的區別
.search("articles_default")
.text(text -> text.path("title").query("mongodb"))
.fields(
pair("deleted", false),
pair("status", "PUBLISHED")
)
.findAll()
.execute();search().fields(...) 不是 $search 內部的過濾器,而是 $search 後的一般 $match。要在搜索索引階段限制候選項,請使用 compound.filter(...)。
$search
-> stage(Bson) if configured
-> $match (fields)
-> metadata addFields / score match
-> paging
-> projection分數和排序
.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() 和一般排序的優先級根據調用順序確定。為了穩定的分頁,建議將分數與穩定的唯一字段排序一起使用。
Highlight / 分數詳情 / 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();基本別名為 highlights、scoreDetails、searchSequenceToken。addFieldsScoreDetails() 也會啟用搜尋選項的分數詳情。
分頁 / 序列令牌 / 計數
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()
post-search fields(...) 和分數閾值反映的最終管道計數。
count().executeSearchMeta()
$searchMeta 元數據計數。支持 SearchCountType.TOTAL/LOWER_BOUND,post-search $match 不會執行。
existsQuery().execute()
檢查搜尋管道結果是否存在一個以上的記錄。
Driver-native 擴展
.search("search-index")
.operator(driverSearchOperator)
.driverOptions(options -> options.option("someNewOption", value))
.stage(Aggregates.score("$rating"))
.findAll()
.execute();stage(Bson) 在 $search 之後進行,並不適用於 executeSearchMeta() 的 $searchMeta 元數據計數管道。如果新的 Driver 功能比 DSL convenience API 先添加,則優先使用 operator、driverOptions、stage、SortSpec.driver、customizeAggregation。