Docs 菜单
Docs 主页
/ /

字符筛选器

字符筛选器一次检查一个字符的文本并执行筛选操作。 字符筛选器需要类型字段,有些还需要额外的选项。

语法
"charFilters": [
{
"type": "<filter-type>",
"<additional-option>": <value>
}
]

MongoDB Search 支持以下类型的字符过滤:

  • htmlStrip

  • icuNormalize

  • 映射

  • 波斯语

以下示例索引定义和查询使用名为 minutes 示例集合。要跟随这些示例,请在集群上加载minutes Create a Search Index集合,并按照创建MongoDB搜索索引教程中的步骤导航到Atlas用户界面中的 页面。然后,选择minutes 集合作为数据源,并按照示例过程从Atlas用户界面或使用mongosh 创建索引。


➤ 使用选择语言下拉菜单设立运行此页面上示例的方法。


htmlStrip字符筛选器去除 HTML 结构。

htmlStrip 字符过滤具有以下属性:

名称
类型
必需?
说明

type

字符串

标识此字符筛选器类型的人类可读标签。 值必须是htmlStrip

ignoredTags

字符串数组

包含要从过滤中排除的 HTML 标记的列表。

以下索引定义示例使用名为htmlStrippingAnalyzer的自定义分析器为分钟collection中的text.en_US字段编制索引。自定义分析器指定以下内容:

  1. Custom Analyzers 部分中,单击 Add Custom Analyzer

  2. 选择 Create Your Own 单选按钮并单击 Next

  3. Analyzer Name 字段中输入 htmlStrippingAnalyzer

  4. 展开 Character Filters,然后单击 Add character filter

  5. 从下拉菜单中选择 htmlStrip,然后在 ignoredTags 字段中输入 a

  6. 单击 Add character filter(连接)。

  7. 如果Tokenizer已折叠,请将其展开,然后从下拉列表中选择standard

  8. 单击 Add,将自定义分析器添加到索引。

  9. Field Mappings 部分中,单击 Add Field Mapping 可对 text.en_US嵌套字段应用自定义分析器。

  10. 选择 text.en_US(嵌套在 Field Name 下拉菜单中),然后从 Data Type 下拉菜单中选择 String(字符串)。

  11. 在数据类型的属性部分中,从 Index AnalyzerSearch Analyzer 下拉菜单中选择 htmlStrippingAnalyzer

  12. 单击 Add,然后单击 Save Changes

将默认索引定义替换为以下内容:

1 {
2 "mappings": {
3 "fields": {
4 "text": {
5 "type": "document",
6 "dynamic": true,
7 "fields": {
8 "en_US": {
9 "analyzer": "htmlStrippingAnalyzer",
10 "type": "string"
11 }
12 }
13 }
14 }
15 },
16 "analyzers": [{
17 "name": "htmlStrippingAnalyzer",
18 "charFilters": [{
19 "type": "htmlStrip",
20 "ignoredTags": ["a"]
21 }],
22 "tokenizer": {
23 "type": "standard"
24 },
25 "tokenFilters": []
26 }]
27 }
1db.minutes.createSearchIndex(
2 "default",
3 {
4 "mappings": {
5 "fields": {
6 "text": {
7 "type": "document",
8 "dynamic": true,
9 "fields": {
10 "en_US": {
11 "analyzer": "htmlStrippingAnalyzer",
12 "type": "string"
13 }
14 }
15 }
16 }
17 },
18 "analyzers": [
19 {
20 "name": "htmlStrippingAnalyzer",
21 "charFilters": [
22 {
23 "type": "htmlStrip",
24 "ignoredTags": ["a"]
25 }
26 ],
27 "tokenizer": {
28 "type": "standard"
29 },
30 "tokenFilters": []
31 }
32 ]
33 }
34)

headtext.en_USminutes以下查询在collection的字段中查找字符串 的出现次数。

  1. 单击索引的 Query 按钮。

  2. 单击 Edit Query 编辑查询。

  3. 单击查询栏上的 并选择数据库和集合。

  4. 将默认查询替换为以下内容,然后单击 Find

    {
    "$search": {
    "text": {
    "query": "head",
    "path": "text.en_US"
    }
    }
    }
    SCORE: 0.32283568382263184 _id: “2
    message: "do not forget to SIGN-IN. See ① for details."
    page_updated_by: Object
    last_name: "OHRBACH"
    first_name: "Noël"
    email: "ohrbach@example.com"
    phone: "(123) 456 0987"
    text: Object
    en_US: "The head of the sales department spoke first."
    fa_IR: "ابتدا رئیس بخش فروش صحبت کرد"
    sv_FI: "Först talade chefen för försäljningsavdelningen"
    SCORE: 0.3076632022857666 _id: “3
    message: "try to sign-in"
    page_updated_by: Object
    last_name: "LEWINSKY"
    first_name: "Brièle"
    email: "lewinsky@example.com"
    phone: "(123).456.9870"
    text: Object
    en_US: "<body>We'll head out to the conference room by noon.</body>"
1db.minutes.aggregate([
2 {
3 "$search": {
4 "text": {
5 "query": "head",
6 "path": "text.en_US"
7 }
8 }
9 },
10 {
11 "$project": {
12 "_id": 1,
13 "text.en_US": 1
14 }
15 }
16])
[
{
_id: 2,
text: { en_US: "The head of the sales department spoke first." }
},
{
_id: 3,
text: {
en_US: "<body>We'll head out to the conference room by noon.</body>"
}
}
]

MongoDB Search 不会返回包含 _id: 1 的文档,因为字符串 head 是 HTML标签<head> 的一部分。 带有 _id: 3 的文档包含 HTML 标记,但字符串 head 在其他位置,因此该文档是匹配项。 下表显示了MongoDB Search 使用 htmlStrippingAnalyzer分钟集合中文档 _id: 1_id: 2_id: 3 中的 text.en_US字段值生成的词元。

文档 ID
输出词元

_id: 1

This, page , deals , with , department , meetings

_id: 2

The, head, of, the, sales, department, spoke, first

_id: 3

We'll, head, out, to, the, conference, room, by, noon

icuNormalize 字符过滤器使用 ICU Normalizer 对文本进行规范化。它基于 Lucene 的 ICUN ormalizer2 CharFilter。

icuNormalize 字符过滤器具有以下属性:

名称
类型
必需?
说明

type

字符串

标识此字符筛选器类型的人类可读标签。 值必须是icuNormalize

以下索引定义示例使用名为normalizingAnalyzer的自定义分析器为分钟collection中的message字段编制索引。自定义分析器指定以下内容:

  • 使用icuNormalize字符筛选器对message字段值中的文本进行规范化。

  • 使用空格分词器根据单词之间出现的空格对字段中的单词进行分词。

  1. Custom Analyzers 部分中,单击 Add Custom Analyzer

  2. 选择 Create Your Own 单选按钮并单击 Next

  3. Analyzer Name 字段中输入 normalizingAnalyzer

  4. 展开 Character Filters,然后单击 Add character filter

  5. 从下拉菜单中选择icuNormalize ,然后单击Add character filter

  6. 如果Tokenizer已折叠,请将其展开,然后从下拉列表中选择whitespace

  7. 单击 Add,将自定义分析器添加到索引。

  8. Field Mappings 部分中,单击 Add Field Mapping 以在message(消息)字段上应用自定义分析器。

  9. Field Name 下拉列表中选择 message(消息),并从 Data Type 下拉列表中选择String(字符串)。

  10. 在数据类型的属性部分,从Index AnalyzerSearch Analyzer下拉列表中选择normalizingAnalyzer

  11. 单击 Add,然后单击 Save Changes

将默认索引定义替换为以下内容:

1{
2 "mappings": {
3 "fields": {
4 "message": {
5 "type": "string",
6 "analyzer": "normalizingAnalyzer"
7 }
8 }
9 },
10 "analyzers": [
11 {
12 "name": "normalizingAnalyzer",
13 "charFilters": [
14 {
15 "type": "icuNormalize"
16 }
17 ],
18 "tokenizer": {
19 "type": "whitespace"
20 },
21 "tokenFilters": []
22 }
23 ]
24}
db.minutes.createSearchIndex("default", {
"mappings": {
"fields": {
"message": {
"type": "string",
"analyzer": "normalizingAnalyzer"
}
}
},
"analyzers": [
{
"name": "normalizingAnalyzer",
"charFilters": [
{
"type": "icuNormalize"
}
],
"tokenizer": {
"type": "whitespace"
},
"tokenFilters": []
}
]
})

nomessageminutes以下查询在collection的字段中搜索字符串 (数字)的出现次数。

  1. 单击索引的 Query 按钮。

  2. 单击 Edit Query 编辑查询。

  3. 单击查询栏上的 并选择数据库和集合。

  4. 将默认查询替换为以下内容,然后单击 Find

    {
    "$search": {
    "text": {
    "query": "no",
    "path": "message"
    }
    }
    }
    SCORE: 0.4923309087753296 _id: “4
    message: "write down your signature or phone №"
    page_updated_by: Object
    last_name: "LEVINSKI"
    first_name: "François"
    email: "levinski@example.com"
    phone: "123-456-8907"
    text: Object
    en_US: "<body>This page has been updated with the items on the agenda.</body>"
    es_MX: "La página ha sido actualizada con los puntos de la agenda."
    pl_PL: "Strona została zaktualizowana o punkty porządku obrad."
1db.minutes.aggregate([
2 {
3 "$search": {
4 "text": {
5 "query": "no",
6 "path": "message"
7 }
8 }
9 },
10 {
11 "$project": {
12 "_id": 1,
13 "message": 1,
14 "title": 1
15 }
16 }
17])
[
{
_id: 4,
title: 'The daily huddle on tHe StandUpApp2',
message: 'write down your signature or phone №'
}
]

MongoDB Search 将包含 _id: 4 的文档与查询术语no 进行匹配,因为它使用 icuNormalize 字符过滤对该字段中的数字符号 进行了规范化,并为单词“number”的印刷缩写创建了词元 no。 ”。MongoDB Search 使用 normalizingAnalyzer 为文档_id: 4 中的 message字段值生成以下词元:

文档 ID
输出词元

_id: 4

write, down, your, signature, or, phone, no

mapping 字符过滤将用户指定的规范化映射应用于字符。它基于 Lucene 的MappingCharFilter。

mapping 字符过滤具有以下属性:

名称
类型
必需?
说明

type

字符串

标识此字符过滤器类型的人类可读标签。

值必须是 mapping

mappings

对象

包含以逗号分隔的映射列表的对象。 映射表示应用一个字符或一组字符替换另一个字符或一组字符,格式为<original> : <replacement>

以下索引定义示例使用名为mappingAnalyzer的自定义分析器为分钟collection中的page_updated_by.phone字段编制索引。自定义分析器指定以下内容:

  • 使用mapping字符筛选器删除电话字段中连字符 ( - )、点 ( . )、左括号 ( ( )、右括号 ( ) ) 和空格字符的实例。

  • 使用关键字分词器将整个输入分词为单个词元。

  1. Custom Analyzers 部分中,单击 Add Custom Analyzer

  2. 选择 Create Your Own 单选按钮并单击 Next

  3. Analyzer Name 字段中输入 mappingAnalyzer

  4. 展开 Character Filters,然后单击 Add character filter

  5. 从下拉菜单中选择 mapping,然后点击 Add mapping

  6. Original 字段中输入以下字符,一次一个,并将相应的 Replacement 字段留空。

    原始
    更换

    -

    .

    (

    )

    {SPACE}

  7. 单击 Add character filter(连接)。

  8. 如果Tokenizer已折叠,请将其展开,然后从下拉列表中选择keyword

  9. 单击 Add,将自定义分析器添加到索引。

  10. Field Mappings 部分中,单击 Add Field Mapping 以对 page_updated_by.phone嵌套)字段应用自定义分析器。

  11. Field Name 下拉列表中选择 page_updated_by.phone嵌套),并从Data Type 下拉列表中选择 String

  12. 在数据类型的属性部分中,从 Index AnalyzerSearch Analyzer 下拉菜单中选择 mappingAnalyzer

  13. 单击 Add,然后单击 Save Changes

将默认索引定义替换为以下内容:

1{
2 "mappings": {
3 "fields": {
4 "page_updated_by": {
5 "fields": {
6 "phone": {
7 "analyzer": "mappingAnalyzer",
8 "type": "string"
9 }
10 },
11 "type": "document"
12 }
13 }
14 },
15 "analyzers": [
16 {
17 "name": "mappingAnalyzer",
18 "charFilters": [
19 {
20 "mappings": {
21 "-": "",
22 ".": "",
23 "(": "",
24 ")": "",
25 " ": ""
26 },
27 "type": "mapping"
28 }
29 ],
30 "tokenizer": {
31 "type": "keyword"
32 }
33 }
34 ]
35}
1db.minutes.createSearchIndex(
2 "default",
3 {
4 "mappings": {
5 "fields": {
6 "page_updated_by": {
7 "fields": {
8 "phone": {
9 "analyzer": "mappingAnalyzer",
10 "type": "string"
11 }
12 },
13 "type": "document"
14 }
15 }
16 },
17 "analyzers": [
18 {
19 "name": "mappingAnalyzer",
20 "charFilters": [
21 {
22 "mappings": {
23 "-": "",
24 ".": "",
25 "(": "",
26 ")": "",
27 " ": ""
28 },
29 "type": "mapping"
30 }
31 ],
32 "tokenizer": {
33 "type": "keyword"
34 }
35 }
36 ]
37 }
38)

以下查询在page_updated_by.phone字段中搜索字符串1234567890

  1. 单击索引的 Query 按钮。

  2. 单击 Edit Query 编辑查询。

  3. 单击查询栏上的 并选择数据库和集合。

  4. 将默认查询替换为以下内容,然后单击 Find

    {
    "$search": {
    "text": {
    "query": "1234567890",
    "path": "page_updated_by.phone"
    }
    }
    }
    SCORE: 0.5472603440284729 _id: “1
    message: "try to siGn-In"
    page_updated_by: Object
    last_name: "AUERBACH"
    first_name: "Siân"
    email: "auerbach@example.com"
    phone: "(123)-456-7890"
    text: Object
    en_US: "<head> This page deals with department meetings.</head>"
    sv_FI: "Den här sidan behandlar avdelningsmöten"
    fr_CA: "Cette page traite des réunions de département"
1db.minutes.aggregate([
2 {
3 "$search": {
4 "text": {
5 "query": "1234567890",
6 "path": "page_updated_by.phone"
7 }
8 }
9 },
10 {
11 "$project": {
12 "_id": 1,
13 "page_updated_by.phone": 1,
14 "page_updated_by.last_name": 1
15 }
16 }
17])
[
{
_id: 1,
page_updated_by: { last_name: 'AUERBACH', phone: '(123)-456-7890' }
}
]

MongoDB搜索结果包含一个文档,其中 phone 字符串中的数字与查询字符串匹配。 即使查询不包含电话区号两边的括号以及数字之间的连字符, MongoDB搜索也会将文档与查询字符串进行匹配,因为MongoDB搜索使用 mapping 字符过滤删除了这些字符,并为以下内容创建了单个词元:字段值。 具体来说, MongoDB Search 为具有 _id: 1 的文档中的 phone字段生成了以下词元:

文档 ID
输出词元

_id: 1

1234567890

对于搜索 (123)-456-7890123-456-7890123.456.7890 等, MongoDB搜索还会将文档与 _id: 1 进行匹配,因为对于如何索引字符串字段字段, MongoDB搜索还使用搜索查询索引(或如果已指定,则使用 searchAnalyzer)。下表显示了MongoDB Search 通过删除查询中的连字符 (-)、点 (.)、左括号 (()、右括号 ()) 和空格字符的实例来创建的词元术语:

查询术语
输出词元

(123)-456-7890

1234567890

123-456-7890

1234567890

123.456.7890

1234567890

persian 字符过滤器会用空格字符替换零宽度非连接符的实例。这个角色过滤器基于 Lucene 的 PersianCharFilter。

persian 字符过滤器具有以下属性:

名称
类型
必需?
说明

type

字符串

标识此字符筛选器类型的人类可读标签。 值必须是persian

以下索引定义示例使用名为persianCharacterIndex的自定义分析器为分钟collection中的text.fa_IR字段编制索引。自定义分析器指定以下内容:

  • 应用persian字符筛选器,将字段值中的非打印字符替换为空格字符。

  • 使用空格分词器根据词之间出现的空格创建标记。

  1. Custom Analyzers 部分中,单击 Add Custom Analyzer

  2. 选择 Create Your Own 单选按钮并单击 Next

  3. Analyzer Name 字段中输入 persianCharacterIndex

  4. 展开 Character Filters,然后单击 Add character filter

  5. 从下拉菜单中选择persian ,然后单击Add character filter

  6. 如果Tokenizer已折叠,请将其展开,然后从下拉列表中选择whitespace

  7. 单击 Add,将自定义分析器添加到索引。

  8. Field Mappings 部分中,单击 Add Field Mapping,将自定义分析器应用于 text.fa_IR嵌套)字段。

  9. 从以下内容选择 text.fa_IR嵌套Field Name 下拉列表和 Data Type 下拉列表的 String中。

  10. 在数据类型的属性部分,从Index AnalyzerSearch Analyzer下拉列表中选择persianCharacterIndex

  11. 单击 Add,然后单击 Save Changes

将默认索引定义替换为以下内容:

1{
2 "analyzer": "lucene.standard",
3 "mappings": {
4 "fields": {
5 "text": {
6 "dynamic": true,
7 "fields": {
8 "fa_IR": {
9 "analyzer": "persianCharacterIndex",
10 "type": "string"
11 }
12 },
13 "type": "document"
14 }
15 }
16 },
17 "analyzers": [
18 {
19 "name": "persianCharacterIndex",
20 "charFilters": [
21 {
22 "type": "persian"
23 }
24 ],
25 "tokenizer": {
26 "type": "whitespace"
27 }
28 }
29 ]
30}
db.minutes.createSearchIndex("default", {
"analyzer": "lucene.standard",
"mappings": {
"fields": {
"text": {
"dynamic": true,
"fields": {
"fa_IR": {
"analyzer": "persianCharacterIndex",
"type": "string"
}
},
"type": "document"
}
}
},
"analyzers": [
{
"name": "persianCharacterIndex",
"charFilters": [
{
"type": "persian"
}
],
"tokenizer": {
"type": "whitespace"
}
}
]
})

以下查询在text.fa_IR字段中搜索صحبت一词。

  1. 单击索引的 Query 按钮。

  2. 单击 Edit Query 编辑查询。

  3. 单击查询栏上的 并选择数据库和集合。

  4. 将默认查询替换为以下内容,然后单击 Find

    {
    "$search": {
    "text": {
    "query": "صحبت",
    "path": "text.fa_IR"
    }
    }
    }
    SCORE: 0.13076457381248474 _id: “2
    message: "do not forget to SIGN-IN. See ① for details."
    page_updated_by: Object
    last_name: "OHRBACH"
    first_name: "Noël"
    email: "ohrbach@example.com"
    phone: "(123) 456 0987"
    text: Object
    en_US: "The head of the sales department spoke first."
    fa_IR: "ابتدا رئیس بخش فروش صحبت کرد"
    sv_FI: "Först talade chefen för försäljningsavdelningen"
1db.minutes.aggregate([
2 {
3 "$search": {
4 "text": {
5 "query": "صحبت",
6 "path": "text.fa_IR"
7 }
8 }
9 },
10 {
11 "$project": {
12 "_id": 1,
13 "text.fa_IR": 1,
14 "page_updated_by.last_name": 1
15 }
16 }
17])
[
{
_id: 2,
page_updated_by: { last_name: 'OHRBACH' },
text: { fa_IR: 'ابتدا رئیس بخش فروش صحبت کرد' }
}
]

MongoDB Search 返回包含查询术语的 _id: 2文档。MongoDB Search 将查询术语与文档进行匹配,方法是首先用空格字符替换零宽度非连接符的实例,然后根据单词之间出现的空格为字段值中的每个单词创建单独的词元。具体来说, MongoDB Search 为包含 _id: 2 的文档生成以下词元:

文档 ID
输出词元

_id: 2

ابتدا, رئیس , بخش , فروش , صحبت , کرد

要查看使用 mapping 字符过滤器的其他索引定义和查询,请参阅以下参考页面示例:

后退

自定义分析器

在此页面上