Overview
在本指南中,您可以学习如何使用 Scala 驱动程序在集合上运行 MongoDB Search 查询。MongoDB Search 使您能够对您的集合执行全文搜索。MongoDB Search 索引指定搜索行为以及要索引的字段。
要了解如何使用 Scala 驱动程序创建和管理 MongoDB Search 索引,请参阅 MongoDB Search 和 MongoDB Vector Search 索引指南。
样本数据
本指南中的示例使用Atlas示例数据集的 sample_mflix数据库中的 movies集合。要学习如何创建免费的MongoDB Atlas 集群并加载示例数据集,请参阅Atlas入门。
运行MongoDB搜索查询
注意
MongoDB Search 可在以下部署上使用:
MongoDB Atlas v4.2 或更新版本
MongoDB Community Edition v8.2 或更高版本
您的集合必须具有 MongoDB Search 索引才能运行 MongoDB Search 查询。要学习如何创建 MongoDB Search 搜索索引,请参阅 MongoDB Search 和 MongoDB Vector Search 索引指南。
要执行全文搜索,请使用 $search 管道阶段调用 aggregate() 方法。Scala 驱动程序提供 Aggregates.search() 辅助方法来创建此阶段。search() 方法需要以下参数:
SearchOperator指定要搜索的字段和文本的实例SearchOptions指定用于自定义搜索的选项的实例,包括要使用的 MongoDB Search 索引的名称
以下示例在 title 字段中搜索文本 "Alabama",并仅投影匹配文档的 _id 字段(默认包含)和 title 字段:
val operator = SearchOperator.text( SearchPath.fieldPath("title"), "Alabama") val options = searchOptions().index("<search index name>") val pipeline = Seq( Aggregates.search(operator, options), Aggregates.project(Projections.include("title")) ) try { Await .result(collection.aggregate(pipeline).toFuture(), 20.seconds) .foreach((doc: Document) => println(doc.toJson())) } catch { case e: Throwable => println(s"There was an error: $e") }
提示
要查看 Atlas 文档中的 MongoDB Search 教程,请参阅 运行 Atlas 搜索查询。
MongoDB搜索元数据
注意
MongoDB Search 元数据在以下部署中可用:
MongoDB Atlas v4.4.11 或更新版本
MongoDB Community Edition v8.2 或更高版本
使用 Aggregates.searchMeta() 辅助方法仅从 MongoDB Search 查询中检索元数据,而不返回搜索结果。以下示例返回搜索 year 字段中接近 2010 值的查询的元数据:
try { Await .result( collection.aggregate(Seq( Aggregates.searchMeta( SearchOperator.near(2010, 1, SearchPath.fieldPath("year"))) )).toFuture(), 20.seconds ) .foreach((doc: Document) => println(doc.toJson())) } catch { case e: Throwable => println(s"There was an error: $e") }
使用搜索操作符
Scala驱动程序为以下操作符提供了辅助方法:
Operator | 说明 |
|---|---|
从不完整输入字符串中搜索包含字符序列的单词或短语。 | |
将两个或多个操作符组合到一个查询中。 | |
检查字段是否与您指定的值匹配。映射到 | |
测试指定索引字段名称的路径在文档中是否存在。 | |
在给定路径搜索由BSON数字、日期、布尔值、ObjectId、uuid 或字符串值组成的大量,并返回该字段的值等于指定大量中任意值的文档。 | |
返回与输入文档类似的文档。 | |
支持对数字、日期和GeoJSON point值进行查询和评分。 | |
使用索引配置中指定的分析器搜索包含有序术语序列的文档。 | |
支持查询索引字段和值的组合。 | |
支持对数字、日期和字符串值进行查询和评分。 映射到 | |
将查询字段解释为正则表达式。 | |
使用在索引配置中指定的分析器执行全文搜索。 | |
启用在搜索字符串中使用可匹配任何字符的特殊字符的查询。 |
管道搜索阶段示例
在运行此示例之前,您必须在具有以下定义的 movies 集合上创建 MongoDB 搜索索引:
{ "mappings": { "dynamic": true, "fields": { "title": { "analyzer": "lucene.keyword", "type": "string" }, "genres": { "normalizer": "lowercase", "type": "token" } } } }
要了解有关创建 MongoDB Search 索引的更多信息,请参阅MongoDB Search 和 MongoDB 向量搜索索引指南。
以下代码创建具有以下规范的 $search 阶段:
检查
genres大量是否包含"Comedy"在
fullplot字段中搜索字段"new york"匹配介于
1950和2000(含)之间的year值搜索以术语
"Love"开头的title值
val searchStage = Aggregates.search( SearchOperator.compound() .must( Iterable( SearchOperator.in(fieldPath("genres"), List("Comedy")), SearchOperator.phrase(fieldPath("fullplot"), "new york"), SearchOperator.numberRange(fieldPath("year")).gtLt(1950, 2000), SearchOperator.wildcard("Love *", fieldPath("title")), ).asJava ) ) val projectStage = Aggregates.project( Projections.include("title", "year", "genres")) try { Await .result( collection.aggregate(Seq(searchStage, projectStage)).toFuture(), 20.seconds ) .foreach((doc: Document) => println(doc.toJson())) } catch { case e: Throwable => println(s"There was an error: $e") }
要学习有关MongoDB 搜索辅助程序方法的更多信息,请参阅驱动程序 Core API 文档中的SearchOperator接口参考。
更多信息
有关 MongoDB Search 的更多信息,请参阅 Atlas 文档中的MongoDB Search 概述。
API 文档
要进一步了解本指南所讨论的方法和类型,请参阅以下 API 文档: