您可以使用MongoDB Search numberFacet 类型,使用指定的 representation 进行分面来对数值索引。您可以索引BSON 类型int32、int64 和 double 的编号。
MongoDB Search 仅支持对索引为 numberFacet 类型的字段进行分面(Facet)操作符查询。要对同一字段也执行普通搜索,您也必须按类型 number 对该字段索引。
要对嵌入式文档中的字符串字段进行分面(Facet),您必须将父字段作为文档类型索引。当您对嵌入式文档内的字符串字段进行分面(Facet), MongoDB Search 仅返回匹配父文档数量的分面(Facet)计数。
MongoDB Search 不会为分面动态索引number 值。您必须使用静态映射为分面的 number 值索引。您可以使用Atlas用户界面中的可视化编辑器或JSON编辑器将 number 字段索引为 number 类型。
numberFacet 类型限制
适用以下限制:
您无法为
decimal128进行分面(Facet)索引。您无法为数组或数组中包含的文档中的数值进行分面(Facet)索引。
您无法对作为
embeddedDocuments字段一部分进行索引的数字字段进行分面(Facet)。
为 numberFacet 类型定义索引
要定义 numberFacet 类型的索引,请在 Atlas UI 中选择您的首选配置方法,然后选择数据库和集合。
单击 Refine Your Index 配置索引。
在 Field Mappings 部分中,单击 Add Field Mapping 打开 Add Field Mapping 窗口。
单击 Customized Configuration(连接)。
从 Field Name 下拉菜单中选择要索引的字段。
注意
您无法对字段名称开头包含美元 (
$) 符号的字段创建索引。单击 Data Type(添加数据)下拉列表并选择 NumberFacet(插入文档)。
配置
numberFacet类型的字段属性。要了解详情,请参阅字段属性 。单击 Add(连接)。
以下是 numberFacet 类型的 JSON 语法。将默认索引定义替换为以下内容。要了解有关字段的更多信息,请参阅字段属性。
{ "mappings": { "dynamic": true|false, "fields": { "<field-name>": { "type": "numberFacet", "representation": "int64|double", "indexIntegers": true|false, "indexDoubles": true|false } } } }
配置 numberFacet 字段属性
MongoDB Search numberFacet 类型采用以下参数:
选项 | 类型 | 必要性 | 说明 | 默认 |
|---|---|---|---|---|
| 字符串 | 必需 | 字段的类型。值必须是 | |
| 字符串 | Optional |
| |
| 布尔 | Optional | 指示是对 |
|
| 布尔 | Optional | 指示是否对 |
|
numberFacet 类型尝试示例
The following index definition example uses the sample_mflix.movies collection. If you have the sample data already loaded on your cluster, you can use the Visual Editor or JSON Editor in the Atlas UI to configure the index. After you select your preferred configuration method, select the database and collection, and refine your index to add field mappings.
以下示例索引定义将 year字段索引为MongoDB Search numberFacet 类型,以支持使用MongoDB Search分面(Facet)面对该字段进行查询。
在 Add Field Mapping 窗口中,从 Field Name 下拉列表中选择 year。
单击 Data Type(添加数据)下拉列表并选择 NumberFacet(插入文档)。
接受NumberFacet Properties的默认值。
单击 Add(连接)。
将默认索引定义替换为以下索引定义。
{ "mappings": { "dynamic": false, "fields": { "year": { "type": "numberFacet" } } } }
以下示例索引定义将 year 字段索引为 numberFacet 和 number 类型,以返回以下类型的查询结果:
使用MongoDB Search
facet(MongoDB Search Operator)进行查询的元数据结果。使用MongoDB Search 操作符(例如 near、equals 和范围)的查询的搜索结果。
在 Add Field Mapping 窗口中,从 Field Name 下拉列表中选择 year。
单击 Data Type(添加数据)下拉列表并选择 NumberFacet(插入文档)。
接受NumberFacet Properties的默认值。
单击 Add(连接)。
重复步骤 1,并从 Data Type 下拉列表中选择 Number。
接受Number Properties的默认值。
单击 Add(连接)。
将默认索引定义替换为以下索引定义。
{ "mappings": { "dynamic": false, "fields": { "year": [ { "type": "numberFacet" }, { "type": "number" } ] } } }
了解详情
要了解有关 facet 收集器的更多信息并查看示例查询,请参阅示例。
要学习;了解如何使用分面(Facet)定义创建索引并使用 facet 收集器查询该索引,请参阅如何在MongoDB Search 中使用分面教程。