Overview
在本指南中,您可以学习如何使用Java驱动程序对集合运行Atlas Search搜索查询。Atlas Search使您能够对MongoDB Atlas上托管的集合执行全文搜索。Atlas Search索引指定搜索行为以及要索引的字段。
样本数据
The examples in this guide use the movies collection in the sample_mflix database from the Atlas sample datasets. To learn how to create a free MongoDB deployment and load the sample datasets, see the MongoDB Get Started guide. To learn more about aggregation operations and builders, see the Aggregation guide.
运行 Atlas Search 查询
本节将介绍如何创建聚合管道以在集合上运行 Atlas Search 查询。您可以使用 Aggregates.search() 构建器方法来创建 $search 管道阶段,该阶段指定搜索条件。然后,调用 aggregate() 方法,并将您的管道作为参数传递。
注意
仅适用于 Atlas for MongoDB v4.2 及更高版本
此聚合管道操作符仅适用于在运行v4.2 或更高版本的MongoDB Atlas集群上托管且由Atlas Search索引覆盖的集合。从Atlas Search文档了解详情有关此操作符所需设置和功能的更多信息。
在运行Atlas Search查询之前,您必须在集合上创建Atlas Search索引。要学习;了解如何创建Atlas Search索引,请参阅MongoDB Atlas文档中的支持的客户端。
Atlas Search 示例
此示例通过执行以下操作来运行Atlas Search查询:
使用
Aggregates.search()构建器方法构建$search阶段,指示驱动程序查询title字段中包含单词"Alabama"的文档使用
Aggregates.project()构建者方法构造$project阶段,指示驱动程序在查询结果中包含title字段将管道阶段传递到
aggregate()方法并打印结果
collection.aggregate( Arrays.asList( Aggregates.search(SearchOperator.text( SearchPath.fieldPath("title"), "Alabama")), Aggregates.project(Projections.include("title")) ) ).forEach(doc -> System.out.println(doc.toJson()));
{"_id": {"$oid": "..."}, "title": "Alabama Moon"} {"_id": {"$oid": "..."}, "title": "Crazy in Alabama"} {"_id": {"$oid": "..."}, "title": "Sweet Home Alabama"}
Atlas Search 元数据
使用 searchMeta() 方法创建 $searchMeta 管道阶段,该阶段仅返回Atlas Search结果中的元数据。
提示
仅适用于 Atlas for MongoDB v4.4.11 及更高版本
此聚合管道操作符仅适用于运行 v4.4.11 及更高版本的 MongoDB Atlas 集群。有关可用版本的详细列表,请参阅 MongoDB Atlas 文档中的 $searchMeta.
以下示例显示了 Atlas 搜索聚合阶段的 near 元数据:
Aggregates.searchMeta( SearchOperator.near(2010, 1, SearchPath.fieldPath("year")));
要学习;了解有关此辅助方法的更多信息,请参阅 searchMeta() API文档。
创建管道搜索阶段
Java 驱动程序为以下操作符提供了辅助工具/辅助程序:
Operator | 说明 |
|---|---|
从不完整输入字符串中搜索包含字符序列的单词或短语。 | |
将两个或多个操作符组合到一个查询中。 | |
检查字段是否与您指定的值匹配。映射到 | |
测试指定索引字段名称的路径在文档中是否存在。 | |
在给定路径搜索由BSON数字、日期、布尔值、ObjectId、uuid 或字符串值组成的大量,并返回该字段的值等于指定大量中任意值的文档。 | |
返回与输入文档类似的文档。 | |
支持对数字、日期和GeoJSON point值进行查询和评分。 | |
使用索引配置中指定的分析器搜索包含有序术语序列的文档。 | |
支持查询索引字段和值的组合。 | |
支持对数字、日期和字符串值进行查询和评分。 映射到 | |
将查询字段解释为正则表达式。 | |
使用在索引配置中指定的分析器执行全文搜索。 | |
启用在搜索字符串中使用可匹配任何字符的特殊字符的查询。 |
管道搜索阶段示例
注意
Atlas样本数据集
此示例使用Atlas示例数据集中的 sample_mflix.movies集合。要学习;了解如何设立免费套餐的Atlas 集群并加载示例数据集,请参阅Atlas文档中的Atlas入门教程。
在运行此示例之前,您必须在具有以下定义的 movies集合上创建MongoDB搜索索引:
{ "mappings": { "dynamic": true, "fields": { "title": { "analyzer": "lucene.keyword", "type": "string" }, "genres": { "normalizer": "lowercase", "type": "token" } } } }
要学习;了解有关创建MongoDB搜索索引的更多信息,请参阅MongoDB Atlas文档中的支持的客户端。
以下代码创建具有以下规范的 $search 阶段:
检查
genres大量是否包含"Comedy"在
fullplot字段中搜索字段"new york"匹配介于
1950和2000(含)之间的year值搜索以术语
"Love"开头的title值
List<Bson> pipeline = new ArrayList<>(); pipeline.add(Aggregates.search( SearchOperator.compound() .filter( List.of( SearchOperator.in(fieldPath("genres"), "Comedy"), SearchOperator.phrase(fieldPath("fullplot"), "new york"), SearchOperator.numberRange(fieldPath("year")).gtLt(1950, 2000), SearchOperator.wildcard(fieldPath("title"), "Love *") )))); pipeline.add(Aggregates.project( Projections.include("title", "year", "genres") )); AggregateIterable<Document> results = collection.aggregate(pipeline); results.forEach(doc -> System.out.println(doc.toJson()));
{"_id": ..., "genres": ["Comedy", "Romance"], "title": "Love at First Bite", "year": 1979} {"_id": ..., "genres": ["Comedy", "Drama"], "title": "Love Affair", "year": 1994}
要学习;了解有关MongoDB Search 助手方法的更多信息,请参阅驱动程序核心API文档中的 SearchOperator 接口参考。
更多信息
要学习;了解有关Atlas Search 的更多信息,请参阅Atlas文档中的Atlas Search 。
API 文档
要学习;了解有关本指南中提到的方法的更多信息,请参阅以下API文档: