On this page本页内容
MongoDB Atlas SearchMongoDB Atlas搜索
Atlas Search makes it easy to build fast, relevance-based search capabilities on top of your MongoDB data. Atlas Search可以轻松地在MongoDB数据之上构建快速、基于相关性的搜索功能。Try it today on MongoDB Atlas, our fully managed database as a service.今天就在MongoDB Atlas上试试吧,它是我们完全管理的数据库即服务。
MongoDB supports query operations that perform a text search of string content. MongoDB支持对字符串内容执行文本搜索的查询操作。To perform text search, MongoDB uses a text index and the 为了执行文本搜索,MongoDB使用文本索引和$text operator.$text运算符。
This example demonstrates how to build a text index and use it to find coffee shops, given only text fields.这个例子演示了如何构建一个文本索引,并在只给定文本字段的情况下使用它来查找咖啡店。
Create a collection 使用以下文档创建集合stores with the following documents:stores:
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.
To perform text search queries, you must have a 若要执行文本搜索查询,集合上必须有文本索引。text index on your collection. A collection can only have one text search index, but that index can cover multiple fields.一个集合只能有一个文本搜索索引,但该索引可以覆盖多个字段。
For example you can run the following in a 例如,您可以在mongo shell to allow text search over the name and description fields:mongo shell中运行以下命令,以允许对name和description字段进行文本搜索:
$textUse the 使用$text query operator to perform text searches on a collection with a text index.$text查询运算符对具有文本索引的集合执行文本搜索。
$text will tokenize the search string using whitespace and most punctuation as delimiters, and perform a logical 将使用空格和大多数标点符号作为分隔符标记搜索字符串,并对搜索字符串中的所有此类标记执行逻辑OR of all such tokens in the search string.OR。
For example, you could use the following query to find all stores containing any terms from the list “coffee”, “shop”, and “java”:例如,您可以使用以下查询查找包含列表“coffee”、“shop”和“java”中任何术语的所有商店:
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”:例如,以下内容将查找包含“coffee shop”的所有文档:
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”的所有商店,请使用以下命令:
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字段并对其进行排序:
Text search is also available in the aggregation pipeline.聚合管道中还提供文本搜索。
MongoDB supports text search for various languages. MongoDB支持各种语言的文本搜索。See Text Search Languages for a list of supported languages.有关支持的语言列表,请参阅文本搜索语言。