Docs HomeMongoDB Manual

Perform a Text Search (Self-Managed Deployments)执行文本搜索(自我管理部署)

Note

This page describes text search capabilities for self-managed (non-Atlas) deployments. 本页介绍了用于自我管理(非Atlas)部署的文本搜索功能。For data hosted on MongoDB Atlas, MongoDB offers an improved full-text search solution, Atlas Search.对于托管在MongoDB Atlas上的数据,MongoDB提供了一个改进的全文搜索解决方案Atlas search

To run text search queries on self-managed deployments, you must have a text index on your collection. 若要在自管理部署上运行文本搜索查询,您的集合上必须有文本索引MongoDB provides text indexes to support text search queries on string content. MongoDB提供文本索引以支持对字符串内容的文本搜索查询。Text indexes can include any field whose value is a string or an array of string elements. 文本索引可以包括任何值为字符串或字符串元素数组的字段。A collection can only have one text search index, but that index can cover multiple fields.一个集合只能有一个文本搜索索引,但该索引可以覆盖多个字段。

See the Text Indexes section for a full reference on text indexes, including behavior, tokenization, and properties.有关文本索引(包括行为、标记化和属性)的完整参考,请参阅“文本索引”部分。

Examples实例

This example demonstrates how to build a text index and use it to find coffee shops, given only text fields.这个示例演示了如何在只给定文本字段的情况下构建文本索引并使用它来查找咖啡店。

Create a Collection创建集合

Create a collection stores with the following documents:使用以下文档创建集合stores

db.stores.insertMany(
[
{ _id: 1, name: "Java Hut", description: "Coffee and cakes" },
{ _id: 2, name: "Burger Buns", description: "Gourmet hamburgers" },
{ _id: 3, name: "Coffee Shop", description: "Just coffee" },
{ _id: 4, name: "Clothes Clothes Clothes", description: "Discount clothing" },
{ _id: 5, name: "Java Shopping", description: "Indonesian goods" }
]
)

Create a Text Index创建文本索引

Run the following in mongosh to allow text search over the name and description fields:mongosh中运行以下操作,以允许在namedescription字段上进行文本搜索:

db.stores.createIndex( { name: "text", description: "text" } )

Search for an Exact Phrase搜索精确短语

You can also search for exact phrases by wrapping them in double-quotes. 你也可以通过用双引号括起来来搜索精确的短语。If the $search string includes a phrase and individual terms, text search will only match documents that include the phrase.如果$search字符串包含短语和单个术语,则文本搜索将只匹配包含该短语的文档。

For example, the following will find all documents containing "coffee shop":例如,以下内容将查找包含“咖啡店”的所有文档:

db.stores.find( { $text: { $search: "\"coffee shop\"" } } )

For more information, see Phrases.有关详细信息,请参阅短语

Exclude a Term排除术语

To exclude a word, you can prepend a "-" character. 要排除单词,可以在前面加一个“-”字符。For example, to find all stores containing "java" or "shop" but not "coffee", use the following:例如,要查找所有包含“java”或“shop”但不包含“coffee”的商店,请使用以下命令:

db.stores.find( { $text: { $search: "java shop -coffee" } } )

Sort the Results对结果排序

MongoDB will return its results in unsorted order by default. 默认情况下,MongoDB会以未排序的顺序返回结果。However, text search queries will compute a relevance score for each document that specifies how well a document matches the query.但是,文本搜索查询将为每个文档计算相关性分数,该分数指定文档与查询的匹配程度。

To sort the results in order of relevance score, you must explicitly project the $meta textScore field and sort on it:要按相关性得分的顺序对结果进行排序,必须显式投影$meta textScore字段并对其进行排序:

db.stores.find(
{ $text: { $search: "java coffee shop" } },
{ score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } )

Text search is also available in the aggregation pipeline.聚合管道中也提供文本搜索。