只要找到空格字符, whitespace
分析器就会将文本分割为可搜索术语(词元)。它将所有文本保留为原始字母大小写。
您可以在 Atlas UI Visual Editor 中创建或编辑索引时,查看 whitespace
分析器为内置示例文档和查询字符串创建的词元。如果您选择 Refine Your Index,Atlas UI 将在 Index Configurations 部分中显示一个标题为 View text analysis of your selected index configuration 的部分。如果您展开此部分,Atlas UI 将显示 whitespace
分析器为每个示例字符串生成的索引和搜索词元。
重要
Atlas Search 不会索引分析器令牌大小超过 32766 字节的字符串字段。如果使用关键字分析器,则不会对超过 32766 字节的字符串字段编制索引。
例子
以下示例索引定义使用 whitespace
分析器指定 sample_mflix.movies 集合中的 title
字段上的索引。要按照此示例进行操作,请在您的集群上加载示例数据,然后按照创建 Atlas Search 索引教程中的步骤导航到 Atlas UI中的 Create a Search Index 页面。然后,选择 minutes
集合作为您的数据源,并按照示例过程在 Visual Editor 或 JSON editor 中创建索引。
单击 Refine Your Index 配置索引。
在 Index Configurations 部分中,将 Dynamic Mapping 切换为 off。
在 Field Mappings 部分中,单击 Add Field 打开 Add Field Mapping 窗口。
从 Field Name 下拉列表中选择
title
。单击 Customized Configuration(连接)。
单击 Data Type 下拉列表并选择 String(如果尚未选择)。
展开 String Properties 并进行以下更改:
索引分析器
从下拉列表中选择
lucene.whitespace
。Search Analyzer
从下拉列表中选择
lucene.whitespace
。索引选项
使用默认
offsets
。Store
使用默认
true
。忽略以上内容
保留默认设置。
规范
使用默认
include
。单击 Add(连接)。
单击 Save Changes(连接)。
单击 Create Search Index(连接)。
将默认索引定义替换为以下索引定义。
{ "mappings": { "fields": { "title": { "type": "string", "analyzer": "lucene.whitespace", "searchAnalyzer": "lucene.whitespace" } } } } 单击 Next(连接)。
单击 Create Search Index(连接)。
以下查询在title
字段中搜索词语Lion's
。
db.movies.aggregate([ { "$search": { "text": { "query": "Lion's", "path": "title" } } }, { "$project": { "_id": 0, "title": 1 } } ])
[ { title: 'Lion's Den' }, { title: 'The Lion's Mouth Opens' } ]
Atlas Search 使用lucene.whitespace
分析器对title
字段中的文本执行以下操作,从而返回这些文档:
保留文本的原始字母大小写。
在找到空白字符的地方将文本分割为词元。
下表显示了 Atlas Search 使用空格分析器创建的词元(可搜索术语),并与使用简单分析器和关键字分析器为结果中的文档创建的词元进行对比:
标题 | 空白分析器令牌 | 简单分析器词元 | 关键字分析器词元 |
---|---|---|---|
|
|
|
|
|
|
|
|
使用whitespace
分析器的索引区分大小写。 因此,Atlas Search 能够将查询词Lion's
Lion's
与whitespace
分析器创建的词元 进行匹配。