Docs 菜单
Docs 主页
/
Atlas
/ / / /

空格分析器

只要找到空格字符, 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 EditorJSON editor 中创建索引。

  1. 单击 Refine Your Index 配置索引。

  2. Index Configurations 部分中,将 Dynamic Mapping 切换为 off

  3. Field Mappings 部分中,单击 Add Field 打开 Add Field Mapping 窗口。

  4. Field Name 下拉列表中选择 title

  5. 单击 Customized Configuration(连接)。

  6. 单击 Data Type 下拉列表并选择 String(如果尚未选择)。

  7. 展开 String Properties 并进行以下更改:

    索引分析器

    从下拉列表中选择 lucene.whitespace

    Search Analyzer

    从下拉列表中选择 lucene.whitespace

    索引选项

    使用默认 offsets

    Store

    使用默认 true

    忽略以上内容

    保留默认设置。

    规范

    使用默认 include

  8. 单击 Add(连接)。

  9. 单击 Save Changes(连接)。

  10. 单击 Create Search Index(连接)。

  1. 将默认索引定义替换为以下索引定义。

    {
    "mappings": {
    "fields": {
    "title": {
    "type": "string",
    "analyzer": "lucene.whitespace",
    "searchAnalyzer": "lucene.whitespace"
    }
    }
    }
    }
  2. 单击 Next(连接)。

  3. 单击 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 使用空格分析器创建的词元(可搜索术语),并与使用简单分析器关键字分析器为结果中的文档创建的词元进行对比:

标题
空白分析器令牌
简单分析器词元
关键字分析器词元

Lion's Den

Lion's, Den

lion, s , den

Lion's Den

The Lion's Mouth Opens

The, Lion's , Mouth , Opens

the, lion , s , mouth , opens

The Lion's Mouth Opens

使用whitespace分析器的索引区分大小写。 因此,Atlas Search 能够将查询词Lion's Lion'swhitespace 分析器创建的词元 进行匹配。

后退

simple