Docs Home → Develop Applications → MongoDB Manual
Text Indexes文本索引
On this page本页内容
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。
Text indexes support text search queries on fields containing string content. 文本索引支持对包含字符串内容的字段进行文本搜索查询。Text indexes improve performance when searching for specific words or phrases within string content.在字符串内容中搜索特定单词或短语时,文本索引可提高性能。
A collection can only have one text index, but that index can cover multiple fields.一个集合只能有一个文本索引,但该索引可以覆盖多个字段。
To create a text index, use the following prototype:要创建文本索引,请使用以下原型:
db.<collection>.createIndex(
{
<field1>: "text",
<field2>: "text",
...
}
)
Text Search Support文本搜索支持
Text indexes support 文本索引支持本地部署上的$text
query operations on on-premises deployments. $text
查询操作。To perform text searches, you must create a text index and use the 若要执行文本搜索,必须创建文本索引并使用$text
query operator.$text
查询运算符。
Use Cases用例
Documents in an online shop's 网店clothing
collection include a description
field that contains a string of text describing each item. clothing
集合中的文档包括一个description
字段,该字段包含一系列描述每件商品的文本。To find clothes made of 要查找丝绸制成的衣服,请在silk
, create a text index on the description
field and run a text search query for documents with the keyword silk
. description
字段上创建一个文本索引,并对带有键silk
的文档运行文本搜索查询。The search returns all documents that mention 搜索将返回silk
in the description
field.description
字段中提到silk
的所有文档。
Get Started起步
To learn how to create text indexes and use text indexes in specific use cases, see:要了解如何在特定用例中创建文本索引和使用文本索引,请参阅:
Details详细信息
This section describes details for text indexes.本节介绍文本索引的详细信息。
Compound Text Indexes复合文本索引
For a compound index that includes a text index key along with keys of other types, only the text index field determines whether the index references a document. 对于包含文本索引键和其他类型键的复合索引,只有文本索引字段确定索引是否引用文档。The other keys do not determine whether the index references the documents.其他键不确定索引是否引用文档。
sparse
Property所有物
Text indexes are always sparse. When you create a text index, MongoDB ignores the 文本索引始终是稀疏的。创建文本索引时,MongoDB会忽略sparse
option.sparse
选项。
If an existing or newly inserted document lacks a text index field (or the field is null or an empty array), MongoDB does not add a text index entry for the document.如果现有或新插入的文档缺少文本索引字段(或者该字段为null
或空数组),MongoDB不会为该文档添加文本索引条目。
Storage Requirements and Performance Costs存储要求和性能成本
Text indexes have the following storage requirements and performance costs:文本索引具有以下存储要求和性能成本:
Text indexes can take up a large amount of RAM.文本索引可能会占用大量RAM。They contain one index entry for each unique post-stemmed word in each indexed field for each document inserted.它们为插入的每个文档的每个索引字段中的每个唯一后词干单词包含一个索引条目。Building a text index is similar to building a large multikey index but takes longer than building a simple ordered (scalar) index on the same data.构建文本索引类似于构建大型多键索引,但比在相同数据上构建简单有序(标量)索引需要更长的时间。When building a text index that takes up a large amount of RAM, ensure that you have a sufficiently high limit on open file descriptors.当构建一个占用大量RAM的文本索引时,请确保对打开的文件描述符有足够高的限制。See the recommended settings.请参阅建议的设置。Text indexes impact write performance because MongoDB must add an index entry for each unique post-stemmed word in each indexed field of each new source document.文本索引会影响写入性能,因为MongoDB必须为每个新源文档的每个索引字段中的每个唯一后词干单词添加一个索引条目。Text indexes store individual words of a text string. They do not store phrases or information about the proximity of words in the documents.文本索引存储文本字符串中的各个单词。它们不存储短语或有关文档中单词接近度的信息。As a result, queries that specify multiple words run faster when the entire collection fits in RAM.因此,当整个集合适合RAM时,指定多个单词的查询运行得更快。
Learn More了解更多信息
To learn more about about text indexes, see:要了解有关文本索引的更多信息,请参阅:For text search examples, see the有关文本搜索示例,请参阅$text reference page
.$text
参考页。For sample有关聚合管道中$text
operations in aggregation pipelines, see Text Search in the Aggregation Pipeline.$text
操作的示例,请参阅聚合管道中的文本搜索。