keyword
分析器接受字符串或字符串数组以作为参数,并将它们作为单个词语(词元)进行索引。仅返回与字段精确匹配的结果。它将所有文本保留为原始字母大小写。
如果您选择 Refine Your Index, Atlas用户界面会在 Index Configurations 部分中显示标题为 View text analysis of your selected index configuration 的部分。如果展开此部分, Atlas用户界面会显示 keyword
分析器为每个示例字符串生成的索引和搜索词元。 当您在Atlas用户界面Visual Editor 中创建或编辑索引时,您可以看到 keyword
分析器为内置示例文档和查询字符串创建的词元。
重要
MongoDB Search 不会对分析器词元大小超过 32766 字节的字符串字段索引。 如果使用关键字分析器,则不会对超过 32766 字节的字符串字段编制索引。
例子
以下示例索引定义使用title
keyword
分析器指定 sample_mflix.movies集合中 字段的索引。要跟随此示例,请在集群上加载示例数据,并使用mongosh
或导航到Atlas用户界面中的Create a Search
Index 页面,按照创建MongoDB搜索索引教程中的步骤操作。
然后,使用 movies
集合作为数据源,按照示例过程从 mongosh
或 Atlas 用户界面 Visual Editor 或 JSON editor 创建索引。
➤ 使用 Select your language(选择您的语言)下拉菜单设置此页面上示例的语言。
单击 Refine Your Index 配置索引。
在 Index Configurations 部分中,将 Dynamic Mapping 切换为 off。
在 Field Mappings 部分中,单击 Add Field 打开 Add Field Mapping 窗口。
单击 Customized Configuration(连接)。
从 Field Name 下拉列表中选择
title
。单击 Data Type 下拉列表并选择 String(如果尚未选择)。
展开 String Properties 并进行以下更改:
索引分析器
从下拉列表中选择
lucene.keyword
。Search Analyzer
从下拉列表中选择
lucene.keyword
。索引选项
使用默认
offsets
。Store
使用默认
true
。忽略以上内容
保留默认设置。
规范
使用默认
include
。单击 Add(连接)。
单击 Save Changes(连接)。
单击 Create Search Index(连接)。
将默认索引定义替换为以下索引定义。
{ "mappings": { "fields": { "title": { "type": "string", "analyzer": "lucene.keyword" } } } } 单击 Next(连接)。
单击 Create Search Index(连接)。
1 db.movies.createSearchIndex( 2 "default", 3 { 4 "mappings": { 5 "fields": { 6 "title": { 7 "type": "string", 8 "analyzer": "lucene.keyword" 9 } 10 } 11 } 12 } 13 )
以下查询在 title
字段中搜索短语 Class Action
。
单击索引的 Query 按钮。
单击 Edit Query 编辑查询。
单击查询栏上的 并选择数据库和集合。
将默认查询替换为以下内容,然后单击 Find:
[ { "$search": { "text": { "query": "Class Action", "path": "title" } } } ] SCORE: 4.346973419189453 _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. G…" 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. G…" 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
db.movies.aggregate([ { "$search": { "text": { "query": "Class Action", "path": "title" } } }, { "$project": { "_id": 0, "title": 1 } } ])
[ { title: 'Class Action' } ]
MongoDB Search 返回文档,因为它将查询术语Class
Action
与使用 lucene.keyword
分析器为字段中的文本创建的单个词元 Class Action
进行匹配。相比之下, MongoDB Search 不会为以下查询返回任何结果:
db.cases.aggregate([ { "$search": { "text": { "query": "action", "path": "title" } } } ])
集合中的许多文档都包含字符串 action
,但 keyword
分析器仅匹配搜索术语与字段的全部内容精确匹配的文档。 对于前面的查询,keyword
分析器不会返回任何结果。但是,如果您使用标准分析器或简单分析器对该字段进行索引, MongoDB Search 将在结果中返回多个文档,包括标题字段值为 Class
Action
的文档,因为它会创建类似于以下内容的词元,而这些词元将然后匹配到查询术语:
分析器 | 输出词元 | matches action | matches Class Action |
---|---|---|---|
关键字分析器词元 |
| X | √ |
标准分析器词元 |
| √ | √ |
简单分析器词元 |
| √ | √ |