Overview概述
Text queries let you query string type fields in your collection for specified words or phrases. You can perform a text query by using the 文本查询允许您查询集合中的字符串类型字段,以查找指定的单词或短语。您可以使用$text operator, which performs a logical OR on each term separated by a space in the query string. $text运算符执行文本查询,该运算符对查询字符串中用空格分隔的每个词执行逻辑OR。You can also specify more options to the operator to handle case sensitivity, stop words, and word stemming (such as plural forms or other tenses) for a supported language. This is often used for unstructured text such as transcripts, essays, or web pages.您还可以为运算符指定更多选项,以处理所支持语言的区分大小写、停用词和词干(如复数形式或其他时态)。这通常用于非结构化文本,如成绩单、论文或网页。
The $text query operator requires that you specify the query field in a text index on your collection. See the examples below for sample code for creating a text index and using the $text query operator.$text查询运算符要求您在集合的文本索引中指定查询字段。有关创建文本索引和使用$text查询运算符的示例代码,请参阅下面的示例。
Note
MongoDB Search helps you build fast, relevance-based search capabilities on top of your MongoDB data. Try it today on MongoDB Atlas, our fully managed database as a service.帮助您在MongoDB数据之上构建快速、基于相关性的搜索功能。今天就在MongoDB Atlas上尝试一下,这是我们完全托管的数据库即服务。
Examples示例
The following examples use sample data from the 以下示例使用movies collection in the sample_mflix database. To enable text queries on the title field, create a text index by using the following command:sample_mflix数据库中movies集合的示例数据。要在title字段上启用文本查询,请使用以下命令创建文本索引:
await db.movies.createIndex({ title: "text" });
We use a single field text index for the examples in this guide, but you can create a compound text index that broadens your text queries to multiple fields. The following command creates a text index on two fields in the 本指南中的示例使用单字段文本索引,但您可以创建一个复合文本索引,将文本查询扩展到多个字段。以下命令在movies collection:movies集合中的两个字段上创建文本索引:
await db.movies.createIndex({ title: "text", plot: "text" });
Tip
Specify Field Weights in a Text Index在文本索引中指定字段权重
When creating a compound text index, you can specify a weight option to prioritize certain text fields in your index. When you execute a text query, the field weights influence how MongoDB calculates the text query score for each matching document.创建复合文本索引时,您可以指定一个权重选项,以对索引中的某些文本字段进行优先级排序。当您执行文本查询时,字段权重会影响MongoDB计算每个匹配文档的文本查询分数的方式。
To learn more about specifying field weights when creating a text index, see the Text Indexes section in the Indexes guide.要了解有关在创建文本索引时指定字段权重的更多信息,请参阅《索引》指南中的“文本索引”部分。
You can only create one text index per collection. Every text query searches all the fields specified in that index for matches.每个集合只能创建一个文本索引。每个文本查询都会搜索该索引中指定的所有字段以查找匹配项。
To learn more about text indexes, see Text Indexes in the Server manual.要了解有关文本索引的更多信息,请参阅服务器手册中的文本索引。
Query for Words查询单词
This example queries for Star Trek movies by searching for titles containing the word "trek". If you want to query using multiple words, separate your words with spaces to query for documents that match any of the search terms (logical 此示例通过搜索包含单词“Trek”的标题来查询OR).Star Trek(《星际迷航》)电影。如果要使用多个单词进行查询,请用空格分隔单词,以查询与任何搜索词匹配的文档(逻辑OR)。
// Create a query that searches for the string "trek"创建搜索字符串“trek”的查询
const query = { $text: { $search: "trek" } };
// Return only the `title` of each matched document仅返回每个匹配文档的`title`
const projection = {
_id: 0,
title: 1,
};
// Find documents based on our query and projection根据查询和预测查找文档
const cursor = movies.find(query).project(projection);
This operation returns the following documents:此操作返回以下文档:
{ title: 'Trek Nation' }
{ title: 'Star Trek' }
{ title: 'Star Trek Into Darkness' }
{ title: 'Star Trek: Nemesis' }
{ title: 'Star Trek: Insurrection' }
{ title: 'Star Trek: Generations' }
{ title: 'Star Trek: First Contact' }
{ title: 'Star Trek: The Motion Picture' }
{ title: 'Star Trek VI: The Undiscovered Country' }
{ title: 'Star Trek V: The Final Frontier' }
{ title: 'Star Trek IV: The Voyage Home' }
{ title: 'Star Trek III: The Search for Spock' }
{ title: 'Star Trek II: The Wrath of Khan' }
Success! The query found every document in the 成功!查询发现movies collection with a title including the word "trek". Unfortunately, the search included one unintended item: "Trek Nation," which is a movie about Star Trek and not part of the Star Trek movie series. movies集合中的每个文档都有一个标题,其中包括“trek”一词。不幸的是,搜索中包含了一个意想不到的项目:"Trek Nation"(《星际迷航之国》),这是一部关于《星际迷航》的电影,不是《星际迷航系列》的一部分。To solve this, we can query with a more specific phrase.为了解决这个问题,我们可以使用更具体的短语进行查询。
Query By Phrase按短语查询
To make your query more specific, try using the phrase "star trek" instead of just the word "trek". To search by phrase, surround your multi-word phrase with escaped quotes (为了使您的查询更具体,请尝试使用"star trek"一词,而不仅仅是"trek"一词。要按短语搜索,请用转义引号(\"<term>\"):\"<term>\")括住多词短语:
// Create a query that searches for the phrase "star trek"创建一个搜索短语"star trek"的查询
const query = { $text: { $search: "\"star trek\"" } };
// Return only the `title` of each matched document仅返回每个匹配文档的`title`
const projection = {
_id: 0,
title: 1,
};
// Find documents based on the query and projection根据查询和投影查找文档
const cursor = movies.find(query).project(projection);
Querying by the phrase 使用短语"star trek" instead of just the term "trek" matches the following documents:"star trek"而不是术语"trek"进行查询与以下文档匹配:
{ title: 'Star Trek' }
{ title: 'Star Trek Into Darkness' }
{ title: 'Star Trek: Nemesis' }
{ title: 'Star Trek: Insurrection' }
{ title: 'Star Trek: Generations' }
{ title: 'Star Trek: First Contact' }
{ title: 'Star Trek: The Motion Picture' }
{ title: 'Star Trek VI: The Undiscovered Country' }
{ title: 'Star Trek V: The Final Frontier' }
{ title: 'Star Trek IV: The Voyage Home' }
{ title: 'Star Trek III: The Search for Spock' }
{ title: 'Star Trek II: The Wrath of Khan' }
These results include all movies in the database that contain the phrase 这些结果包括数据库中包含"star trek", which in this case results in only fictional Star Trek movies. "star trek"(星际迷航)一词的所有电影,在这种情况下,只会产生科幻的《星际迷航》电影。Unfortunately, this query returned 不幸的是,此查询返回了"Star Trek Into Darkness", a movie that was not part of the original series of movies. To resolve this issue, we can omit that document with a negation."Star Trek Into Darkness"(《星际迷航:黑暗世界》),这部电影不是原版系列电影的一部分。为了解决这个问题,我们可以用否定省略该文档。
Query with Negations质疑与否定
To use a negated term, place a negative sign, 要使用否定项,请在要从结果集中省略的项前放置一个负号-, in front of the term you to omit from the result set. The query operation omits any documents that contain this term from the search result. Since this query includes two distinct terms, separate them with a space.-。查询操作会从搜索结果中省略包含此术语的任何文档。由于此查询包含两个不同的术语,请用空格分隔它们。
// Create a query that searches for the phrase "star trek" while omitting "into darkness"创建一个查询,搜索短语"star trek",同时省略"into darkness"
const query = { $text: { $search: "\"star trek\" -\"into darkness\"" } };
// Include only the `title` field of each matched document仅包含每个匹配文档的`title`字段
const projection = {
_id: 0,
title: 1,
};
// Find documents based on the query and projection根据查询和投影查找文档
const cursor = movies.find(query).project(projection);
Querying with the negated term yields the following documents:使用否定词进行查询会生成以下文档:
{ title: 'Star Trek' }
{ title: 'Star Trek: Nemesis' }
{ title: 'Star Trek: Insurrection' }
{ title: 'Star Trek: Generations' }
{ title: 'Star Trek: First Contact' }
{ title: 'Star Trek: The Motion Picture' }
{ title: 'Star Trek VI: The Undiscovered Country' }
{ title: 'Star Trek V: The Final Frontier' }
{ title: 'Star Trek IV: The Voyage Home' }
{ title: 'Star Trek III: The Search for Spock' }
{ title: 'Star Trek II: The Wrath of Khan' }
Note
Your query operation may return a reference to a cursor that contains matching documents. To learn how to examine data stored in the cursor, see the Access Data From a Cursor page.您的查询操作可能会返回对包含匹配文档的游标的引用。要了解如何检查存储在游标中的数据,请参阅“从游标访问数据”页面。
Sort by Relevance按相关性排序
Now that the result set reflects the desired results, you can use the text query 现在结果集反映了所需的结果,您可以使用文本查询textScore, accessed using the $meta operator in the query projection, to order the results by relevance:textScore(在查询投影中使用$meta运算符访问)按相关性对结果进行排序:
// Create a query that searches for the phrase "star trek" while omitting "into darkness"创建一个查询,搜索短语“星际迷航”,同时省略“进入黑暗”
const query = { $text: { $search: "\"star trek\" -\"into darkness\"" } };
// Sort returned documents by descending text relevance score按文本相关性得分降序对返回的文档进行排序
const sort = { score: { $meta: "textScore" } };
// Include only the `title` and `score` fields in each returned document在每个返回的文档中仅包含`title`和`score`字段
const projection = {
_id: 0,
title: 1,
score: { $meta: "textScore" },
};
// Find documents based on the query, sort, and projection根据查询、排序和投影查找文档
const cursor = movies
.find(query)
.sort(sort)
.project(projection);
Querying in this way returns the following documents in the following order. In general, text relevance increases as a string matches more terms and decreases as the unmatched portion of the string lengthens.以这种方式查询将按以下顺序返回以下文档。一般来说,文本相关性随着字符串匹配更多术语而增加,随着字符串中不匹配部分的延长而降低。
{ title: 'Star Trek', score: 1.5 }
{ title: 'Star Trek: Generations', score: 1.3333333333333333 }
{ title: 'Star Trek: Insurrection', score: 1.3333333333333333 }
{ title: 'Star Trek: Nemesis', score: 1.3333333333333333 }
{ title: 'Star Trek: The Motion Picture', score: 1.25 }
{ title: 'Star Trek: First Contact', score: 1.25 }
{ title: 'Star Trek II: The Wrath of Khan', score: 1.2 }
{ title: 'Star Trek III: The Search for Spock', score: 1.2 }
{ title: 'Star Trek IV: The Voyage Home', score: 1.2 }
{ title: 'Star Trek V: The Final Frontier', score: 1.2 }
{ title: 'Star Trek VI: The Undiscovered Country', score: 1.2 }
For more information about the $text operator and its options, see the manual entry.有关$text运算符及其选项的更多信息,请参阅手册输入。