定义
text 操作符使用在索引配置中指定的分析器执行全文检索。如果忽略分析器,text 操作符将使用默认的标准分析器。
您可以使用 text 操作符来搜索单词术语的准确匹配。
语法
text 通过以下语法实现:
{ $search: { "index": <index name>, // optional, defaults to "default" "text": { "query": "<search-string>", "path": "<field-to-search>", "fuzzy": <options>, "matchCriteria": "any" | "all" "score": <options>, "synonyms": "<synonyms-mapping-name>" } } }
选项
text 使用以下词条来构造查询:
字段 | 类型 | 说明 | 必要性 |
|---|---|---|---|
| 字符串或字符串数组 | 要搜索的一个或多个字符串。 如果字符串中有多个词, MongoDB Search 还会分别为字符串中的每个术语查找匹配项。 | 必需 |
| 字符串或字符串数组 | 索引字段或要搜索的字段。您还可以指定通配符路径进行搜索。 | 必需 |
| 文档 | 启用模糊搜索。查找与搜索词相似的字符串。不能将 | Optional |
| 整型 | 匹配指定搜索词所需的最大单字符编辑数。值可以是 | Optional |
| 整型 | 结果中每个术语开头必须完全匹配的字符数。默认值为 | Optional |
| 整型 | 要生成和搜索的最大变体数量。此限制适用于每个令牌。默认值为 | Optional |
| 字符串 | 用于匹配查询中术语的条件。如果省略,则默认值为
| 可选,但建议使用 |
| 文档 | 分配给匹配搜索词结果的分数。使用以下选项之一修改分数:
查询大量值时,如果大量中的更多值与查询匹配, MongoDB Search 会分配更高的分数。 | Optional |
| 字符串 | 使用同义词运行查询时必需。 索引定义中同义词映射定义的名称。值不能是空字符串。您不能将 重要提示:要使用 使用 MongoDB搜索执行使用同义词映射的查询所需的时间取决于同义词源集合中文档的数量和大小。使用基于很少同义词文档的同义词映射的查询可能比使用基于许多同义词文档的同义词映射的查询更快。 | Optional |
评分行为
By default, the text operator uses the bm25 similarity algorithm to score documents depending on their relevance to the query.
您可以通过在 similarity.type 属性中为 MongoDB Search 索引定义中的 string 字段指定不同的相似性算法来更改所使用的相似性算法。要了解如何为 string 类型配置 MongoDB Search 索引,请参阅如何索引 String 字段。
要了解有关支持的相似度算法的更多信息,请参阅评分详情。
行为
对于使用 synonyms 而不使用 matchCriteria 的 text 查询, MongoDB Search 仅匹配包含查询词语(带或不带扩展同义词)完全匹配的文档。
这种行为将来可能会发生变化,您的查询结果也可能会发生变化。为了避免服务中断,请在使用 synonyms 时始终使用 matchCriteria。或者,请使用 phrase 操作符的同义词。
例如,假设 nyc 和 new york city 的同义词映射是等效的。对 i like nyc 的搜索将返回以下内容:
操作符规范 | 默认行为 | "matchCriteria": "any" | "matchCriteria": "all" |
|---|---|---|---|
|
|
|
|
|
|
|
|
MongoDB搜索执行使用同义词映射的查询所需的时间取决于同义词源集合中文档的数量和大小。使用基于很少同义词文档的同义词映射的查询可能比使用基于许多同义词文档的同义词映射的查询更快。
示例
The examples in this page use the movies collection in the sample_mflix database. After loading the sample dataset into your cluster, create the MongoDB Search index with dynamic mappings and run the example queries on your cluster. You must also define a synonyms mapping collection in the index as shown in the Index Definition Examples to try the synonyms examples below.
基本示例
The following MongoDB Search query uses the text operator to search the title field in the movies collection for the term surfer. It includes a $project stage to:
排除所有字段,但不包括
title添加字段
score
db.movies.aggregate([ { $search: { "text": { "path": "title", "query": "surfer" } } }, { $project: { "_id": 0, "title": 1, score: { $meta: "searchScore" } } } ])
元数据示例
以下查询在 title术语中搜索 summer字段。它使用 $searchMeta 阶段检索以下存储桶(年份)中与搜索条件匹配的电影数量:
1970,包括此存储桶的下限
1980,1970 存储桶的不含上限以及此存储桶的包含下限
1990,1980 存储桶的不含上限以及此存储桶的包含下限
2000,1990 存储桶的不含上限以及此存储桶的包含下限
2010,2000 存储桶的不含上限以及此存储桶的包含下限
2015,2010 存储桶的独占上限
db.movies.aggregate([ { "$searchMeta": { "facet": { "operator": { "text": { "path": "title", "query": "summer" } }, "facets": { "yearFacet": { "type": "number", "path": "year", "boundaries": [1970, 1980, 1990, 2000, 2010, 2020 ] } } } } } ])
模糊示例
以下查询使用 text操作符在 movies集合中的 title字段中搜索某些词,而这些词位于 query 短语 naw yark 中每个术语的一个字符变体内。MongoDB Search 会返回不同的结果,具体取决于您使用的是默认fuzzy 选项还是定义了 maxExpansions、prefixLength 或 maxEdits 字段。
单击以下选项卡,查看使用默认选项和指定选项的查询示例:
以下查询在 title 字段中搜索短语 naw yark。它使用 fuzzy 默认选项,其中:
maxEdits允许给定短语中每个术语最多有两个字符变体,以将查询与文档相匹配。maxExpansions为naw yark中的每个术语考虑多达五十个相似术语来查找匹配项。prefixLength已禁用。
The query also includes a $limit stage to limit the output to 10 results and a $project stage to:
排除所有字段,但不包括
title添加字段
score
db.movies.aggregate([ { $search: { "text": { "path": "title", "query": "naw yark", "fuzzy": {} } } }, { $limit: 10 }, { $project: { "_id": 0, "title": 1, score: { $meta: "searchScore" } } } ])
以下查询会在 title 字段中搜索与字符串 naw yark 中每个术语相差一个字符以内的术语。它使用:
maxEdits字段表示只允许使用一种字符变体,以便将查询与文档匹配。maxExpansions字段表示将查询与文档进行匹配时,必须考虑最多一百个与naw相似的术语和一百个与yark相似的术语。
The query also includes a $limit stage to limit the output to 10 results and a $project stage to:
排除所有字段,但不包括
title添加字段
score
db.movies.aggregate([ { $search: { "text": { "path": "title", "query": "naw yark", "fuzzy": { "maxEdits": 1, "maxExpansions": 100, } } } }, { $limit: 10 }, { $project: { "_id": 0, "title": 1, score: { $meta: "searchScore" } } } ])
以下查询会在 title 字段中搜索与字符串 naw yark 中每个术语相差一个字符以内的术语。它使用:
maxEdits字段表示只允许使用一种字符变体来将查询与文档相匹配。prefixLength字段表示不得更改字符串naw yark中每个术语的前两个字符以将查询与文档匹配。
The query also includes a $limit stage to limit the output to 8 results and a $project stage to:
排除
_id和title之外的所有字段添加字段
score
db.movies.aggregate([ { $search: { "text": { "path": "title", "query": "naw yark", "fuzzy": { "maxEdits": 1, "prefixLength": 2, } } } }, { $limit: 8 }, { $project: { "_id": 1, "title": 1, score: { $meta: "searchScore" } } } ])
匹配 all
以下查询使用 text 操作符在 movies 集合中的 plot 字段中搜索查询字符串 automobile race 中的所有术语。
db.movies.aggregate([ { "$search": { "text": { "path": "plot", "query": "automobile race", "matchCriteria": "all" } } }, { "$limit": 20 }, { "$project": { "_id": 0, "plot": 1, "title": 1, "score": { "$meta": "searchScore" } } } ])
[ { plot: 'A young driver, Speed Racer, aspires to be champion of the racing world with the help of his family and his high-tech Mach 5 automobile.', title: 'Speed Racer', score: 6.122188568115234 }, { plot: 'A gorgeous young automobile fanatic--and front to the hottest unsigned band on the West coast--finds herself caught up in illegal drag-racing competitions organized by exotic car fanatics.', title: 'Redline', score: 5.497724533081055 }, { plot: 'When a popular daredevil proposes an automobile race across three continents, his arch rival vows to beat him, while an ambitious female reporter has her own plans for victory.', title: 'The Great Race', score: 5.282209396362305 } ]
MongoDB Search 返回在 plot字段中任何位置包含术语 automobile 和 race 的文档。
同义词示例
以下示例使用 text操作符搜索sample_mflix.movies命名空间中的 plot字段。MongoDB Search 根据映射类型和同义词源集合synonymous_terms 中的映射类型返回结果,该类型在 sample_mflix.movies集合的索引的同义词映射定义中指定。
使用 equivalent 映射匹配 any
以下查询在 plot 字段中搜索短语 attire。它还使用集合索引中名为 mySynonyms 的同义词映射来搜索配置为单词 dress 的同义词的单词。
db.movies.aggregate([ { "$search": { "text": { "path": "plot", "query": "attire", "synonyms": "my_synonyms", "matchCriteria": "any" } } }, { "$limit": 5 }, { "$project": { "_id": 0, "plot": 1, "title": 1, "score": { "$meta": "searchScore" } } } ])
MongoDB Search 返回包含术语 attire、apparel 和 dress 的文档,因为我们将所有这些术语配置为同义词源集合synonymous_terms 中的 equivalent 同义词,这平均值所有这些术语都是同义词。因此, MongoDB Search 会为 dress 和 attire 的搜索返回类似的文档。
使用 explicit 映射匹配 any
以下查询在 plot 字段中搜索短语 boat race。它还使用集合索引中名为 my_synonyms 的同义词映射来搜索配置为单词 boat 和 race 的同义词的单词。
db.movies.aggregate([ { "$search": { "text": { "path": "plot", "query": "boat race", "synonyms": "my_synonyms", "matchCriteria": "any" } } }, { "$limit": 10 }, { "$project": { "_id": 0, "plot": 1, "title": 1, "score": { "$meta": "searchScore" } } } ])
MongoDB Search 会返回包含术语boat、vessel,、sail 或 race、rally 或 contest 的文档,因为我们将 boat 配置为 explicit 同义词或 vessel和 sail,并且我们将 race 配置为 rally 和 contest 的 explicit 同义词。MongoDB Search 不会返回任何关于 vessel 和 sail 或 rally 和 contest 的查询结果,因为我们没有将这些单词配置为将任何其他单词视为同义词。
使用同义词匹配 all
以下查询使用 text 操作符在 movies 集合中的 plot 字段中搜索查询字符串 automobile race 中的所有词语。
MongoDB Search 根据映射类型在同义词源集合synonymous_terms 中的映射类型返回结果,该类型在 sample_mflix.movies集合的索引的同义词映射定义中指定。
db.movies.aggregate([ { "$search": { "text": { "path": "plot", "query": "automobile race", "matchCriteria": "all", "synonyms": "my_synonyms" } } }, { "$limit": 20 }, { "$project": { "_id": 0, "plot": 1, "title": 1, "score": { "$meta": "searchScore" } } } ])
MongoDB Search 返回包含查询词 automobile、car 或 vehicle 以及 race、context 或 rally 的文档,因为我们将 automobile、vehicle 和 car 配置为是 equivalent 同义词,并且我们在 synonymous_terms集合中将 rally 和 contest 配置为 race 的 explicit 同义词。