如何在 Atlas Search 中使用分面
本教程介绍如何使用分面定义 在sample_mflix.movies
集合中为字符串、日期和数字字段创建索引。教程展示了如何针对这些字段运行 Atlas Search 查询,从而获取按字符串字段值以及按日期和数字字段范围分组的结果(结果包含各组计数)。它将引导您完成以下步骤:
使用
sample_mflix.movies
集合中的genres
、released
和year
字段的分面定义设置 Atlas Search 索引。针对
sample_mflix.movies
集合中的released
字段运行 Atlas Search 查询,以获取按genres
字段的值和year
字段的范围分组的结果。
要创建 Atlas Search 索引,您必须拥有 Project Data Access Admin
或更高的项目访问权限。
先决条件
要完成这些教程,除了 Atlas Search 教程页面中列出的先决条件外,您还必须有一个运行以下版本之一的 Atlas cluster:
MongoDB 5.0.4+
MongoDB 6.0+
MongoDB 7.0+
为分面创建 Atlas Search 索引
在本部分,您将在 sample_mflix.movies
集合中的 genres
、year
和 released
字段上创建 Atlas Search 索引。
AtlasGoClusters在Atlas中,Go项目的 页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含所需项目的组织。
如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。
如果尚未出现,请单击侧边栏中的 Clusters(集群)。
显示 集群页面。
指定索引定义。
您可以创建使用动态映射或静态映射的 Atlas Search 索引。要了解有关动态和静态映射的更多信息,请参阅静态和动态映射。
以下索引定义将genres
字段静态索引为stringFacet
类型,并动态索引 movies
集合中每个文档中受支持类型的其他字段。您可以使用Atlas SearchVisual Editor 或Atlas SearchJSON Editor 在Atlas 用户界面中创建索引。
单击 Next,然后单击 Review Your Index。
单击 Field Mappings 部分下的 Add Field Mapping。
单击 Customzed COnfiguration,然后从下拉列表中选择以下内容:
Field Name
genres
Data Type
StringFacet
单击 Add,然后单击 Save Changes。
单击 Next(连接)。
查看索引定义。
索引定义应类似于以下内容:
{ "mappings": { "dynamic": true, "fields": { "genres": { "type": "stringFacet" } } } } 单击 Next(连接)。
搜索集合
➤ 使用选择语言下拉菜单设置本节中示例的语言。
您可以在使用 $search
和 $searchMeta
阶段的查询中使用分面。在本节中,连接到您的 Atlas 集群,然后使用$searchMeta
或$search
阶段针对 sample_mflix.movies
集合运行示例查询,将 genre
和 year
字段分组到桶中。为了优化性能:
如果您只需要
facet
元数据,请使用$searchMeta
阶段。如果要同时检索查询结果和
facet
元数据,请使用$search
阶段。
AtlasGoClusters在Atlas中,Go项目的 页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含所需项目的组织。
如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。
如果尚未出现,请单击侧边栏中的 Clusters(集群)。
会显示集群页面。
针对索引字段运行 Atlas Search 查询。
要运行查询,请将以下查询复制并粘贴到 Query Editor,然后单击 Search。
以下查询搜索在 1921 年 11 月 11 日前后上映的电影。它要求电影相对于 origin
的 pivot
距离为大约三个月。它要求提供 genres
和 year
字段上的元数据。该查询要求计数:
genres
字符串数组字段中每种类型的电影数量1910 年至 1939 年(含)期间的电影数量
[ { "$searchMeta": { "index": "facet-tutorial", "facet": { "operator": { "near": { "path": "released", "origin": ISODate("1921-11-01T00:00:00.000+00:00"), "pivot": 7776000000 } }, "facets": { "genresFacet": { "type": "string", "path": "genres" }, "yearFacet" : { "type" : "number", "path" : "year", "boundaries" : [1910,1920,1930,1940] } } } } } ]
展开查询结果。
Search Tester 可能不会显示结果中字段的所有值。要查看结果中字段的所有值,请展开字段。
Atlas Search 在页面中显示以下结果:
count: Object lowerBound: 20878 facet: Object genresFacet: Object buckets: Array (10) 0: Object _id: "Drama" count: 12149 1: Object _id: "Comedy" count: 6436 2: Object _id: "Romance" count: 3274 3: Object _id: "Crime" count: 2429 4: Object _id: "Thriller" count: 2400 5: Object _id: "Action" count: 2349 6: Object _id: "Adventure" count: 1876 7: Object _id: "Documentary" count: 1755 8: Object _id: "Horror" count: 1432 9: Object _id: "Biography" count: 1244 yearFacet: Object buckets: Array (3) 0: Object _id: 1910 count: 14 1: Object _id: 1920 count: 47 2: Object _id: 1930 count: 238
通过 mongosh
连接到您的集群。
在终端窗口中打开mongosh
并连接到集群。 有关连接的详细说明,请参阅通过mongosh
连接。
使用 sample_mflix
数据库。
在 mongosh
提示符下运行以下命令:
use sample_mflix
运行 Atlas Search 分面查询,将流派和年份字段分组到存储桶中。
示例查询使用以下内容来查询集合:
near 搜索 1921 年 11 月 11 日前后上映的电影,要求电影相对于
origin
的pivot
距离为大约三个月facets
要请求genres
string 数组字段中每种类型的电影数,以及 1910 到 1939 年中的电影数,包括
您可以使用$searchMeta
或使用$search
与 SEARCH_META
聚合变量来运行此查询。
在 MongoDB Compass 中连接到您的集群。
打开 MongoDB Compass 并连接到您的集群。有关连接的详细说明,请参阅通过 Compass 连接。
运行 Atlas Search 分面查询,将流派和年份字段分组到存储桶中。
该查询使用以下 searchMeta
操作符子句:
near 搜索 1921 年 11 月 11 日前后上映的电影,要求电影相对于
origin
的pivot
距离为大约三个月facets
要请求genres
string 数组字段中每种类型的电影数,以及 1910 到 1939 年中的电影数,包括
若要在 MongoDB Compass 中运行此查询:
单击 Aggregations 标签页。
单击 Select...,然后从下拉菜单中选择阶段并为该阶段添加查询,以配置以下每个管道阶段。单击 Add Stage 以添加其他阶段。
您可以使用
$searchMeta
或使用$search
与SEARCH_META
聚合变量来运行此查询。
如果启用了 Auto Preview,MongoDB Compass 将在 $set
管道阶段旁边显示以下文档:
count: Object lowerBound: 20878 facet: Object genresFacet: Object buckets: Array (10) 0: Object _id: "Drama" count: 12149 1: Object _id: "Comedy" count: 6436 2: Object _id: "Romance" count: 3274 3: Object _id: "Crime" count: 2429 4: Object _id: "Thriller" count: 2400 5: Object _id: "Action" count: 2349 6: Object _id: "Adventure" count: 1876 7: Object _id: "Documentary" count: 1755 8: Object _id: "Horror" count: 1432 9: Object _id: "Biography" count: 1244 yearFacet: Object buckets: Array (3) 0: Object _id: 1910 count: 14 1: Object _id: 1920 count: 47 2: Object _id: 1930 count: 238
在 Program.cs
文件中创建查询。
将
Program.cs
文件的内容替换为以下代码。示例查询使用以下内容来查询集合:
near 搜索 1921 年 11 月 11 日前后上映的电影,要求电影相对于
origin
的pivot
距离为大约三个月facets
要请求genres
string 数组字段中每种类型的电影数,以及 1910 到 1939 年中的电影数,包括
您可以使用
$searchMeta
或使用$search
与SEARCH_META
聚合变量来运行此查询。在运行示例之前,请将
<connection-string>
替换为 Atlas 连接字符串。确保您的连接字符串包含数据库用户的档案。要了解详情,请参阅通过驱动程序连接。
编译并运行 Program.cs
文件。
dotnet run facet-query-example.csproj
{ "meta" : { "count" : { "lowerBound" : 20878 }, "facet" : { "genresFacet" : { "buckets" : [ { "_id" : "Drama", "count" : 12149 }, { "_id" : "Comedy", "count" : 6436 }, { "_id" : "Romance", "count" : 3274 }, { "_id" : "Crime", "count" : 2429 }, { "_id" : "Thriller", "count" : 2400 }, { "_id" : "Action", "count" : 2349 }, { "_id" : "Adventure", "count" : 1876 }, { "_id" : "Documentary", "count" : 1755 }, { "_id" : "Horror", "count" : 1432 }, { "_id" : "Biography", "count" : 1244 } ] }, "yearFacet" : { "buckets" : [ { "_id" : 1910, "count" : 14 }, { "_id" : 1920, "count" : 47 }, { "_id" : 1930, "count" : 238 } ] } } } }
运行 Atlas Search 分面查询,将流派和年份字段分组到存储桶中。
创建一个名为
facet-query.go
的文件。将以下代码复制并粘贴到
facet-query.go
文件。此代码示例将执行以下任务:
导入
mongodb
包和依赖项。建立与您的 Atlas 集群的连接。
使用以下 searchMeta 子句来查询集合:
near 搜索 1921 年 11 月 11 日前后上映的电影,要求电影相对于
origin
的pivot
距离为大约三个月facets
要请求genres
string 数组字段中每种类型的电影数,以及 1910 到 1939 年中的电影数,包括
遍历游标以打印与查询匹配的文档。
您可以使用
$searchMeta
或使用$search
与SEARCH_META
聚合变量来运行此查询。在运行示例之前,请将
<connection-string>
替换为 Atlas 连接字符串。确保您的连接字符串包含数据库用户的档案。要了解详情,请参阅通过驱动程序连接。运行以下命令来查询您的集合:
go run facet-query.go [ {count [ {lowerBound 20878} ]} {facet [ {genresFacet [ {buckets [ [{_id Drama} {count 12149}] [{_id Comedy} {count 6436}] [{_id Romance} {count 3274}] [{_id Crime} {count 2429}] [{_id Thriller} {count 2400}] [{_id Action} {count 2349}] [{_id Adventure} {count 1876}] [{_id Documentary} {count 1755}] [{_id Horror} {count 1432}] [{_id Biography} {count 1244}] ]} ]} {yearFacet [ {buckets [ [{_id 1910} {count 14}] [{_id 1920} {count 47}] [{_id 1930} {count 238}] ]} ]} ]} ]
运行 Atlas Search 分面查询,将流派和年份字段分组到存储桶中。
创建一个名为
FacetQuery.java
的文件。将以下代码复制并粘贴到
FacetQuery.java
文件。此代码示例将执行以下任务:
导入
mongodb
包和依赖项。建立与您的 Atlas 集群的连接。
使用以下 searchMeta 子句来查询集合:
near 搜索 1921 年 11 月 11 日前后上映的电影,要求电影相对于
origin
的pivot
距离为大约三个月facets
要请求genres
string 数组字段中每种类型的电影数,以及 1910 到 1939 年中的电影数,包括
遍历游标以打印与查询匹配的文档。
您可以使用
$searchMeta
或使用$search
与SEARCH_META
聚合变量来运行此查询。注意
要在 Maven 环境中运行示例代码,请在文件中的 import 语句上方添加以下内容。
package com.mongodb.drivers; 在运行示例之前,请将
<connection-string>
替换为 Atlas 连接字符串。确保您的连接字符串包含数据库用户的档案。要了解详情,请参阅通过驱动程序连接。编译并运行
FacetQuery.java
文件。javac FacetQuery.java java FacetQuery {meta: { count: { lowerBound: Long('20878') }, facet: { genresFacet: { buckets: [ { _id: 'Drama', count: Long('12149') }, { _id: 'Comedy', count: Long('6436') }, { _id: 'Romance', count: Long('3274') }, { _id: 'Crime', count: Long('2429') }, { _id: 'Thriller', count: Long('2400') }, { _id: 'Action', count: Long('2349') }, { _id: 'Adventure', count: Long('1876') }, { _id: 'Documentary', count: Long('1755') }, { _id: 'Horror', count: Long('1432') }, { _id: 'Biography', count: Long('1244') } ] }, yearFacet: { buckets: [ { _id: 1910, count: Long('14') }, { _id: 1920, count: Long('47') }, { _id: 1930, count: Long('238') } ] } } }}
运行 Atlas Search 分面查询,将流派和年份字段分组到存储桶中。
创建一个名为
FacetQuery.kt
的文件。将以下代码复制并粘贴到
FacetQuery.kt
文件。此代码示例将执行以下任务:
导入
mongodb
包和依赖项。建立与您的 Atlas 集群的连接。
使用以下内容查询集合:
near 搜索 1921 年 11 月 11 日前后上映的电影,要求电影相对于
origin
的pivot
距离为大约三个月facets
要请求genres
string 数组字段中每种类型的电影数,以及 1910 到 1939 年中的电影数,包括
打印与
AggregateFlow
实例中的查询相匹配的文档。
您可以使用
$searchMeta
或使用$search
与SEARCH_META
聚合变量来运行此查询。在运行示例之前,请将
<connection-string>
替换为 Atlas 连接字符串。确保您的连接字符串包含数据库用户的档案。要了解详情,请参阅通过驱动程序连接。运行
FacetQuery.kt
文件。当您在 IDE 中运行
FacetQuery.kt
程序时,它会打印以下结果:Document{{ count=Document{{lowerBound=20878}}, facet=Document{{ genresFacet=Document{{ buckets=[ Document{{_id=Drama, count=12149}}, Document{{_id=Comedy, count=6436}}, Document{{_id=Romance, count=3274}}, Document{{_id=Crime, count=2429}}, Document{{_id=Thriller, count=2400}}, Document{{_id=Action, count=2349}}, Document{{_id=Adventure, count=1876}}, Document{{_id=Documentary, count=1755}}, Document{{_id=Horror, count=1432}}, Document{{_id=Biography, count=1244}} ] }}, yearFacet=Document{{ buckets=[ Document{{_id=1910, count=14}}, Document{{_id=1920, count=47}}, Document{{_id=1930, count=238}} ] }} }} }}
运行 Atlas Search 分面查询,将流派和年份字段分组到存储桶中。
创建一个名为
facet-query.js
的文件。将以下代码复制并粘贴到
facet-query.js
文件。此代码示例将执行以下任务:
导入
mongodb
,即 MongoDB 的 Node.js 驱动程序。创建一个
MongoClient
类实例,以建立与 Atlas 集群的连接。使用以下 searchMeta 子句来查询集合:
near 搜索 1921 年 11 月 11 日前后上映的电影,要求电影相对于
origin
的pivot
距离为大约三个月facets
要请求genres
string 数组字段中每种类型的电影数,以及 1910 到 1939 年中的电影数,包括
遍历游标以打印与查询匹配的文档。
您可以使用
$searchMeta
或使用$search
与SEARCH_META
聚合变量来运行此查询。在运行示例之前,请将
<connection-string>
替换为 Atlas 连接字符串。确保您的连接字符串包含数据库用户的档案。要了解详情,请参阅通过驱动程序连接。运行以下命令来查询您的集合:
node facet-query.js '{ "meta":{ "count":{"lowerBound":20878}, "facet":{ "genresFacet":{ "buckets":[ {"_id":"Drama","count":12149}, {"_id":"Comedy","count":6436}, {"_id":"Romance","count":3274}, {"_id":"Crime","count":2429}, {"_id":"Thriller","count":2400}, {"_id":"Action","count":2349}, {"_id":"Adventure","count":1876}, {"_id":"Documentary","count":1755}, {"_id":"Horror","count":1432}, {"_id":"Biography","count":1244} ] }, "yearFacet":{ "buckets":[ {"_id":1910,"count":14}, {"_id":1920,"count":47}, {"_id":1930,"count":238} ] } } } }'
运行 Atlas Search 分面查询,将流派和年份字段分组到存储桶中。
创建一个名为
facet-query.py
的文件。touch facet-query.py 将以下代码复制并粘贴到
facet-query.py
文件。以下代码示例:
导入
pymongo
、MongoDB 的 Python 驱动程序和dns
模块,这是使用 DNS 种子列表连接字符串将pymongo
连接到Atlas
所必需的。创建一个
MongoClient
类实例,以建立与 Atlas 集群的连接。使用以下内容查询集合:
near 搜索 1921 年 11 月 11 日前后上映的电影,要求电影相对于
origin
的pivot
距离为大约三个月facets
要请求genres
string 数组字段中每种类型的电影数,以及 1910 到 1939 年中的电影数,包括
遍历游标以打印与查询匹配的文档。
您可以使用
$searchMeta
或使用$search
与SEARCH_META
聚合变量来运行此查询。在运行示例之前,请将
<connection-string>
替换为 Atlas 连接字符串。确保您的连接字符串包含数据库用户的档案。要了解详情,请参阅通过驱动程序连接。运行以下命令来查询您的集合:
python facet-query.py { 'meta': { 'count': {'lowerBound': 20878}, 'facet': { 'genresFacet': { 'buckets': [ {'_id': 'Drama', 'count': 12149}, {'_id': 'Comedy', 'count': 6436}, {'_id': 'Romance', 'count': 3274}, {'_id': 'Crime', 'count': 2429}, {'_id': 'Thriller', 'count': 2400}, {'_id': 'Action', 'count': 2349}, {'_id': 'Adventure', 'count': 1876}, {'_id': 'Documentary', 'count': 1755}, {'_id': 'Horror', 'count': 1432}, {'_id': 'Biography', 'count': 1244} ] }, 'yearFacet': { 'buckets': [ {'_id': 1910, 'count': 14}, {'_id': 1920, 'count': 47}, {'_id': 1930, 'count': 238} ] } } } }
结果显示两种类型的分面搜索的元数据结果。genresFacet
文档显示每类电影的数量,yearFacet
文档显示边界内的电影数量:
1910
,包括1910
存储桶的下限1920
,1910
存储桶不包括上边界,1920
存储桶包括下边界1930
,1920
存储桶不包括上边界,1930
存储桶包括下边界
继续学习
您可以通过我们的课程或视频了解有关 Atlas Search 中的分面的更多信息。
通过课程学习
要了解有关在 Atlas Search 中使用分面的更多信息,请学习 MongoDB University 中 MongoDB 入门课程第 9 单元。该单元时长 1.5 小时,包括 Atlas Search 概述以及有关创建 Atlas Search 索引、使用复合操作符运行 $search
查询以及使用 facet
对结果进行分组的课程。
通过视频学习
观看此视频,了解如何在查询中创建和使用数字和字符串分面,对结果进行分组并检索分组中的结果计数。
时长:11 分钟