Docs 菜单
Docs 主页
/ /

查询文本

In this guide, you can learn how to run a text query in the MongoDB Kotlin driver.

您可以使用文本查询来检索在指定字段中包含术语短语的文档。术语是不包括空白字符的字符序列。短语是具有任意数量的空白字符的一系列术语。

以下部分向您展示如何执行以下类型的文本查询:

  • 按词查询文本

  • 按短语查询文本

  • 排除词语的查询文本

如果您想对文本查询结果进行排序,请参阅排序结果指南中的 文本查询 部分。

以下部分功能对 fast_and_furious_movies集合进行文本查询的示例。每个部分都使用一个名为 collection 的变量来引用 fast_and_furious_movies集合的 MongoCollection实例。

fast_and_furious_movies 集合包含描述《速度与激情》系列电影中的几部电影之一的文档。每个文档都包含标题字段和标签字段。

{ "_id": 1, "title": "2 Fast 2 Furious ", "tags": ["undercover", "drug dealer"] }
{ "_id": 2, "title": "Fast 5", "tags": ["bank robbery", "full team"] }
{ "_id": 3, "title": "Furious 7", "tags": ["emotional"] }
{ "_id": 4, "title": "The Fate of the Furious", "tags": ["betrayal"] }

此数据使用以下 Kotlin 数据类进行建模:

data class Movies(
@BsonId val id: Int,
val title: String,
val tags: List<String>
)

You must create a text index before running a text query. A text index specifies the string or string array field on which to run a text query.

In the following examples, you run text queries on the title field in the fast_and_furious_movies collection. To enable text queries on the title field, create a text index using the Indexes builder with the following snippet:

collection.createIndex(Indexes.text("title"))

有关更多信息,请参阅以下资源:

  • 索引指南的文本索引部分

  • 文本索引服务器手册条目

使用 Filters.text() 方法指定文本查询。

The Filters.text() method uses the Filters builder to define a query filter specifying what to search for during the text query. The query filter is represented by a BSON instance. Pass the query filter to the find() method to run a text query.

When you execute the find() method, MongoDB runs a text query on all the fields indexed with the text index on the collection. MongoDB returns documents that contain one or more of the query terms and a relevance score for each result. For more information on relevance scores, see the Text Query section in our Sort Results guide.

You can include TextSearchOptions as the second parameter of the Filters.text() method to specify text query options such as case sensitivity. By default, text queries run without case sensitivity which means the query matches lowercase and uppercase values.

To specify a case sensitive query, use the following snippet:

val options: TextSearchOptions = TextSearchOptions().caseSensitive(true)
val filter = Filters.text("SomeText", options)

有关本节中提到的方法和类的详情,请参阅以下 API 文档:

将术语作为字符串传递给 Filters.text() 方法,以指定文本查询中的术语。

以下示例对 fast_and_furious_movies集合中的文档运行文本查询,查找包含术语“fast”的标题:

val filter = Filters.text("fast")
val findFlow = collection.find(filter)
findFlow.collect { println(it) }
Movies(id=1, title=2 Fast 2 Furious, tags=[undercover, drug dealer])
Movies(id=2, title=Fast 5, tags=[bank robbery, full team])

要匹配文本查询中的多个词语,请在 Filters.text() 构建器方法中用空格分隔每个术语。构建器方法将文本查询作为 Bson实例返回。当您将其传递给 find() 方法时,它会返回与任何术语匹配的文档。

以下示例对 fast_and_furious_movies集合中的文档运行文本查询,查找包含术语“fate”或“7”的标题:

val filter = Filters.text("fate 7")
val findFlow = collection.find(filter)
findFlow.collect { println(it) }
Movies(id=3, title=Furious 7, tags=[emotional])
Movies(id=4, title=The Fate of the Furious, tags=[betrayal])

将带有转义引号的短语传递给 Filters.text() 方法,以指定文本查询中的短语。转义引号是双引号字符,前面有一个反斜杠字符。 如果不在短语两边添加转义引号,find() 方法将运行术语搜索。

以下示例对 fast_and_furious_movies集合中的文档运行文本查询,查找包含短语“fate of the furious”的标题:

val filter = Filters.text("\"fate of the furious\"")
val findFlow = collection.find(filter)
findFlow.collect { println(it) }
Movies(id=4, title=The Fate of the Furious, tags=[betrayal])

对于要从文本查询中排除的每个术语,请在传递给 Filters.text() 构建器方法的字符串中为术语添加负号前缀。

None of the documents returned from the query contain the excluded term in your text index field.

重要

You must have at least one text query term if you want to exclude terms from your query.

以下示例对 fast_and_furious_movies集合中的文档运行文本查询,查找包含术语“furious”但不包含术语“fast”的标题:

val filter = Filters.text("furious -fast")
val findFlow = collection.find(filter)
findFlow.collect { println(it) }
Movies(id=3, title=Furious 7, tags=[emotional])
Movies(id=4, title=The Fate of the Furious, tags=[betrayal])

后退

按地理空间搜索

在此页面上