词元筛选器执行如下操作:
提取词干,这会将相关词语(例如“talking”、“talked”和“talks”)缩减为词根“talk”。
编辑,这会从公共文档中删除敏感信息。
词元过滤器需要一个类型字段,有些还支持额外的选项。
"tokenFilters": [ { "type": "<token-filter-type>", "<additional-option>": <value> } ]
词元过滤器类型
MongoDB Search 支持以下类型的词元过滤:
以下示例索引定义和查询使用名为 的minutes
示例集合。要跟随这些示例,请在集群上加载minutes
Create a Search Index集合,并按照创建MongoDB搜索索引教程中的步骤导航到Atlas用户界面中的 页面。然后,选择minutes
集合作为数据源,并按照示例过程从Atlas用户界面或使用mongosh
创建索引。
➤ 使用 Select your language(选择您的语言)下拉菜单设置此页面上示例的语言。
asciiFolding
asciiFolding
词元过滤器将不在基本拉丁语 Unicode 块 中的字母、数字和符号 Unicode 字符转换为等效的 ASCII 字符(如果可用)。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
| 字符串 | no | 此字符串指定在词元过滤器的输出中是否包含或省略原始词元。值可以是以下值之一:
默认值: |
例子
以下索引定义使用名为 asciiConverter
的自定义分析器对 minutes 集合中的 page_updated_by.first_name
字段进行索引。自定义分析器指定以下内容:
应用标准分词器,以根据分词规则创建词元。
应用
asciiFolding
词元筛选器,以将字段值转换为等效的 ASCII 字符。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
asciiConverter
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 standard。
展开 Token Filters,然后单击 Add token filter。
从下拉列表中选择 asciiFolding。
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分,单击 Add Field Mapping 可对 page_updated_by.first_name字段应用自定义分析器。
选择 page_updated_by.first_name(位于 Field Name 下拉菜单中),然后从 Data Type 下拉菜单中选择 String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
asciiConverter
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "mappings": { "dynamic": false, "fields": { "page_updated_by": { "type": "document", "dynamic": false, "fields": { "first_name": { "type": "string", "analyzer": "asciiConverter" } } } } }, "analyzers": [ { "name": "asciiConverter", "tokenizer": { "type": "standard" }, "tokenFilters": [ { "type": "asciiFolding" } ] } ] }
1 db.minutes.createSearchIndex( 2 "default", 3 { 4 "mappings": { 5 "dynamic": false, 6 "fields": { 7 "page_updated_by": { 8 "type": "document", 9 "dynamic": false, 10 "fields": { 11 "first_name": { 12 "type": "string", 13 "analyzer": "asciiConverter" 14 } 15 } 16 } 17 } 18 }, 19 "analyzers": [ 20 { 21 "name": "asciiConverter", 22 "tokenizer": { 23 "type": "standard" 24 }, 25 "tokenFilters": [ 26 { 27 "type": "asciiFolding" 28 } 29 ] 30 } 31 ] 32 } 33 )
以下查询使用等效的 ASCII 字符在 minutes 集合的 first_name
字段中搜索名称。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "index": "default", "text": { "query": "Sian", "path": "page_updated_by.first_name" } } }
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: "Cette page traite des réunions de département"
db.minutes.aggregate([ { "$search": { "index": "default", "text": { "query": "Sian", "path": "page_updated_by.first_name" } } }, { "$project": { "_id": 1, "page_updated_by.last_name": 1, "page_updated_by.first_name": 1 } } ])
[ { _id: 1, page_updated_by: { last_name: 'AUERBACH', first_name: 'Siân'} } ]
MongoDB Search 在结果中返回包含 _id: 1
的文档,因为MongoDB Search 为文档中的 page_updated_by.first_name
字段创建了以下词元(可搜索词语),然后使用该词元与查询术语Sian
进行匹配:
字段名称 | 输出词元 |
---|---|
|
|
daitchMokotoffSoundex
The daitchMokotoffSoundex
词元过滤根据Daitch-Mokotoff Soundex语音算法为发音相同的单词创建词元。此过滤可以为每个输入生成多个编码,其中每个编码的词元是一个 6 位数字。
注意
不要在以下位置中使用 daitchMokotoffSoundex
词元筛选器:
synonym 或 autocomplete 类型映射定义。
启用了
fuzzy
的操作符。MongoDB Search 支持以下操作符的fuzzy
选项:
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
| 字符串 | no | 此字符串指定在词元过滤器的输出中是否包含或省略原始词元。值可以是以下值之一:
默认值: |
例子
以下索引定义使用名为 dmsAnalyzer
的自定义分析器对 minutes 集合中的 page_updated_by.last_name
字段进行索引。自定义分析器指定以下内容:
应用标准分词器,以根据分词规则创建词元。
应用
daitchMokotoffSoundex
词元过滤器,对发音相同的单词的标记进行编码。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
dmsAnalyzer
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 standard。
展开 Token Filters,然后单击 Add token filter。
从下拉菜单中选择 daitchMokotoffSoundex,然后为 originalTokens 字段选择下表中显示的值:
字段值originalTokens
include
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分,单击 Add Field Mapping 可对 page_updated_by.last_name字段应用自定义分析器。
选择 page_updated_by.last_name(位于 Field Name 下拉菜单中),然后从 Data Type 下拉菜单中选择 String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
dmsAnalyzer
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "mappings": { "dynamic": false, "fields": { "page_updated_by": { "type": "document", "dynamic": false, "fields": { "last_name": { "type": "string", "analyzer": "dmsAnalyzer" } } } } }, "analyzers": [ { "name": "dmsAnalyzer", "tokenizer": { "type": "standard" }, "tokenFilters": [ { "type": "daitchMokotoffSoundex", "originalTokens": "include" } ] } ] }
1 db.minutes.createSearchIndex( 2 "default", 3 { 4 "mappings": { 5 "dynamic": false, 6 "fields": { 7 "page_updated_by": { 8 "type": "document", 9 "dynamic": false, 10 "fields": { 11 "last_name": { 12 "type": "string", 13 "analyzer": "dmsAnalyzer" 14 } 15 } 16 } 17 } 18 }, 19 "analyzers": [ 20 { 21 "name": "dmsAnalyzer", 22 "tokenizer": { 23 "type": "standard" 24 }, 25 "tokenFilters": [ 26 { 27 "type": "daitchMokotoffSoundex", 28 "originalTokens": "include" 29 } 30 ] 31 } 32 ] 33 } 34 )
以下查询在分钟集合的 page_updated_by.last_name
字段中搜索听起来类似于 AUERBACH
的词语。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "index": "default", "text": { "query": "AUERBACH", "path": "page_updated_by.last_name" } } }
SCORE: 0.568153440952301 _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" SCORE: 0.521163284778595 _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"
db.minutes.aggregate([ { "$search": { "index": "default", "text": { "query": "AUERBACH", "path": "page_updated_by.last_name" } } }, { "$project": { "_id": 1, "page_updated_by.last_name": 1 } } ])
[ { "_id" : 1, "page_updated_by" : { "last_name" : "AUERBACH" } } { "_id" : 2, "page_updated_by" : { "last_name" : "OHRBACH" } } ]
MongoDB Search 返回包含 _id: 1
和 _id: 2
的文档,因为两个文档中的词语在语音上相似,并使用相同的 6 位数字(097400
和 097500
)进行编码。下表显示了MongoDB Search 为结果中的文档创建的词元(可搜索词语和六位数字编码):
文档 ID | 输出词元 |
---|---|
|
|
|
|
edgeGram
edgeGram
词元筛选器将文本输入左侧(或“边缘”)的输入拆分为 n 个配置大小的词语。
注意
通常,词元筛选器的运行方式与管道类似,每个输入词元生成不超过一个输出词元,然后将其输入到后续词元中。相比之下,edgeGram
词元过滤是一种图形生成过滤,可从单个输入词元中生成多个输出词元。
由于 synonym 和 autocomplete 字段类型映射定义仅在与非图表生成词元过滤器一起使用时才有效,因此,您无法在 synonym 或 autocomplete 字段类型映射定义中使用 edgeGram 词元过滤器。
对于使用regex
(MongoDB搜索操作符)或wildcard
操作符进行查询,您不能将 edgeGram
词元过滤用作 searchAnalyzer
,因为它为每个输入词元生成多个输出词元。在索引定义中指定不同的分析器作为 searchAnalyzer
。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
| 整型 | 是 | 指定生成的 n 个词语的最小长度的数字。值必须小于或等于 |
| 整型 | 是 | 指定生成的 n 个词语的最大长度的数字。值必须大于或等于 |
| 字符串 | no | 字符串,用于指定对短于
如果指定了 默认值: |
例子
以下索引定义使用名为 titleAutocomplete
的自定义分析器对 minutes 集合中的 title
字段进行索引。自定义分析器指定以下内容:
应用标准分词器,以根据分词规则创建词元。
将以下筛选器应用于词元:
icuFolding
词元筛选器,用于将字符折叠应用于词元。edgeGram
词元筛选器,用于从左侧创建 4 到 7 个字符长的词元。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
titleAutocomplete
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 standard。
展开 Token Filters,然后单击 Add token filter。
从下拉列表中选择 icuFolding,然后单击 Add token filter 以将词元过滤器添加到您的自定义分析器中。
单击 Add token filter 可添加另一个词元筛选器。
从下拉菜单中选择 edgeGram,然后在字段中输入下表中显示的值:
字段值minGram
4
maxGram
7
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分中,单击 Add Field Mapping 以对标题字段应用自定义分析器。
从 Field Name 下拉列表中选择 Title(标题),然后从 Data Type 下拉列表中选择 String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
titleAutocomplete
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "analyzer": "titleAutocomplete", "mappings": { "dynamic": false, "fields": { "title": { "type": "string", "analyzer": "titleAutocomplete" } } }, "analyzers": [ { "name": "titleAutocomplete", "charFilters": [], "tokenizer": { "type": "standard" }, "tokenFilters": [ { "type": "icuFolding" }, { "type": "edgeGram", "minGram": 4, "maxGram": 7 } ] } ] }
1 db.minutes.createSearchIndex( 2 "default", 3 { 4 "analyzer": "titleAutocomplete", 5 "mappings": { 6 "dynamic": false, 7 "fields": { 8 "title": { 9 "type": "string", 10 "analyzer": "titleAutocomplete" 11 } 12 } 13 }, 14 "analyzers": [ 15 { 16 "name": "titleAutocomplete", 17 "charFilters": [], 18 "tokenizer": { 19 "type": "standard" 20 }, 21 "tokenFilters": [ 22 { 23 "type": "icuFolding" 24 }, 25 { 26 "type": "edgeGram", 27 "minGram": 4, 28 "maxGram": 7 29 } 30 ] 31 } 32 ] 33 } 34 )
以下查询在 minutes 集合的 title
字段中搜索以 mee
开头并后跟任意数量的其他字符的词语。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "wildcard": { "query": "mee*", "path": "title", "allowAnalyzedField": true } } }
SCORE: 1 _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" SCORE: 1 _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>"
db.minutes.aggregate([ { "$search": { "wildcard": { "query": "mee*", "path": "title", "allowAnalyzedField": true } } }, { "$project": { "_id": 1, "title": 1 } } ])
[ { _id: 1, title: 'The team's weekly meeting' }, { _id: 3, title: 'The regular board meeting' } ]
MongoDB Search 返回包含 _id: 1
和 _id: 3
的文档,因为这些文档包含与查询条件匹配的术语meeting
。具体来说, MongoDB Search 会为结果中的文档创建以下 4 到 7 字符词元(可搜索词语),然后将其与查询术语mee*
进行匹配:
文档 ID | 输出词元 |
---|---|
|
|
|
|
englishPossessive
englishPossessive
词元筛选器从词语中删除所有格(末尾的 's
)。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
例子
以下索引定义使用名为 englishPossessiveStemmer
的自定义分析器对 minutes 集合中的 title
字段进行索引。自定义分析器指定以下内容:
应用标准分词器,以根据分词规则创建词元(搜索词语)。
应用englishPossessive 词元筛选器从词元中删除所有格(末尾的
's
)。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
englishPossessiveStemmer
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 standard。
展开 Token Filters,然后单击 Add token filter。
从下拉列表中选择 englishPossessive。
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分中,单击 Add Field Mapping 以对标题字段应用自定义分析器。
从 Field Name 下拉列表中选择 Title(标题),然后从 Data Type 下拉列表中选择 String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
englishPossessiveStemmer
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "mappings": { "fields": { "title": { "type": "string", "analyzer": "englishPossessiveStemmer" } } }, "analyzers": [ { "name": "englishPossessiveStemmer", "charFilters": [], "tokenizer": { "type": "standard" }, "tokenFilters": [ { "type": "englishPossessive" } ] } ] }
1 db.minutes.createSearchIndex( 2 "default", 3 { 4 "mappings": { 5 "fields": { 6 "title": { 7 "type": "string", 8 "analyzer": "englishPossessiveStemmer" 9 } 10 } 11 }, 12 "analyzers": [ 13 { 14 "name": "englishPossessiveStemmer", 15 "charFilters": [], 16 "tokenizer": { 17 "type": "standard" 18 }, 19 "tokenFilters": [ 20 { 21 "type": "englishPossessive" 22 } 23 ] 24 } 25 ] 26 } 27 )
以下查询在 minutes 集合的 title
字段中搜索词语 team
。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "index": "default", "text": { "query": "team", "path": "title" } } }
SCORE: 0.34314215183258057 _id: "1" message: "try to siGn-In" page_updated_by: Object text: Object SCORE: 0.29123833775520325 _id: "2" message: "do not forget to SIGN-IN. See ① for details." page_updated_by: Object text: Object
db.minutes.aggregate([ { "$search": { "index": "default", "text": { "query": "team", "path": "title" } } }, { "$project": { "_id": 1, "title": 1 } } ])
[ { _id: 1, title: 'The team's weekly meeting' }, { _id: 2, title: 'The check-in with sales team' } ]
MongoDB Search 返回在 title
字段中包含术语team
的结果。MongoDB Search 返回包含 _id: 1
的文档,因为MongoDB Search 在分析过程中将 title
字段中的 team's
转换为词元 team
。具体来说, MongoDB Search 为结果中的文档创建以下词元(可搜索词语),然后将其与查询术语进行匹配:
文档 ID | 输出词元 |
---|---|
|
|
|
|
flattenGraph
flattenGraph
词元过滤器将词元过滤器图表转换为适合编制索引的平面形式。如果使用 wordDelimiterGraph 词元过滤器,请在 wordDelimiterGraph 词元过滤器后面使用该过滤器。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
例子
以下索引定义使用名为 wordDelimiterGraphFlatten
的自定义分析器为分钟集合中的 message
字段编制索引。自定义分析器指定以下内容:
应用空格分词器,以根据词语之间出现的空格创建词元。
将以下过滤器应用于词元:
wordDelimiterGraph 词元筛选器,用于根据子词拆分词元,为原始词生成词元,并保护词
SIGN_IN
免遭分隔。flattenGraph 词元过滤器,用于将词元扁平化为平面形式。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
wordDelimiterGraphFlatten
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 whitespace。
展开 Token Filters,然后单击 Add token filter。
从下拉菜单中选择 wordDelimiterGraph,并为词元筛选器配置以下字段。
选择以下字段:
字段值delimiterOptions.generateWordParts
true
delimiterOptions.preserveOriginal
true
在
protectedWords.words
字段中输入SIGN_IN
。选择
protectedWords.ignoreCase
。
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add token filter 可添加另一个词元筛选器。
从下拉列表中选择 flattenGraph。
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分中,单击 Add Field Mapping 以在message(消息)字段上应用自定义分析器。
从 Field Name 下拉列表中选择 message(消息),并从 Data Type 下拉列表中选择String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
wordDelimiterGraphFlatten
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "mappings": { "fields": { "message": { "type": "string", "analyzer": "wordDelimiterGraphFlatten" } } }, "analyzers": [ { "name": "wordDelimiterGraphFlatten", "charFilters": [], "tokenizer": { "type": "whitespace" }, "tokenFilters": [ { "type": "wordDelimiterGraph", "delimiterOptions" : { "generateWordParts" : true, "preserveOriginal" : true }, "protectedWords": { "words": [ "SIGN_IN" ], "ignoreCase": false } }, { "type": "flattenGraph" } ] } ] }
1 db.minutes.createSearchIndex( 2 "default", 3 { 4 "mappings": { 5 "fields": { 6 "message": { 7 "type": "string", 8 "analyzer": "wordDelimiterGraphFlatten" 9 } 10 } 11 }, 12 "analyzers": [ 13 { 14 "name": "wordDelimiterGraphFlatten", 15 "charFilters": [], 16 "tokenizer": { 17 "type": "whitespace" 18 }, 19 "tokenFilters": [ 20 { 21 "type": "wordDelimiterGraph", 22 "delimiterOptions": { 23 "generateWordParts": true, 24 "preserveOriginal": true 25 }, 26 "protectedWords": { 27 "words": [ 28 "SIGN_IN" 29 ], 30 "ignoreCase": false 31 } 32 }, 33 { 34 "type": "flattenGraph" 35 } 36 ] 37 } 38 ] 39 } 40 )
以下查询在 minutes 集合的 message
字段中搜索词语 sign
。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "index": "default", "text": { "query": "sign", "path": "message" } } }
SCORE: 0.6763891577720642 _id: "3" message: "try to sign-in" page_updated_by: Object text: Object
db.minutes.aggregate([ { "$search": { "index": "default", "text": { "query": "sign", "path": "message" } } }, { "$project": { "_id": 1, "message": 1 } } ])
[ { _id: 3, message: 'try to sign-in' } ]
MongoDB Search 在查询术语sign
的结果中返回包含 _id: 3
的文档,即使该文档在 title
字段中包含带连字符的术语sign-in
。wordDelimiterGraph 词元过滤创建词元过滤图表,而 flattenGraph 词元过滤将词元过滤图表转换为适合索引的平面形式。具体来说, MongoDB Search 会为结果中的文档创建以下词元(可搜索词语),然后将其与查询术语sign
进行匹配:
文档 ID | 输出词元 |
|
|
icuFolding
icuFolding
词元过滤应用 Unicode 技术报告 #30 中的字符折叠,例如删除重音、大小写折叠、规范重复项折叠以及报告中详细说明的许多其他内容。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
例子
以下索引定义使用名为 diacriticFolder
的自定义分析器对 minutes 集合中的 text.sv_FI
字段进行索引。自定义分析器指定以下内容:
应用关键字分词器,以将字符串字段中的所有词语作为单个词语进行分词。
使用
icuFolding
词元筛选器应用折叠,例如删除重音、大小写折叠、规范重复项折叠等。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
diacriticFolder
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 keyword。
展开 Token Filters,然后单击 Add token filter。
从下拉列表中选择 icuFolding。
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分中,单击 Add Field Mapping 可对 text.sv_FI嵌套字段应用自定义分析器。
选择 text.sv_FI(嵌套在 Field Name 下拉菜单中),然后从 Data Type 下拉菜单中选择 String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
diacriticFolder
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "analyzer": "diacriticFolder", "mappings": { "fields": { "text": { "type": "document", "fields": { "sv_FI": { "analyzer": "diacriticFolder", "type": "string" } } } } }, "analyzers": [ { "name": "diacriticFolder", "charFilters": [], "tokenizer": { "type": "keyword" }, "tokenFilters": [ { "type": "icuFolding" } ] } ] }
1 db.minutes.createSearchIndex( 2 "default", 3 { 4 "analyzer": "diacriticFolder", 5 "mappings": { 6 "fields": { 7 "text": { 8 "type": "document", 9 "fields": { 10 "sv_FI": { 11 "analyzer": "diacriticFolder", 12 "type": "string" 13 } 14 } 15 } 16 } 17 }, 18 "analyzers": [ 19 { 20 "name": "diacriticFolder", 21 "charFilters": [], 22 "tokenizer": { 23 "type": "keyword" 24 }, 25 "tokenFilters": [ 26 { 27 "type": "icuFolding" 28 } 29 ] 30 } 31 ] 32 } 33 )
以下查询使用 wildcard 操作符在 minutes 集合的 text.sv_FI
字段中搜索所有包含词语 avdelning
(前后具有任意数量的其他字符)的词语。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "index": "default", "wildcard": { "query": "*avdelning*", "path": "text.sv_FI", "allowAnalyzedField": true } } } SCORE: 1 _id: "1" message: "try to siGn-In" page_updated_by: Object 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" SCORE: 1 _id: "2" message: "do not forget to SIGN-IN. See ① for details." page_updated_by: Object 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"
db.minutes.aggregate([ { "$search": { "index": "default", "wildcard": { "query": "*avdelning*", "path": "text.sv_FI", "allowAnalyzedField": true } } }, { "$project": { "_id": 1, "text.sv_FI": 1 } } ])
[ { _id: 1, text: { sv_FI: 'Den här sidan behandlar avdelningsmöten' } }, { _id: 2, text: { sv_FI: 'Först talade chefen för försäljningsavdelningen' } } ]
MongoDB Search 在结果中返回具有 _id: 1
和 _id: 2
的文档,因为这些文档包含查询术语avdelning
,在具有 _id: 1
的文档中后跟其他字符,在文档_id: 2
。具体来说, MongoDB Search 为结果中的文档创建以下词元,然后将其与查询术语*avdelning*
进行匹配。
文档 ID | 输出词元 |
---|---|
|
|
|
|
icuNormalizer
icuNormalizer
词元过滤使用标准Unicode 规范化模式。对词元进行规范化。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
| 字符串 | no | 要应用的规范化形式。接受的值为:
要学习;了解有关支持的规范化形式的详情,请参阅部分 1.2:规范化形式,UTR#15。 默认值: |
例子
以下索引定义使用名为 textNormalizer
的自定义分析器对 minutes 集合中的 message
字段进行索引。自定义分析器指定以下内容:
使用空格分词器根据词之间出现的空格创建标记。
使用
icuNormalizer
词元筛选器,通过兼容性分解和随后的规范组合对词元进行规范化。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
textNormalizer
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 whitespace。
展开 Token Filters,然后单击 Add token filter。
从下拉菜单中选择 icuNormalizer,然后从 normalizationForm 下拉菜单中选择
nfkc
。单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分中,单击 Add Field Mapping 以在message(消息)字段上应用自定义分析器。
从 Field Name 下拉列表中选择 message(消息),并从 Data Type 下拉列表中选择String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
textNormalizer
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "analyzer": "textNormalizer", "mappings": { "fields": { "message": { "type": "string", "analyzer": "textNormalizer" } } }, "analyzers": [ { "name": "textNormalizer", "charFilters": [], "tokenizer": { "type": "whitespace" }, "tokenFilters": [ { "type": "icuNormalizer", "normalizationForm": "nfkc" } ] } ] }
1 db.minutes.createSearchIndex( 2 "default", 3 { 4 "analyzer": "textNormalizer", 5 "mappings": { 6 "fields": { 7 "message": { 8 "type": "string", 9 "analyzer": "textNormalizer" 10 } 11 } 12 }, 13 "analyzers": [ 14 { 15 "name": "textNormalizer", 16 "charFilters": [], 17 "tokenizer": { 18 "type": "whitespace" 19 }, 20 "tokenFilters": [ 21 { 22 "type": "icuNormalizer", 23 "normalizationForm": "nfkc" 24 } 25 ] 26 } 27 ] 28 } 29 )
以下查询在 minutes 集合的 message
字段中搜索词语 1
。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "index": "default", "text": { "query": "1", "path": "message" } } } SCORE: 0.4342196583747864 _id: "2" message: "do not forget to SIGN-IN. See ① for details." page_updated_by: Object text: Object
db.minutes.aggregate([ { "$search": { "index": "default", "text": { "query": "1", "path": "message" } } }, { "$project": { "_id": 1, "message": 1 } } ])
[ { _id: 2, message: 'do not forget to SIGN-IN. See ① for details.' } ]
MongoDB搜索会在查询术语1
的结果中返回包含 _id: 2
的文档,即使该文档在 message
字段中包含带圆圈的数字 ①
,因为 icuNormalizer
词元过滤会为以下内容创建词元 1
:此字符使用 nfkc
规范化形式。下表显示了MongoDB Search 使用 nfkc
规范化形式为结果中的文档创建的词元(可搜索词语),并比较了它为其他规范化形式创建的词元。
规范化形式 | 输出词元 | matches |
|
| X |
|
| X |
|
| √ |
|
| √ |
kStemming
kStemming
词元筛选器将算法词干提取与内置的英语词典结合使用,以提取词语的词干。它要求使用小写文本,而不会修改大写文本。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
例子
以下索引定义使用名为 kStemmer
的自定义分析器对 minutes 集合中的 text.en_US
字段进行索引。自定义分析器指定以下内容:
应用标准分词器,以根据分词规则创建词元。
将以下筛选器应用于词元:
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
kStemmer
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 standard。
展开 Token Filters,然后单击 Add token filter。
从下拉列表中选择 lowercase,然后单击 Add token filter 以将词元过滤器添加到您的自定义分析器中。
单击 Add token filter 可添加另一个词元筛选器。
从下拉列表中选择 kStemming。
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分中,单击 Add Field Mapping 可对 text.en_US嵌套字段应用自定义分析器。
选择 text.en_US(嵌套在 Field Name 下拉菜单中),然后从 Data Type 下拉菜单中选择 String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
kStemmer
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "analyzer": "kStemmer", "mappings": { "dynamic": true }, "analyzers": [ { "name": "kStemmer", "tokenizer": { "type": "standard" }, "tokenFilters": [ { "type": "lowercase" }, { "type": "kStemming" } ] } ] }
1 db.minutes.createSearchIndex( 2 "default", 3 { 4 "analyzer": "kStemmer", 5 "mappings": { 6 "dynamic": true 7 }, 8 "analyzers": [ 9 { 10 "name": "kStemmer", 11 "tokenizer": { 12 "type": "standard" 13 }, 14 "tokenFilters": [ 15 { 16 "type": "lowercase" 17 }, 18 { 19 "type": "kStemming" 20 } 21 ] 22 } 23 ] 24 } 25 )
以下查询在 minutes 集合的 text.en_US
字段中搜索词语 Meeting
。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "index": "default", "text": { "query": "Meeting", "path": "text.en_US" } } } SCORE: 0.5960260629653931 _id: "1" message: "try to siGn-In" page_updated_by: Object 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"
db.minutes.aggregate([ { "$search": { "index": "default", "text": { "query": "Meeting", "path": "text.en_US" } } }, { "$project": { "_id": 1, "text.en_US": 1 } } ])
[ { _id: 1, text: { en_US: '<head> This page deals with department meetings. </head>' } } ]
MongoDB Search 返回包含 _id: 1
的文档,其中包含小写的复数术语meetings
。MongoDB Search 将查询术语与文档进行匹配,因为 lowercase 词元过滤将词元文本规范化为小写,而 kStemming 词元过滤允许MongoDB Search 将文档text.en_US
字段中的复数 meetings
与单数查询术语进行匹配。MongoDB Search 还使用索引分析器(如果指定,则使用 searchAnalyzer
)分析查询术语。具体来说, MongoDB Search 会为结果中的文档创建以下词元(可搜索词语),然后使用这些词元与查询术语进行匹配:
head
, this
, page
, deal
, with
, department
, meeting
, head
长度
length
词元筛选器删除太短或太长的词元。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
| 整型 | no | 指定词元的最大长度的数字。值必须小于或等于 默认值: |
| 整型 | no | 指定词元的最大长度的数字。值必须大于或等于 默认值: |
例子
以下索引定义使用名为 longOnly
的自定义分析器对 minutes 集合中的 text.sv_FI
字段进行索引。自定义分析器指定以下内容:
使用标准分词器,以根据分词规则创建词元。
将以下筛选器应用于词元:
icuFolding 词元过滤器,用于应用字符折叠。
length
词元筛选器,它仅对分词后至少为 20 个 UTF-16 代码单元长的词元进行索引。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
longOnly
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 standard。
展开 Token Filters,然后单击 Add token filter。
从下拉列表中选择 icuFolding,然后单击 Add token filter 以将词元过滤器添加到您的自定义分析器中。
单击 Add token filter 可添加另一个词元筛选器。
从下拉菜单中选择 length 并为词元筛选器配置以下字段:
字段值min
20
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分中,单击 Add Field Mapping 可对 text.sv.FI 嵌套字段应用自定义分析器。
从 Field Name 下拉菜单中选择嵌套的 text.sv.FI,并从 Data Type 下拉菜单中选择 String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
longOnly
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "mappings": { "fields": { "text": { "type": "document", "dynamic": true, "fields": { "sv_FI": { "type": "string", "analyzer": "longOnly" } } } } }, "analyzers": [ { "name": "longOnly", "charFilters": [], "tokenizer": { "type": "standard" }, "tokenFilters": [ { "type": "icuFolding" }, { "type": "length", "min": 20 } ] } ] }
1 db.minutes.createSearchIndex( 2 "default", 3 { 4 "mappings": { 5 "fields": { 6 "text": { 7 "type": "document", 8 "dynamic": true, 9 "fields": { 10 "sv_FI": { 11 "type": "string", 12 "analyzer": "longOnly" 13 } 14 } 15 } 16 } 17 }, 18 "analyzers": [ 19 { 20 "name": "longOnly", 21 "charFilters": [], 22 "tokenizer": { 23 "type": "standard" 24 }, 25 "tokenFilters": [ 26 { 27 "type": "icuFolding" 28 }, 29 { 30 "type": "length", 31 "min": 20 32 } 33 ] 34 } 35 ] 36 } 37 )
以下查询在 minutes 集合的 text.sv_FI
字段中搜索词语 forsaljningsavdelningen
。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "index": "default", "text": { "query": "forsaljningsavdelningen", "path": "text.sv_FI" } } } SCORE: 0.13076457381248474 _id: "2" message: "do not forget to SIGN-IN. See ① for details." page_updated_by: Object 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"
db.minutes.aggregate([ { "$search": { "index": "default", "text": { "query": "forsaljningsavdelningen", "path": "text.sv_FI" } } }, { "$project": { "_id": 1, "text.sv_FI": 1 } } ])
[ { _id: 2, text: { sv_FI: 'Först talade chefen för försäljningsavdelningen' } } ]
MongoDB Search 返回包含 _id: 2
的文档,其中包含术语försäljningsavdelningen
。MongoDB Search 将文档与查询术语进行匹配,因为该术语超过 20 个字符。此外,虽然查询术语forsaljningsavdelningen
不包含变音符号,但MongoDB Search 通过折叠文档中原始术语的变音符号来将查询术语与文档进行匹配。具体来说, MongoDB搜索为包含 _id: 2
的文档创建以下词元(可搜索词)。
forsaljningsavdelningen
MongoDB Search 不会返回集合中 text.sv_FI
字段中任何其他术语的搜索结果,因为该字段中的所有其他词语的字符数都少于 20。
lowercase
lowercase
词元筛选器将词元文本规范化为小写。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
示例
以下示例索引定义使用 nGram
分词策略为 minutes 集合中的 title
字段构建 autocomplete 类型的索引。它会对 title
字段应用名为 keywordLowerer
的自定义分析器。自定义分析器指定以下内容:
应用关键字分词器为字符串或字符串数组创建单个词元。
应用
lowercase
词元筛选器将词元文本转换为小写。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
keywordLowerer
。如果 Tokenizer 已折叠,请将其展开,然后从下拉菜单中选择 keyword。
展开 Token Filters,然后单击 Add token filter。
从下拉列表中选择 lowercase,然后单击 Add token filter 以将词元过滤器添加到您的自定义分析器中。
在 Field Mappings 部分中,单击 Add Field Mapping 以对标题字段应用自定义分析器。
从 Field Name 下拉菜单中选择 title(标题),并从 Data Type 下拉菜单中选择 Autocomplete(自动完成)。
在数据类型的属性部分中,从属性的下拉菜单中选择以下值:
属性名称值Analyzer
keywordLowerer
Tokenization
nGram
单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下内容:
{ "mappings": { "fields": { "title": { "analyzer": "keywordLowerer", "tokenization": "nGram", "type": "autocomplete" } } }, "analyzers": [ { "name": "keywordLowerer", "charFilters": [], "tokenizer": { "type": "keyword" }, "tokenFilters": [ { "type": "lowercase" } ] } ] }
1 db.minutes.createSearchIndex( 2 "default", 3 { 4 "mappings": { 5 "fields": { 6 "title": { 7 "analyzer": "keywordLowerer", 8 "tokenization": "nGram", 9 "type": "autocomplete" 10 } 11 } 12 }, 13 "analyzers": [ 14 { 15 "name": "keywordLowerer", 16 "charFilters": [], 17 "tokenizer": { 18 "type": "keyword" 19 }, 20 "tokenFilters": [ 21 { 22 "type": "lowercase" 23 } 24 ] 25 } 26 ] 27 } 28 )
以下查询使用 autocomplete 操作符在 title
字段中搜索字符 standup
。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "index": "default", "autocomplete": { "query": "standup", "path": "title" } } } SCORE: 0.9239386320114136 _id: “4” message: "write down your signature or phone №" page_updated_by: Object text: Object
db.minutes.aggregate([ { "$search": { "index": "default", "autocomplete": { "query": "standup", "path": "title" } } }, { "$project": { "_id": 1, "title": 1 } } ])
[ { _id: 4, title: 'The daily huddle on tHe StandUpApp2' } ]
MongoDB Search 返回结果中包含 _id: 4
的文档,因为该文档包含查询术语standup
。MongoDB Search 使用 keyword
分词器、lowercase
词元过滤和 自动完成 类型的 nGram
分词策略为 title
字段创建词元。具体来说, MongoDB Search 使用 keyword
分词器将整个字符串分词为单个词元,该词元仅支持整个字符串的精确匹配,然后使用 lowercase
词元过滤将词元转换为小写。 对于结果中的文档, MongoDB Search 使用自定义分析器创建以下词元:
文档 ID | 输出词元 |
|
|
应用自定义分析器后, MongoDB搜索会创建更多 n 元语法词元,因为MongoDB搜索会将 title
字段索引为索引定义中指定的自动完成类型。MongoDB Search 使用 n 元语法的词元(其中包括 standup
的词元)来将文档与查询术语standup
进行匹配。
以下索引定义使用名为 lowerCaser
的自定义分析器对 minutes 集合中的 message
字段进行索引。自定义分析器指定以下内容:
应用标准分词器,以根据分词规则创建词元。
将以下筛选器应用于词元:
icuNormalizer 使用标准 Unicode 规范化模式对词元进行规范化。
lowercase
词元过滤器,用于将词元文本转换为小写。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
lowerCaser
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 standard。
展开 Token Filters,然后单击 Add token filter。
从下拉菜单中选择 icuNormalizer,然后从 normalizationForm 下拉菜单中选择
nfkd
。单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add token filter 可添加另一个词元筛选器。
从下拉列表中选择 lowercase。
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分中,单击 Add Field Mapping 以在message(消息)字段上应用自定义分析器。
从 Field Name 下拉列表中选择 message(消息),并从 Data Type 下拉列表中选择String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
lowerCaser
。单击 Add,然后单击 Save Changes。
{ "mappings": { "fields": { "message": { "type": "string", "analyzer": "lowerCaser" } } }, "analyzers": [ { "name": "lowerCaser", "charFilters": [], "tokenizer": { "type": "standard" }, "tokenFilters": [ { "type": "icuNormalizer", "normalizationForm": "nfkd" }, { "type": "lowercase" } ] } ] }
1 db.minutes.createSearchIndex( 2 "default", 3 { 4 "mappings": { 5 "fields": { 6 "message": { 7 "type": "string", 8 "analyzer": "lowerCaser" 9 } 10 } 11 }, 12 "analyzers": [ 13 { 14 "name": "lowerCaser", 15 "charFilters": [], 16 "tokenizer": { 17 "type": "standard" 18 }, 19 "tokenFilters": [ 20 { 21 "type": "icuNormalizer", 22 "normalizationForm": "nfkd" 23 }, 24 { 25 "type": "lowercase" 26 } 27 ] 28 } 29 ] 30 } 31 )
以下查询在message
字段中搜索sign-in
一词。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "index": "default", "text": { "query": "sign-in", "path": "message" } } } SCORE: 0.37036222219467163 _id: "1" message: "try to siGn-In" page_updated_by: Object text: Object SCORE: 0.37036222219467163 _id: "3" message: "try to sign-in" page_updated_by: Object text: Object SCORE: 0.2633555233478546 _id: "2" message: "do not forget to SIGN-IN. See ① for details." page_updated_by: Object text: Object
db.minutes.aggregate([ { "$search": { "index": "default", "text": { "query": "sign-in", "path": "message" } } }, { "$project": { "_id": 1, "message": 1 } } ])
[ { _id: 1, message: 'try to siGn-In' }, { _id: 3, message: 'try to sign-in' }, { _id: 2, message: 'do not forget to SIGN-IN. See ① for details.' } ]
MongoDB Search 在查询术语sign-in
的结果中返回包含 _id: 1
、_id: 3
和 _id:
2
的文档,因为 icuNormalizer
分词器首先通过拆分文本(包括带连字符的单词)来创建单独的词元,但保留文档中的原始字母大小写,然后 lowercase
词元过滤将这些词元转换为小写。MongoDB Search 还使用索引分析器(或者,如果指定,则使用 searchAnalyzer
)分析查询术语,分割查询术语并将其与文档匹配。
规范化形式 | 输出词元 |
|
|
|
|
|
|
nGram
nGram
词元过滤器将输入拆分为 n 个配置大小的词语。您无法在 synonym 或 autocomplete 映射定义中使用 nGram 词元过滤器。
注意
对于使用regex
(MongoDB搜索操作符)或wildcard
操作符进行查询,您不能将 ngram
词元过滤用作 searchAnalyzer
,因为它为每个输入词元生成多个输出词元。在索引定义中指定不同的分析器作为 searchAnalyzer
。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
| 整型 | 是 | 指定生成的 n 个词语的最小长度的数字。值必须小于或等于 |
| 整型 | 是 | 指定生成的 n 个词语的最大长度的数字。值必须大于或等于 |
| 字符串 | no | 字符串,用于指定对短于
如果指定了 默认值: |
例子
以下索引定义使用名为 titleAutocomplete
的自定义分析器为分钟集合中的 title
字段编制索引。它将关键字分析器指定为 searchAnalyzer
。自定义分析器函数指定以下内容:
应用标准分词器以根据分词规则创建词元。
将一系列词元筛选器应用于词元:
englishPossessive
从词语中删除所有格(末尾的's
)。nGram
将词语拆分为 4 到 7 个字符长的词语。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
titleAutocomplete
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 standard。
展开 Token Filters,然后单击 Add token filter。
从下拉列表中选择 englishPossessive,然后单击 Add token filter 以将词元过滤器添加到您的自定义分析器中。
单击 Add token filter 可添加另一个词元筛选器。
从下拉菜单中选择 nGram 并为词元筛选器配置以下字段:
字段值minGram
4
maxGram
7
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分中,单击 Add Field Mapping 以对标题字段应用自定义分析器。
从 Field Name 下拉列表中选择 Title(标题),然后从 Data Type 下拉列表中选择 String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
titleAutocomplete
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "mappings": { "fields": { "title": { "type": "string", "analyzer": "titleAutocomplete", "searchAnalyzer": "lucene.keyword" } } }, "analyzers": [ { "name": "titleAutocomplete", "charFilters": [], "tokenizer": { "type": "standard" }, "tokenFilters": [ { "type": "englishPossessive" }, { "type": "nGram", "minGram": 4, "maxGram": 7 } ] } ] }
1 db.minutes.createSearchIndex( 2 "default", 3 { 4 "mappings": { 5 "fields": { 6 "title": { 7 "type": "string", 8 "analyzer": "titleAutocomplete", 9 "searchAnalyzer": "lucene.keyword" 10 } 11 } 12 }, 13 "analyzers": [ 14 { 15 "name": "titleAutocomplete", 16 "charFilters": [], 17 "tokenizer": { 18 "type": "standard" 19 }, 20 "tokenFilters": [ 21 { 22 "type": "englishPossessive" 23 }, 24 { 25 "type": "nGram", 26 "minGram": 4, 27 "maxGram": 7 28 } 29 ] 30 } 31 ] 32 } 33 )
以下查询使用 wildcard 操作符在 minutes 集合的 title
字段中搜索词语 meet
,该词语后面具有任意数量的其他字符。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "index": "default", "wildcard": { "query": "meet*", "path": "title", "allowAnalyzedField": true } } } SCORE: 1 _id: "1" message: "try to siGn-In" page_updated_by: Object text: Object title: "The team's weekly meeting" SCORE: 1 _id: "3" message: "try to sign-in" page_updated_by: Object text: Object title: "The regular board meeting"
db.minutes.aggregate([ { "$search": { "index": "default", "wildcard": { "query": "meet*", "path": "title", "allowAnalyzedField": true } } }, { "$project": { "_id": 1, "title": 1 } } ])
[ { _id: 1, title: 'The team's weekly meeting' }, { _id: 3, title: 'The regular board meeting' } ]
MongoDB Search 返回包含 _id: 1
和 _id: 3
的文档,因为这些文档包含术语meeting
, MongoDB Search 通过创建以下词元(可搜索词语)将其与查询条件 meet*
进行匹配。
规范化形式 | 输出词元 |
---|---|
|
|
|
|
注意
MongoDB Search 不会为少于 4 个字符(例如 the
)和多于 7 个字符的词语创建词元,因为 termNotInBounds
参数默认设立为 omit
。如果将 termNotInBounds
参数的值设立为 include
, MongoDB Search 也会为术语the
创建词元。
porterStemming
porterStemming
词元筛选器使用 porter 词干提取算法,从英语词语中删除常见的形态后缀和屈折后缀。它要求使用小写文本,对于大写文本无效。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
例子
以下索引定义使用名为 porterStemmer
的自定义分析器对 minutes 集合中的 title
字段进行索引。自定义分析器指定以下内容:
应用标准分词器,以根据分词规则创建词元。
将以下词元筛选器应用于词元:
lowercase 词元过滤器,用于将词语转换为小写。
porterStemming 词元过滤器,用于从词语中删除常见的形态后缀和屈折后缀。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
porterStemmer
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 standard。
展开 Token Filters,然后单击 Add token filter。
从下拉列表中选择 lowercase,然后单击 Add token filter 以将词元过滤器添加到您的自定义分析器中。
单击 Add token filter 可添加另一个词元筛选器。
从下拉列表中选择 porterStemming。
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分中,单击 Add Field Mapping 以对标题字段应用自定义分析器。
从 Field Name 下拉列表中选择 Title(标题),然后从 Data Type 下拉列表中选择 String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
porterStemmer
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "mappings": { "fields": { "title": { "type": "string", "analyzer": "porterStemmer" } } }, "analyzers": [ { "name": "porterStemmer", "tokenizer": { "type": "standard" }, "tokenFilters": [ { "type": "lowercase" }, { "type": "porterStemming" } ] } ] }
db.minutes.createSearchIndex("default", { "mappings": { "fields": { "title": { "type": "string", "analyzer": "porterStemmer" } } }, "analyzers": [ { "name": "porterStemmer", "tokenizer": { "type": "standard" }, "tokenFilters": [ { "type": "lowercase" }, { "type": "porterStemming" } ] } ] })
以下查询在 minutes 集合的 title
字段中搜索词语 Meet
。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "index": "default", "text": { "query": "Meet", "path": "title" } } } SCORE: 0.34314215183258057 _id: “1” message: "try to siGn-In" page_updated_by: Object text: Object SCORE: 0.34314215183258057 _id: “3” message: "try to sign-in" page_updated_by: Object text: Object
db.minutes.aggregate([ { "$search": { "index": "default", "text": { "query": "Meet", "path": "title" } } }, { "$project": { "_id": 1, "title": 1 } } ])
[ { _id: 1, title: 'The team's weekly meeting' }, { _id: 3, title: 'The regular board meeting' } ]
MongoDB Search 返回包含 _id: 1
和 _id: 3
的文档,因为小写词元过滤将词元文本规范化为小写,然后 porterStemming
词元过滤从 meeting
词元中提取形态后缀以创建 meet
词元,这MongoDB Search 与查询术语Meet
匹配。具体来说, MongoDB Search 为结果中的文档创建以下词元(可搜索词语),然后将其与查询术语Meet
进行匹配:
规范化形式 | 输出词元 |
---|---|
|
|
|
|
正则表达式(Regex)
regex
词元过滤器将具有 Java 正则表达式语法 的正则表达式应用于每个词元,以将匹配项替换为指定的字符串。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器的人类可读标签。值必须是 | |||||||||||||||||||||
| 字符串 | 是 | 应用于每个词元的正则表达式模式。 | |||||||||||||||||||||
| 字符串 | 是 | 替换字符串,用于在出现匹配模式时进行替换。 如果指定空字符串 (
| |||||||||||||||||||||
| 字符串 | 是 | 可接受的值为:
如果 |
例子
以下索引定义使用名为 emailRedact
的自定义分析器对 minutes 集合中的 page_updated_by.email
字段进行索引。自定义分析器指定以下内容:
应用关键字分词器,以将字段值中的所有词语作为单个词语进行索引。
将以下词元筛选器应用于词元:
lowercase 词元过滤器,用于将词元中的大写字符转换为小写。
regex
词元筛选器,用于查找词元中看起来像电子邮件地址的字符串,并将其替换为词语redacted
。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
emailRedact
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 keyword。
展开 Token Filters,然后单击 Add token filter。
从下拉列表中选择 lowercase,然后单击 Add token filter 以将词元过滤器添加到您的自定义分析器中。
单击 Add token filter 可添加另一个词元筛选器。
从下拉菜单中选择 regex 并为词元筛选器配置以下内容:
在 pattern 字段中输入
^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,5})$
。在 replacement 字段中输入
redacted
。从 matches 下拉列表中选择
all
。
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分中,单击 Add Field Mapping 可对 page_updated_by.email 嵌套字段应用自定义分析器。
从 Field Name 下拉菜单中选择嵌套的 page_updated_by.email,并从 Data Type 下拉菜单中选择 String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
emailRedact
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "analyzer": "lucene.standard", "mappings": { "dynamic": false, "fields": { "page_updated_by": { "type": "document", "fields": { "email": { "type": "string", "analyzer": "emailRedact" } } } } }, "analyzers": [ { "charFilters": [], "name": "emailRedact", "tokenizer": { "type": "keyword" }, "tokenFilters": [ { "type": "lowercase" }, { "matches": "all", "pattern": "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,5})$", "replacement": "redacted", "type": "regex" } ] } ] }
db.minutes.createSearchIndex("default", { "analyzer": "lucene.standard", "mappings": { "dynamic": false, "fields": { "page_updated_by": { "type": "document", "fields": { "email": { "type": "string", "analyzer": "emailRedact" } } } } }, "analyzers": [ { "charFilters": [], "name": "emailRedact", "tokenizer": { "type": "keyword" }, "tokenFilters": [ { "type": "lowercase" }, { "matches": "all", "pattern": "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,5})$", "replacement": "redacted", "type": "regex" } ] } ] })
以下查询使用 wildcard 操作符在 minutes 集合的 page_updated_by.email
字段中搜索前面带有任意数量的其他字符的词语 example.com
。
1 db.minutes.aggregate([ 2 { 3 "$search": { 4 "index": "default", 5 "wildcard": { 6 "query": "*example.com", 7 "path": "page_updated_by.email", 8 "allowAnalyzedField": true 9 } 10 } 11 }, 12 { 13 "$project": { 14 "_id": 1, 15 "page_updated_by.email": 1 16 } 17 } 18 ])
尽管 page_updated_by.email
字段在电子邮件地址中包含单词 example.com
,但MongoDB Search 不会返回任何查询结果。MongoDB Search 将与自定义分析器中提供的正则表达式匹配的字符串标记为单词 redacted
,因此MongoDB Search 不会将查询术语与任何文档相匹配。
reverse
reverse
词元筛选器颠倒每个字符串词元的顺序。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器的人类可读标签。值必须是 |
例子
以下索引定义使用名为 keywordReverse
的自定义分析器对 minutes 集合中的 page_updated_by.email
字段进行索引。该自定义分析器指定以下内容:
应用关键字分词器,以将整个字符串作为单个词语进行分词。
应用
reverse
词元筛选器以颠倒字符串词元的顺序。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
reverseAnalyzer
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 standard。
展开 Token Filters,然后单击 Add token filter。
从下拉列表中选择 reverse。
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分中,单击 Add Field Mapping 以对标题字段应用自定义分析器。
从 Field Name 下拉列表中选择 Title(标题),然后从 Data Type 下拉列表中选择 String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
reverseAnalyzer
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "analyzer": "keywordReverse", "mappings": { "dynamic": true }, "analyzers": [ { "name": "keywordReverse", "charFilters": [], "tokenizer": { "type": "keyword" }, "tokenFilters": [ { "type": "reverse" } ] } ] }
db.minutes.createSearchIndex("default", { "analyzer": "keywordReverse", "mappings": { "dynamic": true }, "analyzers": [ { "name": "keywordReverse", "charFilters": [], "tokenizer": { "type": "keyword" }, "tokenFilters": [ { "type": "reverse" } ] } ] })
以下查询使用 wildcard 操作符搜索 minutes 集合中的 page_updated_by.email
字段,以按倒序匹配字符 @example.com
前面的任何字符。reverse
词元筛选器可以加快前导通配符查询的速度。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "wildcard": { "query": "*@example.com", "path": "page_updated_by.email", "allowAnalyzedField": true } } } SCORE: 1 _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 SCORE: 1 _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 SCORE: 1 _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 SCORE: 1 _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
.. io-code-block:: :copyable: true .. input:: :language: json db.minutes.aggregate([ { "$search": { "index": "default", "wildcard": { "query": "*@example.com", "path": "page_updated_by.email", "allowAnalyzedField": true } } }, { "$project": { "_id": 1, "page_updated_by.email": 1, } } ]) .. output:: :language: json [ { _id: 1, page_updated_by: { email: 'auerbach@example.com' } }, { _id: 2, page_updated_by: { email: 'ohrback@example.com' } }, { _id: 3, page_updated_by: { email: 'lewinsky@example.com' } }, { _id: 4, page_updated_by: { email: 'levinski@example.com' } } ]
对于前面的查询, MongoDB Search 将自定义分析器应用于通配符查询,以将查询转换如下:
moc.elpmaxe@*
然后, MongoDB Search 针对索引的词元运行查询,这些词元也是倒序的。具体来说, MongoDB Search 为结果中的文档创建以下词元(可搜索词语),然后将其与查询术语moc.elpmaxe@*
进行匹配:
规范化形式 | 输出词元 |
---|---|
|
|
|
|
|
|
|
|
shingle
shingle
词元筛选器通过一系列词元构建 shingle(拆分为 n 个词语)。您无法在 synonym 或 autocomplete 映射定义中使用 shingle
词元筛选器。
注意
对于使用regex
(MongoDB搜索操作符)或wildcard
操作符进行查询,您不能将 shingle
词元过滤用作 searchAnalyzer
,因为它为每个输入词元生成多个输出词元。在索引定义中指定不同的分析器作为 searchAnalyzer
。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
| 整型 | 是 | 每个 shingle 的最小词元数。必须大于或等于 |
| 整型 | 是 | 每个 shingle 的最大词元数。必须大于或等于 |
例子
以下关于 分钟 集合中的 page_updated_by.email
字段的索引定义示例使用两个自定义分析器 emailAutocompleteIndex
和 emailAutocompleteSearch
来实现类似自动完成的功能。MongoDB Search 在索引创建期间使用 emailAutocompleteIndex
分析器执行以下操作:
将字段中的
@
字符替换为AT
使用空格分词器创建词元
Shingle 词元
创建这些shingle 词元的 edgeGram
MongoDB Search 在搜索期间使用 emailAutocompleteSearch
分析器执行以下操作:
将字段中的
@
字符替换为AT
使用空格分词器创建词元
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
emailAutocompleteIndex
。展开 Character Filters,然后单击 Add character filter。
从下拉菜单中选择 mapping,然后点击 Add mapping。
输入以下键和值:
键值@
AT
单击 Add character filter 可将字符筛选器添加到自定义分析器中。
如果 Tokenizer 已折叠,请将其展开。
从下拉菜单中选择 whitespace,并在 maxTokenLength 字段中输入
15
。展开 Token Filters,然后单击 Add token filter。
从下拉菜单中选择 shingle,然后配置以下字段。
字段字段值minShingleSize
2
minShingleSize
3
单击 Add token filter 可添加另一个词元筛选器。
单击 Add token filter 可添加另一个词元筛选器。
从下拉菜单中选择 edgeGram 并为词元筛选器配置以下字段:
字段字段值minGram
2
maxGram
15
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add,将自定义分析器添加到索引。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
emailAutocompleteSearch
。展开 Character Filters,然后单击 Add character filter。
从下拉菜单中选择 mapping,然后点击 Add mapping。
输入以下键和值:
键值@
AT
单击 Add character filter 可将字符筛选器添加到自定义分析器中。
如果 Tokenizer 已折叠,请将其展开。
从下拉菜单中选择 whitespace,并在 maxTokenLength 字段中输入
15
。单击 Add,将自定义分析器添加到索引。
在 Field Mappings 部分中,单击 Add Field Mapping 可对 page_updated_by.email 嵌套字段应用自定义分析器。
从 Field Name 下拉菜单中选择嵌套的 page_updated_by.email,并从 Data Type 下拉菜单中选择 String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 下拉菜单中选择
emailAutocompleteIndex
,并从 Search Analyzer 下拉菜单中选择emailAutocompleteSearch
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
1 { 2 "analyzer": "lucene.keyword", 3 "mappings": { 4 "dynamic": true, 5 "fields": { 6 "page_updated_by": { 7 "type": "document", 8 "fields": { 9 "email": { 10 "type": "string", 11 "analyzer": "emailAutocompleteIndex", 12 "searchAnalyzer": "emailAutocompleteSearch" 13 } 14 } 15 } 16 } 17 }, 18 "analyzers": [ 19 { 20 "name": "emailAutocompleteIndex", 21 "charFilters": [ 22 { 23 "mappings": { 24 "@": "AT" 25 }, 26 "type": "mapping" 27 } 28 ], 29 "tokenizer": { 30 "maxTokenLength": 15, 31 "type": "whitespace" 32 }, 33 "tokenFilters": [ 34 { 35 "maxShingleSize": 3, 36 **** "minShingleSize": 2, 37 "type": "shingle" 38 }, 39 { 40 "maxGram": 15, 41 "minGram": 2, 42 "type": "edgeGram" 43 } 44 ] 45 }, 46 { 47 "name": "emailAutocompleteSearch", 48 "charFilters": [ 49 { 50 "mappings": { 51 "@": "AT" 52 }, 53 "type": "mapping" 54 } 55 ], 56 "tokenizer": { 57 "maxTokenLength": 15, 58 "type": "whitespace" 59 } 60 } 61 ] 62 }
db.minutes.createSearchIndex("default", { "analyzer": "lucene.keyword", "mappings": { "dynamic": true, "fields": { "page_updated_by": { "type": "document", "fields": { "email": { "type": "string", "analyzer": "emailAutocompleteIndex", "searchAnalyzer": "emailAutocompleteSearch" } } } } }, "analyzers": [ { "name": "emailAutocompleteIndex", "charFilters": [ { "mappings": { "@": "AT" }, "type": "mapping" } ], "tokenizer": { "maxTokenLength": 15, "type": "whitespace" }, "tokenFilters": [ { "maxShingleSize": 3, "minShingleSize": 2, "type": "shingle" }, { "maxGram": 15, "minGram": 2, "type": "edgeGram" } ] }, { "name": "emailAutocompleteSearch", "charFilters": [ { "mappings": { "@": "AT" }, "type": "mapping" } ], "tokenizer": { "maxTokenLength": 15, "type": "whitespace" } } ] })
以下查询在 minutes 集合的 page_updated_by.email
字段中搜索电子邮件地址:
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "index": "default", "text": { "query": "auerbach@ex", "path": "page_updated_by.email" } } } SCORE: 0.8824931383132935 _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
db.minutes.aggregate([ { "$search": { "index": "default", "text": { "query": "auerbach@ex", "path": "page_updated_by.email" } } }, { "$project": { "_id": 1, "text.es_MX": 1 } } ])
[ { _id: 1, page_updated_by: { email: 'auerbach@example.com' } } ]
MongoDB Search 使用 emailAutocompleteSearch
分析器创建搜索词元,然后将其与使用 emailAutocompleteIndex
分析器创建的索引词元进行匹配。下表显示了MongoDB Search 创建的搜索词元和索引词元(最多 15 个字符):
搜索词元 | Index Tokens |
---|---|
|
|
snowballStemming
snowballStemming
词元使用 Snowball 生成的词干分析器筛选 Stems 词元。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
| 字符串 | 是 | 以下值是有效的:
|
例子
以下索引定义使用名为 frenchStemmer
的自定义分析器对 minutes 集合中的 text.fr_CA
字段进行索引。自定义分析器指定以下内容:
应用标准分词器,以根据分词规则创建词元。
将以下词元筛选器应用于词元:
lowercase 词元过滤器,用于将词元转换为小写。
french
用于提取词语的词干的snowballStemming
词元筛选器变体。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
frenchStemmer
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 standard。
展开 Token Filters,然后单击 Add token filter。
从下拉列表中选择 lowercase,然后单击 Add token filter 以将词元过滤器添加到您的自定义分析器中。
单击 Add token filter 可添加另一个词元筛选器。
从下拉菜单中选择 snowballStemming,然后从 stemmerName 下拉菜单中选择
french
。单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分中,单击 Add Field Mapping 可对 text.fr_CA嵌套字段应用自定义分析器。
选择 text.fr_CA(嵌套在 Field Name 下拉菜单中),然后从 Data Type 下拉菜单中选择 String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
frenchStemmer
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "mappings": { "fields": { "text": { "type": "document", "fields": { "fr_CA": { "type": "string", "analyzer": "frenchStemmer" } } } } }, "analyzers": [ { "name": "frenchStemmer", "charFilters": [], "tokenizer": { "type": "standard" }, "tokenFilters": [ { "type": "lowercase" }, { "type": "snowballStemming", "stemmerName": "french" } ] } ] }
db.minutes.createSearchIndex("default", { "mappings": { "fields": { "text": { "type": "document", "fields": { "fr_CA": { "type": "string", "analyzer": "frenchStemmer" } } } } }, "analyzers": [ { "name": "frenchStemmer", "charFilters": [], "tokenizer": { "type": "standard" }, "tokenFilters": [ { "type": "lowercase" }, { "type": "snowballStemming", "stemmerName": "french" } ] } ] })
以下查询在 minutes 集合的 text.fr_CA
字段中搜索词语 réunion
。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "index": "default", "text": { "query": "réunion", "path": "text.fr_CA" } } } SCORE: 0.13076457381248474 _id: "1" message: "try to siGn-In" page_updated_by: Object 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"
db.minutes.aggregate([ { "$search": { "index": "default", "text": { "query": "réunion", "path": "text.fr_CA" } } }, { "$project": { "_id": 1, "text.fr_CA": 1 } } ])
[ { _id: 1, text: { fr_CA: 'Cette page traite des réunions de département' } } ]
MongoDB Search 返回结果中包含 _id: 1
的文档。MongoDB Search 将查询术语与文档进行匹配,因为它为文档创建了以下词元,然后使用这些词元与查询术语réunion
进行匹配:
文档 ID | 输出词元 |
---|---|
|
|
spanishPluralStemming
spanishPluralStemming
词元筛选器提取西班牙语复数词语的词干。它要求使用小写文本。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
例子
以下索引定义使用名为 spanishPluralStemmer
的自定义分析器对 minutes 集合中的 text.es_MX
字段进行索引。自定义分析器指定以下内容:
应用标准分词器,以根据分词规则创建词元。
将以下词元筛选器应用于词元:
lowercase 词元过滤器,用于将西班牙语词语转换为小写。
spanishPluralStemming
词元筛选器,用于为词元中的复数西班牙语词语提取单数形式的词干。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
spanishPluralStemmer
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 standard。
展开 Token Filters,然后单击 Add token filter。
从下拉列表中选择 lowercase,然后单击 Add token filter 以将词元过滤器添加到您的自定义分析器中。
单击 Add token filter 可添加另一个词元筛选器。
从下拉列表中选择 spanishPluralStemming。
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分中,单击 Add Field Mapping 可对 text.es_MX嵌套字段应用自定义分析器。
选择 text.es_MX(嵌套在 Field Name 下拉菜单中),然后从 Data Type 下拉菜单中选择 String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
spanishPluralStemmer
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "analyzer": "spanishPluralStemmer", "mappings": { "fields": { "text: { "type": "document", "fields": { "es_MX": { "analyzer": "spanishPluralStemmer", "searchAnalyzer": "spanishPluralStemmer", "type": "string" } } } } }, "analyzers": [ { "name": "spanishPluralStemmer", "charFilters": [], "tokenizer": { "type": "standard" }, "tokenFilters": [ { "type": "lowercase" }, { "type": "spanishPluralStemming" } ] } ] }
db.minutes.createSearchIndex("default", { "analyzer": "spanishPluralStemmer", "mappings": { "fields": { "text": { "type": "document", "fields": { "es_MX": { "analyzer": "spanishPluralStemmer", "searchAnalyzer": "spanishPluralStemmer", "type": "string" } } } } }, "analyzers": [ { "name": "spanishPluralStemmer", "charFilters": [], "tokenizer": { "type": "standard" }, "tokenFilters": [ { "type": "lowercase" }, { "type": "spanishPluralStemming" } ] } ] })
以下查询在 minutes 集合的 text.es_MX
字段中搜索西班牙语词语 punto
。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "index": "default", "text": { "query": "punto", "path": "text.es_MX" } } } SCORE: 0.13076457381248474 _id: "4" message: "write down your signature or phone №" page_updated_by: Object 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."
db.minutes.aggregate([ { "$search": { "index": "default", "text": { "query": "punto", "path": "text.es_MX" } } }, { "$project": { "_id": 1, "text.es_MX": 1 } } ])
[ { _id: 4, text : { es_MX: 'La página ha sido actualizada con los puntos de la agenda.', } } ]
MongoDB Search 返回包含 _id: 4
的文档,因为文档中的 text.es_MX
字段包含复数术语puntos
。MongoDB搜索将此文档与查询术语punto
进行匹配,因为MongoDB搜索通过从术语中去除复数 (s
),将 puntos
作为 punto
进行分析。具体来说, MongoDB Search 会为结果中的文档创建以下词元(可搜索词语),然后使用这些词元与查询术语进行匹配:
文档 ID | 输出词元 |
|
|
stempel
stempel
词元筛选器使用 Lucene 的 默认波兰语词干分析器表提取波兰语词语的词干。它要求使用小写文本。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
例子
以下索引定义使用名为 stempelStemmer
的自定义分析器对 minutes 集合中的 text.pl_PL
字段进行索引。自定义分析器指定以下内容:
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
stempelAnalyzer
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 standard。
展开 Token Filters,然后单击 Add token filter。
从下拉列表中选择 lowercase,然后单击 Add token filter 以将词元过滤器添加到您的自定义分析器中。
单击 Add token filter 可添加另一个词元筛选器。
从下拉列表中选择 spanishPluralStemming。
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分中,单击 Add Field Mapping 以对标题字段应用自定义分析器。
从 Field Name 下拉列表中选择 Title(标题),然后从 Data Type 下拉列表中选择 String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
stempelAnalyzer
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "mappings": { "fields": { "title": { "type": "string", "analyzer": "stempelAnalyzer" } } }, "analyzers": [ { "name": "stempelAnalyzer", "tokenizer": { "type": "standard" }, "tokenFilters": [ { "type": "stempel" } ] } ] }
db.minutes.createSearchIndex("default", { "analyzer": "stempelStemmer", "mappings": { "dynamic": true, "fields": { "text.pl_PL": { "analyzer": "stempelStemmer", "searchAnalyzer": "stempelStemmer", "type": "string" } } }, "analyzers": [ { "name": "stempelStemmer", "charFilters": [], "tokenizer": { "type": "standard" }, "tokenFilters": [ { "type": "lowercase" }, { "type": "stempel" } ] } ] })
以下查询在 minutes 集合的 text.pl_PL
字段中搜索波兰语词语 punkt
。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "index": "default", "text": { "query": "punkt", "path": "text.pl_PL" } } } SCORE: 0.5376965999603271 _id: "4" text: Object pl_PL: "Strona została zaktualizowana o punkty porządku obrad."
db.minutes.aggregate([ { "$search": { "index": "default", "text": { "query": "punkt", "path": "text.pl_PL" } } }, { "$project": { "_id": 1, "text.pl_PL": 1 } } ])
[ { _id: 4, text: { pl_PL: 'Strona została zaktualizowana o punkty porządku obrad.' } } ]
MongoDB Search 返回包含 _id: 4
的文档,因为文档中的 text.pl_PL
字段包含复数术语punkty
。MongoDB搜索将此文档与查询术语punkt
进行匹配,因为MongoDB搜索通过从术语中去除复数 (y
),将 punkty
作为 punkt
进行分析。具体来说, MongoDB Search 会为结果中的文档创建以下词元(可搜索词语),然后将其与查询术语进行匹配:
文档 ID | 输出词元 |
---|---|
|
|
stopword
stopword
词元筛选器删除与指定停用词对应的词元。该词元筛选器不分析指定的停用词。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
| 字符串数组 | 是 | 该列表包含与要删除的词元对应的停用词。值必须是一个或多个停用词。 |
| 布尔 | no | 该标记指示在筛选要删除的词元时是否忽略停用词大小写。值可以是以下值之一:
默认值: |
例子
以下索引定义使用名为 stopwordRemover
的自定义分析器对 minutes 集合中的 title
字段进行索引。自定义分析器指定以下内容:
应用空格分词器,以根据词语之间出现的空格创建词元。
应用
stopword
词元过滤器删除与定义的停用词is
、the
和at
匹配的标记。词元过滤器不区分大小写,并将删除与指定停用词匹配的所有词元。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
stopwordRemover
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 whitespace。
展开 Token Filters,然后单击 Add token filter。
从下拉菜单中选择 stopword,然后在 tokens 字段中输入以下内容:
is
,the
,at
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分中,单击 Add Field Mapping 可对 text.en_US嵌套字段应用自定义分析器。
选择 text.en_US(嵌套在 Field Name 下拉菜单中),然后从 Data Type 下拉菜单中选择 String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
stopwordRemover
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "mappings": { "fields": { "text": { "type" : "document", "fields": { "en_US": { "type": "string", "analyzer": "stopwordRemover" } } } } }, "analyzers": [ { "name": "stopwordRemover", "charFilters": [], "tokenizer": { "type": "whitespace" }, "tokenFilters": [ { "type": "stopword", "tokens": ["is", "the", "at"] } ] } ] }
1 db.minutes.createSearchIndex( 2 "default", 3 { 4 "mappings": { 5 "fields": { 6 "text": { 7 "type": "document", 8 "fields": { 9 "en_US": { 10 "type": "string", 11 "analyzer": "stopwordRemover" 12 } 13 } 14 } 15 } 16 }, 17 "analyzers": [ 18 { 19 "name": "stopwordRemover", 20 "charFilters": [], 21 "tokenizer": { 22 "type": "whitespace" 23 }, 24 "tokenFilters": [ 25 { 26 "type": "stopword", 27 "tokens": ["is", "the", "at"] 28 } 29 ] 30 } 31 ] 32 } 33 )
以下查询在分钟集合的 text.en_US
字段中搜索短语 head of the sales
。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "phrase": { "query": "head of the sales", "path": "text.en_US" } } } SCORE: 1.5351942777633667 _id: "2" message: "do not forget to SIGN-IN. See ① for details." page_updated_by: Object text: Object
1 db.minutes.aggregate([ 2 { 3 "$search": { 4 "phrase": { 5 "query": "head of the sales", 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.' } } ]
MongoDB Search 返回包含 _id: 2
的文档,因为 en_US
字段包含查询术语。MongoDB Search 在分析期间不会为文档中的停用词 the
创建词元,但仍能将其与查询术语进行匹配,因为对于 string
字段,它还会使用索引分析器分析查询术语(或者如果指定,使用 searchAnalyzer
),并从查询术语中删除停用词,这允许MongoDB Search 将查询术语与文档进行匹配。具体来说, MongoDB Search 会为结果中的文档创建以下词元:
文档 ID | 输出词元 |
---|---|
|
|
trim
trim
词元过滤器会删除词元中的前导和尾随空格。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
例子
以下索引定义使用名为 tokenTrimmer
的自定义分析器对 minutes 集合中的 text.en_US
进行索引。该自定义分析器指定以下内容:
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
tokenTrimmer
。展开 Character Filters,然后单击 Add character filter。
从下拉菜单中选择 htmlStrip,然后在 ignoredTags 字段中输入
a
。单击 Add character filter 可将字符筛选器添加到自定义分析器中。
如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 keyword。
展开 Token Filters,然后单击 Add token filter。
从下拉列表中选择 trim。
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add,将自定义分析器添加到索引。
在 Field Mappings 部分中,单击 Add Field Mapping 可对 text.en_US嵌套字段应用自定义分析器。
选择 text.en_US(嵌套在 Field Name 下拉菜单中),然后从 Data Type 下拉菜单中选择 String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
tokenTrimmer
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "mappings": { "fields": { "text": { "type": "document", "fields": { "en_US": { "type": "string", "analyzer": "tokenTrimmer" } } } } }, "analyzers": [ { "name": "tokenTrimmer", "charFilters": [{ "type": "htmlStrip", "ignoredTags": ["a"] }], "tokenizer": { "type": "keyword" }, "tokenFilters": [ { "type": "trim" } ] } ] }
db.minutes.createSearchIndex("default", { "mappings": { "fields": { "text": { "type": "document", "fields": { "en_US": { "type": "string", "analyzer": "tokenTrimmer" } } } } }, "analyzers": [ { "name": "tokenTrimmer", "charFilters": [{ "type": "htmlStrip", "ignoredTags": ["a"] }], "tokenizer": { "type": "keyword" }, "tokenFilters": [ { "type": "trim" } ] } ] })
以下查询在 minutes 集合的 text.en_US
字段中搜索前后具有任意数量的其他字符的短语 *department meetings*
。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "wildcard": { "query": "*department meetings*", "path": "text.en_US", "allowAnalyzedField": true } } } SCORE: 1 _id: "1" message: "try to siGn-In" page_updated_by: Object 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"
1 db.minutes.aggregate([ 2 { 3 "$search": { 4 "wildcard": { 5 "query": "*department meetings*", 6 "path": "text.en_US", 7 "allowAnalyzedField": true 8 } 9 } 10 }, 11 { 12 "$project": { 13 "_id": 1, 14 "text.en_US": 1 15 } 16 } 17 ])
1 [ 2 { 3 _id: 1, 4 text: { en_US: '<head> This page deals with department meetings. </head>' } 5 } 6 ]
MongoDB Search 返回包含 _id: 1
的文档,因为 en_US
字段包含查询术语department meetings
。MongoDB Search 为结果中的文档创建以下词元,这表明MongoDB Search 删除了 HTML 标记,为整个字符串创建了单个词元,并删除了词元中的前导和尾随空格:
文档 ID | 输出词元 |
---|---|
|
|
wordDelimiterGraph
wordDelimiterGraph
词元筛选器根据配置的规则将词元拆分为子词元。我们建议您不要将该词元筛选器与标准分词器一起使用,因为该分词器删除该词元筛选器用于确定边界的很多词语内分隔符。
注意
对于使用regex
(MongoDB搜索操作符)或wildcard
操作符进行查询,您不能将 wordDelimiterGraph
词元过滤用作 searchAnalyzer
,因为它为每个输入词元生成多个输出词元。在索引定义中指定不同的分析器作为 searchAnalyzer
。
属性
它具有以下属性:
名称 | 类型 | 必需? | 说明 |
---|---|---|---|
| 字符串 | 是 | 标识该词元筛选器类型的人类可读标签。值必须是 |
| 对象 | no | 该对象包含确定如何将词语拆分为子词语的规则。 默认值: |
delimiterOptions .generateWordParts | 布尔 | no | 该标记指示是否根据子词语拆分词元。例如,如果为 默认值: |
delimiterOptions .generateNumberParts | 布尔 | no | 该标记指示是否根据子编号拆分词元。例如,如果为 默认值: |
delimiterOptions .concatenateWords | 布尔 | no | 该标记指示是否连接子词语串。例如,如果为 默认值: |
delimiterOptions .concatenateNumbers | 布尔 | no | 该标记指示是否连接子数字串。例如,如果为 默认值: |
delimiterOptions .concatenateAll | 布尔 | no | 该标记指示是否连接所有内容。例如,如果为 默认值: |
delimiterOptions .preserveOriginal | 布尔 | no | 该标记指示是否生成原始词语的词元。 默认值: |
delimiterOptions .splitOnCaseChange | 布尔 | no | 该标记指示是否根据字母大小写转换拆分词元。例如,如果为 默认值: |
delimiterOptions .splitOnNumerics | 布尔 | no | 该标记指示是否根据字母-数字转换拆分词元。例如,如果为 默认值: |
delimiterOptions .stemEnglishPossessive | 布尔 | no | 该标记指示是否从每个子词语中删除尾随所有格。例如,如果为 默认值: |
delimiterOptions .ignoreKeywords | 布尔 | no | 指示是否跳过将 默认值: |
| 对象 | no | 该对象包含保护字选项。 默认值: |
protectedWords .words | 阵列 | 视条件而定 | 该列表包含要禁止分隔的词元。如果您指定 |
protectedWords .ignoreCase | 布尔 | no | 该标记指示是否忽略保护字区分大小写。 默认值: |
如果为 true
,则在该选项后面应用 flattenGraph 词元过滤器,以使词元流适合进行索引。
例子
以下索引定义使用名为 wordDelimiterGraphAnalyzer
的自定义分析器对 minutes 集合中的 title
字段进行索引。自定义分析器指定以下内容:
应用空格分词器,以根据词语之间出现的空格创建词元。
为以下内容应用 wordDelimiterGraph 词元过滤器:
不要尝试拆分
is
、the
和at
。排除区分大小写。例如,不会排除Is
和tHe
。在大小写发生变化时拆分词元,并删除仅包含英语字母表中的字母的词元。
在 Custom Analyzers 部分中,单击 Add Custom Analyzer。
选择 Create Your Own 单选按钮并单击 Next。
在 Analyzer Name 字段中输入
wordDelimiterGraphAnalyzer
。如果 Tokenizer 已折叠,请将其展开。
从下拉列表中选择 whitespace。
展开 Token Filters,然后单击 Add token filter。
从下拉列表中选择 lowercase,然后单击 Add token filter 以将词元过滤器添加到您的自定义分析器中。
单击 Add token filter 可添加另一个词元筛选器。
从下拉菜单中选择 wordDelimiterGraph,然后配置以下字段:
取消选择 delimiterOptions.generateWordParts 然后选择 delimiterOptions.splitOnCaseChange。
在
protectedWords.words
字段中键入并从下拉菜单中选择单词is
、the
和at
,一次一个。取消选择
protectedWords.ignoreCase
。
单击 Add token filter 可将词元筛选器添加到自定义分析器中。
单击 Add 可创建自定义分析器。
在 Field Mappings 部分中,单击 Add Field Mapping 可对 title(标题)嵌套字段应用自定义分析器。
从 Field Name 下拉菜单中选择嵌套的 title(标题),并从 Data Type 下拉菜单中选择 String(字符串)。
在数据类型的属性部分中,从 Index Analyzer 和 Search Analyzer 下拉菜单中选择
wordDelimiterGraphAnalyzer
。单击 Add,然后单击 Save Changes。
将默认索引定义替换为以下示例:
{ "mappings": { "fields": { "title": { "type": "string", "analyzer": "wordDelimiterGraphAnalyzer" } } }, "analyzers": [ { "name": "wordDelimiterGraphAnalyzer", "charFilters": [], "tokenizer": { "type": "whitespace" }, "tokenFilters": [ { "type": "wordDelimiterGraph", "protectedWords": { "words": ["is", "the", "at"], "ignoreCase": false }, "delimiterOptions" : { "generateWordParts" : false, "splitOnCaseChange" : true } } ] } ] }
db.minutes.createSearchIndex("default", { "mappings": { "fields": { "title": { "type": "string", "analyzer": "wordDelimiterGraphAnalyzer" } } }, "analyzers": [ { "name": "wordDelimiterGraphAnalyzer", "charFilters": [], "tokenizer": { "type": "whitespace" }, "tokenFilters": [ { "type": "wordDelimiterGraph", "protectedWords": { "words": ["is", "the", "at"], "ignoreCase": false }, "delimiterOptions" : { "generateWordParts" : false, "splitOnCaseChange" : true } } ] } ] })
以下查询在 minutes 集合的 title
字段中搜索词语 App2
。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
{ "$search": { "index": "default", "text": { "query": "App2", "path": "title" } } } SCORE: 0.5104123950004578 _id: "4" message: "write down your signature or phone №" page_updated_by: Object text: Object
db.minutes.aggregate([ { "$search": { "index": "default", "text": { "query": "App2", "path": "title" } } }, { "$project": { "_id": 1, "title": 1 } } ])
[ { _id: 4, title: 'The daily huddle on tHe StandUpApp2' } ]
MongoDB Search 返回包含 _id: 4
的文档,因为文档中的 title
字段包含 App2
。MongoDB Search 在大小写发生变化时拆分词元,并删除由分割创建的仅包含字母的词元。它还使用索引分析器(如果指定,则使用 searchAnalyzer
)分析查询术语,以便在大小写发生变化时分割单词并删除2
前面的字母。具体来说, MongoDB Search 会为 protectedWords
和 delimiterOptions
选项包含 _id : 4
的文档创建以下词元:
wordDelimiterGraph 选项 | 输出词元 |
---|---|
|
|
|
|