您可以使用MongoDB Search stringFacet 类型为字符串字段索引以进行分面,这样您就可以对该字段运行分面(Facet)操作符查询。在为分面索引string 字段时, MongoDB Search 不应用分析器。
MongoDB Search 仅支持对索引为 stringFacet 类型的字段进行分面(Facet)操作符查询。要对同一字段也执行普通搜索,您也必须按类型 string 对该字段索引。
要对嵌入式文档中的字符串字段进行分面(Facet),您必须将父字段作为文档类型索引。当您对嵌入式文档内的字符串字段进行分面(Facet), MongoDB Search 仅返回匹配父文档数量的分面(Facet)计数。
MongoDB Search 不会为分面动态索引string 值。您必须使用静态映射为分面的 string 值索引。您可以使用Atlas用户界面中的可视化编辑器或JSON编辑器将 string 字段索引为 number 类型。
Define the Index for the stringFacet Type
要定义 stringFacet 类型的索引,请在 Atlas UI 中选择您的首选配置方法,然后选择数据库和集合。
Configure Properties for the stringFacet Type
MongoDB Search stringFacet 类型具有以下参数:
用户界面字段名称 | JSON Option | 类型 | 必要性 | 说明 |
|---|---|---|---|---|
Data Type |
| 字符串 | 必需 | Human-readable label that identifies this field type. Value must be |
Try an Example for the stringFacet Type
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.
stringFacet 类型示例
示例数据集 中 集合的以下索引定义将sample_mflix.moviesgenres 字段索引为stringFacet 以进行分面(Facet)。
多种类型示例
在 示例数据集 中,针对 sample_mflix.movies 集合的以下索引定义将 genres 字段索引为 stringFacet 和 string 类型,从而为您的查询返回以下类型的结果:
使用MongoDB Search
facet(MongoDB Search Operator)进行查询的元数据结果。
了解详情
要了解有关 facet 收集器的更多信息并查看示例查询,请参阅示例。
要学习;了解如何使用分面(Facet)定义创建索引并使用 facet 收集器查询该索引,请参阅如何在MongoDB Search 中使用分面教程。
对于使用 stringFacet 运行带分页结果的分面搜索,如要查看索引和查询示例,请参阅在分页结果中使用分面。