standard分析器是所有MongoDB Search 索引和查询的默认分析器。它根据单词边界将文本划分为术语,因此在大多数使用案例中都与语言无关。它将所有术语转换为小写并删除标点符号。它提供基于语法的分词功能,可识别电子邮件地址、首字母缩略词、中日韩字符、字母数字等。
如果您选择 Refine Your Index, Atlas用户界面会在 Index Configurations 部分中显示标题为 View text analysis of your selected index configuration 的部分。如果展开此部分, Atlas用户界面会显示 standard分析器为每个示例字符串生成的索引和搜索词元。 当您在Atlas用户界面Visual Editor 中创建或编辑索引时,您可以看到 standard分析器为内置示例文档和查询字符串创建的词元。
重要
MongoDB Search 不会对分析器词元大小超过 32766 字节的字符串字段索引。 如果使用关键字分析器,则不会对超过 32766 字节的字符串字段编制索引。
例子
以下示例索引定义使用title standard分析器指定 sample_mflix.movies集合中的 字段上的索引。要跟随此示例,请在集群上加载示例数据,并使用mongosh 或导航到Atlas用户界面中的Create a Search
Index 页面,按照创建MongoDB搜索索引教程中的步骤操作。
然后,使用 movies 集合作为数据源,按照示例过程从 mongosh 或 Atlas 用户界面 Visual Editor 或 JSON editor 创建索引。
➤ 使用选择语言下拉菜单为此页面上的示例设立界面。
单击 Refine Your Index 配置索引。
在 Index Configurations 部分中,将 Dynamic Mapping 切换为 off。
在 Field Mappings 部分中,单击 Add Field Mapping 打开 Add Field Mapping 窗口。
单击 Customized Configuration(连接)。
从 Field Name 下拉列表中选择
title。单击 Data Type 下拉列表并选择 String(如果尚未选择)。
展开 String Properties 并进行以下更改:
索引分析器
如果尚未选择,请从下拉菜单中选择
lucene.standard。Search Analyzer
如果尚未选择,请从下拉菜单中选择
lucene.standard。索引选项
使用默认
offsets。Store
使用默认
true。忽略以上内容
保留默认设置。
规范
使用默认
include。单击 Add(连接)。
单击 Save Changes(连接)。
单击 Create Search Index(连接)。
将默认索引定义替换为以下索引定义。
{ "mappings": { "fields": { "title": { "type": "string", "analyzer": "lucene.standard" } } } } 单击 Next(连接)。
单击 Create Search Index(连接)。
1 db.movies.createSearchIndex( 2 "default", 3 { 4 "mappings": { 5 "fields": { 6 "title": { 7 "type": "string", 8 "analyzer": "lucene.standard" 9 } 10 } 11 } 12 } 13 )
以下查询在 title 字段中搜索词语 action 并将输出限制为两个结果。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
[ { "$search": { "text": { "query": "action", "path": "title" } } } ] SCORE: 4.001246452331543 _id: "573a1398f29313caabceac7f" awards: Object cast: Array (4) countries: Array (1) directors: Array (1) fullplot: "Jericho "Action" Jackson is a Detroit police sergeant who was demoted …" genres: Array (3) imdb: Object languages: Array (1) lastupdated: "2015-08-20 00:24:08.470000000" num_mflix_comments: 2 plot: "Vengence drives a tough Detroit cop to stay on the trail of a power hu…" poster: "https://m.media-amazon.com/images/M/MV5BZWFhNmI3OWQtOTU5Zi00ODA3LWExNj…" rated: "R" released: 1988-02-12T00:00:00.000+00:00 runtime: 96 title: "Action Jackson" tomatoes: Object type: "movie" writers: Array (1) year: 1988 SCORE: 4.001246452331543 _id: "573a1399f29313caabcec6b7" awards: Object cast: Array (4) countries: Array (1) directors: Array (1) fullplot: "Jeb Ward is an attorney who specializes in whistle blower, David vs. Goliath cases. When a major auto manufacturer is accused of knowingly marketing a defective car, Ward takes on the case, risking everything." genres: Array (2) imdb: Object languages: Array (1) lastupdated: "2015-09-06 00:42:51.167000000" metacritic: 58 num_mflix_comments: 2 plot: "Jeb Ward is an attorney who specializes in whistle blower, David vs. Goliath cases." poster: "https://m.media-amazon.com/images/M/MV5BNWY5Mjk4ZmItMTAzYS00NWE3LWEzYz…" rated: "R" released: 1991-03-15T00:00:00.000+00:00 runtime: 110 title: "Class Action" tomatoes: Object type: "movie" writers: Array (3) year: 1991 SCORE: 4.001246452331543 _id: "573a13b0f29313caabd333e7" awards: Object cast: Array (4) countries: Array (2) directors: Array (1) fullplot: "Vishi is a local goon who basically likes beating up people to get his way. He falls in love with Khushi, but his life takes a turn when he crosses paths with a ruthless mafia kingpin." genres: Array (3) imdb: Object languages: Array (1) lastupdated: "2015-09-10 17:20:55.697000000" num_mflix_comments: 1 plot: "In order to escape crime and clutches of a ruthless mafia, a Bangkok based goon seeks help from his lookalike, a Mumbai based criminal." released: 2014-12-05T00:00:00.000+00:00 runtime: 144 title: "Action Jackson" tomatoes: Object type: "movie" writers: Array (3) year: 2014 SCORE: 4.001246452331543 _id: "573a13d2f29313caabd913dc" awards: Object countries: Array (1) directors: Array (1) genres: Array (2) imdb: Object languages: Array (1) lastupdated: "2015-06-15 03:15:33.967000000" num_mflix_comments: 0 released: 2011-01-28T00:00:00.000+00:00 runtime: 45 title: "Slow Action" tomatoes: Object type: "movie" writers: Array (1) year: 2011 SCORE: 3.440462112426758 _id: "573a1399f29313caabcedcb0" awards: Object cast: Array (4) countries: Array (1) directors: Array (1) fullplot: "Young Danny Madigan is a big fan of Jack Slater, a larger-than-life action hero. When Danny is given a magic ticket, he is transported into Slater's latest adventure." genres: Array (3) imdb: Object languages: Array (1) lastupdated: "2015-08-31 00:11:25.920000000" metacritic: 44 num_mflix_comments: 0 plot: "With the help of a magic ticket, a young film fan is transported into the fictional world of his favorite action movie character." poster: "https://m.media-amazon.com/images/M/MV5BNjdhOGY1OTktYWJkZC00OGY5LWJhY2…" rated: "PG-13" released: 1993-06-18T00:00:00.000+00:00 runtime: 130 title: "Last Action Hero" tomatoes: Object type: "movie" writers: Array (4) year: 1993 SCORE: 3.440462112426758 _id: "573a139af29313caabcf0e84" awards: Object cast: Array (4) countries: Array (1) directors: Array (1) fullplot: "Jan Schlichtmann, a tenacious lawyer, is addressed by a group of families whose children have died from leukemia. He takes on a case against two large companies accused of dumping toxic waste." genres: Array (1) imdb: Object languages: Array (1) lastupdated: "2015-09-07 00:21:34.950000000" metacritic: 68 num_mflix_comments: 0 plot: "The families of children who died sue two companies for dumping toxic waste." poster: "https://m.media-amazon.com/images/M/MV5BZmEzNjhiZWEtNTM5OS00ZmQyLThhYj…" rated: "PG-13" released: 1999-01-08T00:00:00.000+00:00 runtime: 115 title: "A Civil Action" tomatoes: Object type: "movie" writers: Array (2) year: 1998 SCORE: 2.687220573425293 _id: "573a1398f29313caabcead32" awards: Object cast: Array (4) countries: Array (1) directors: Array (1) fullplot: "Colonel James Braddock has a Vietnamese wife who was supposed to leave Vietnam with him, but was left behind. Years later, he returns to rescue her and their son." genres: Array (2) imdb: Object languages: Array (1) lastupdated: "2015-08-17 00:28:26.627000000" num_mflix_comments: 0 plot: "Colonel James Braddock has a Vietnamese wife who was supposed to leave Vietnam with him, but was left behind." poster: "https://m.media-amazon.com/images/M/MV5BZTRjODU0MTUtMjBmMi00ZTBmLTk2MD…" rated: "R" released: 1988-03-02T00:00:00.000+00:00 runtime: 101 title: "Braddock: Missing in Action III" tomatoes: Object type: "movie" writers: Array (5) year: 1988 SCORE: 2.687220573425293 _id: "573a13a9f29313caabd1f502" awards: Object cast: Array (4) countries: Array (2) directors: Array (1) fullplot: "Bugs Bunny and Daffy Duck are up to their feuding ways again. Tired of all the attention going to Bugs, Daffy persuades the studio to let him go off on his own." genres: Array (3) imdb: Object languages: Array (1) lastupdated: "2015-08-28 00:30:31.080000000" metacritic: 64 num_mflix_comments: 1 plot: "The Looney Tunes search for a man's missing father and the mythical Blue Monkey diamond." poster: "https://m.media-amazon.com/images/M/MV5BMTkxNDk5MDQ2MF5BMl5BanBnXkFtZT…" rated: "PG" released: 2003-11-14T00:00:00.000+00:00 runtime: 91 title: "Looney Tunes: Back in Action" tomatoes: Object type: "movie" writers: Array (1) year: 2003 SCORE: 2.4220800399780273 _id: "573a13bdf29313caabd5acfe" awards: Object cast: Array (4) countries: Array (1) directors: Array (1) fullplot: "Fueled by the belief that another world is possible, acclaimed filmmaker Velcrow Ripper takes us on a journey through the spiritual activism movement." genres: Array (1) imdb: Object languages: Array (1) lastupdated: "2015-08-12 00:50:56" num_mflix_comments: 1 plot: "Captures the exciting movement of Spiritual Activism that is exploding around the planet." poster: "https://m.media-amazon.com/images/M/MV5BMTI2MjM2Mzk2NV5BMl5BanBnXkFtZT…" released: 2008-10-01T00:00:00.000+00:00 runtime: 90 title: "Fierce Light: When Spirit Meets Action" tomatoes: Object type: "movie" writers: Array (1) year: 2008
db.movies.aggregate([ { "$search": { "text": { "query": "action", "path": "title" } } }, { "$limit": 2 }, { "$project": { "_id": 0, "title": 1 } } ])
[ { title: 'Action Jackson' }, { title: 'Class Action' } ]
MongoDB Search 返回这些文档,因为它将查询术语action 与文档的词元 action 相匹配, MongoDB Search 通过使用 lucene.standard分析器对 title字段中的文本执行以下操作来创建这些文档:
将文本转换为小写。
根据单词边界分割文本并创建独立令牌。
下表显示了MongoDB Search 使用标准分析器创建的词元(可搜索词语),以及MongoDB Search 为关键字分析器和空格分析器为结果中的文档创建的词元:
标题 | 标准分析器词元 | 关键字分析器词元 | 空白分析器令牌 |
|---|---|---|---|
|
|
|
|
|
|
|
|
如果使用以下分析器对字段编制索引: