Atlas 搜索
根据 1.0.0 配置 Atlas Search 操作符、复合/过滤、得分、高亮、序列令牌、分页/计数和 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() 流分开。也可以使用省略索引的 .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(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("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() 和常规排序的优先级按调用顺序确定。为了稳定的分页,建议将得分与稳定的唯一字段排序结合使用。
高亮/得分详情/序列令牌
.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 便利 API 添加,则优先使用 operator, driverOptions, stage, SortSpec.driver, customizeAggregation。