Text Search文本搜索¶

On this page本页内容

  • Overview概述
  • Example示例
  • Language Support语言支持

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上试试吧,它是我们完全管理的数据库即服务。

Overview概述¶

MongoDB supports query operations that perform a text search of string content. MongoDB支持对字符串内容执行文本搜索的查询操作。To perform text search, MongoDB uses a text index and the $text operator.为了执行文本搜索,MongoDB使用文本索引和$text运算符。

Note

Views do not support text search.视图不支持文本搜索。

Example示例¶

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:

db.stores.insert(
   [
     { _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" }
   ]
)

Text Index文本索引¶

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字段进行文本搜索:

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

$text Operator运算符¶

Use 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”中任何术语的所有商店:

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

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”:例如,以下内容将查找包含“coffee shop”的所有文档:

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

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

Term Exclusion术语排除¶

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" } } )

Sorting排序¶

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.聚合管道中还提供文本搜索。

Language Support语言支持¶

MongoDB supports text search for various languages. MongoDB支持各种语言的文本搜索。See Text Search Languages for a list of supported languages.有关支持的语言列表,请参阅文本搜索语言。