对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

queryString Operator

queryString

queryString 操作符支持查询索引字段和值的组合。您可以使用 queryString 操作符对 string 字段执行文本、通配符、正则表达式、模糊和范围搜索。

queryString 通过以下语法实现:

1{
2 "$search": {
3 "index": <index name>, // optional, defaults to "default"
4 "queryString": {
5 "defaultPath": "<default-field-to-search>",
6 "query": "(<field-to-search>: (<search-values>) AND|OR (<search-values>)) AND|OR (<search-values>)"
7 }
8 }
9}

queryString 使用以下词条来构造查询:

字段
类型
说明
必要性

defaultPath

字符串

默认搜索的索引字段。如果您在 query 中省略了要搜索的字段,则MongoDB Search 仅搜索 defaultPath 中的字段。

是

query

字符串

要搜索的一个或多个索引字段和值。字段和值以冒号分隔。例如,要在 plot 字段中搜索字符串 baseball,请使用 plot:baseball。以下操作符可用于组合多个搜索条件:

您可以使用以下方法组合字段和值:

  • AND表示 AND 布尔值操作符。必须存在所有搜索值,文档才能包含在结果中。

  • OR表示 OR 布尔值操作符。必须至少有一个搜索值存在,搜索结果中才能包含文档。

  • NOT表示 NOT 布尔值操作符。指定的搜索值必须不存在,文档才能包含在搜索结果中。

  • TO:表示要搜索的范围。您可以使用 [] 表示包含范围,使用 {} 表示排除范围,或使用 {] 和 [} 表示半开范围。必须存在指定范围内的值,文档才能包含在结果中。

  • ()子查询的分隔符。使用括号对要搜索的字段和值进行分组。

您可以使用以下符号运行通配符和正则表达式查询:

  • ?指示要匹配的任何单个字符。

  • *: 表示要匹配 0 个或多个字符。

    queryString 操作符不支持以 * 作为查询中第一个字符的通配符查询。queryString 操作符会将 * 前面的任何字符视为前缀或必须完全匹配的字符。

  • /正则表达式的分隔符。

  • ~:指示模糊搜索,用于查找与搜索词相似的字符串,最多可进行指定的编辑次数。示例,{ "query":"foo":"stop"~2 } 等于以下查询:

    { "defaultPath": "foo", "query": "stop", "maxEdits": 2 }

    queryString 操作符最多只能支持 2 个编辑的模糊搜索。更高的距离没有用处,因为结果可能与许多字典术语匹配。

    如果将 ~ 与字符串中的多个术语一起使用,则 queryString 操作符会对指定数量术语内的术语进行邻近搜索。例如,{ "query":"foo":"New York"~2 } 等于以下查询:

    { "defaultPath": "foo", "query": "New York", "slop": 2 }

是

score

对象

分配给匹配搜索词结果的分数。您可以使用以下选项修改默认分数:

  • boost将生成的分数乘以给定数字。

  • constant将结果分数替换为给定数字。

  • function:使用给定的表达式替换结果分数。

查询大量值时,如果大量中的更多值与查询匹配, MongoDB Search 会分配更高的分数。

有关在查询中使用 score 的信息,请参阅对结果中的文档进行评分。

no

默认情况下,text 操作符使用 bm25 相似度算法根据文档与查询的相关性对文档进行评分。

您可以通过在 similarity.type 属性中为 MongoDB Search 索引定义中的 string 字段指定不同的相似性算法来更改所使用的相似性算法。要了解如何为 string 类型配置 MongoDB Search 索引,请参阅如何索引 String 字段。

要了解有关支持的相似度算法的更多信息,请参阅评分详情。

The following examples use the movies collection in the sample_mflix database. If you have the sample dataset on your cluster , you can create the MongoDB Search index named default with dynamic mappings and run the example queries on your cluster.

The following example uses the queryString operator to query for movies with the title Rocky and either IV, 4, or Four at path title in the movies collection. In the following query, a field is specified in the defaultPath, but no fields are specified in the query. The query includes a $project stage to exclude all fields except title.

1db.movies.aggregate([
2 {
3 $search: {
4 "queryString": {
5 "defaultPath": "title",
6 "query": "Rocky AND (IV OR 4 OR Four)"
7 }
8 }
9 },
10 {
11 $project: {
12 "_id": 0,
13 "title": 1
14 }
15 }
16])

对于此查询, MongoDB Search 在 defaultPath 处执行搜索,因为 query 中没有字段。它返回以下结果:

{ "title" : "Rocky IV" }

本节中的示例使用 queryString 操作符来查询包含术语 captain、kirk 和 chess 的电影情节。此示例展示了如何使用括号对相同的搜索词进行不同的分组,从而导致不同的文档被包含在搜索结果中。

The queries also includes a $project stage to:

  • 排除 title、plot 和 fullpath 以外的所有字段

  • 添加字段 score

下面的查询在 plot 字段中搜索 chess 以及 captain 或 kirk。

1db.movies.aggregate([
2 {
3 $search: {
4 "queryString": {
5 "defaultPath": "fullplot",
6 "query": "plot:(captain OR kirk) AND chess"
7 }
8 }
9 },
10 {
11 $project: {
12 "_id": 0,
13 "title": 1,
14 "plot": 1,
15 "fullplot": 1,
16 score: { $meta: "searchScore" }
17 }
18 }
19])
{
"fullplot" : "When the crew of the Enterprise is called back home, they find an unstoppable force of terror from within their own organization has detonated the fleet and everything it stands for, leaving our world in a state of crisis. With a personal score to settle, Captain Kirk leads a manhunt to a war-zone world to capture a one-man weapon of mass destruction. As our heroes are propelled into an epic chess game of life and death, love will be challenged, friendships will be torn apart, and sacrifices must be made for the only family Kirk has left: his crew.",
"plot" : "After the crew of the Enterprise find an unstoppable force of terror from within their own organization, Captain Kirk leads a manhunt to a war-zone world to capture a one-man weapon of mass destruction.",
"title" : "Star Trek Into Darkness",
"score" : 7.968792915344238
}

结果中的文档是匹配的,因为 plot 中包含术语 captain 和 kirk,虽然只需要一个术语就能满足匹配条件,而且术语 chess 虽然不在 plot 中,但出现在 fullplot 中,也就是 defaultPath 中。

以下示例使用 queryString 操作符按字母升序查询从字符 count 到任意字符(使用通配符 * 定义)的一系列文本值的电影标题。

1db.movies.aggregate([
2 {
3 "$search": {
4 "queryString": {
5 "defaultPath": "plot",
6 "query": "title:[count TO *]"
7 }
8 }
9 },
10 {
11 "$limit": 10
12 },
13 {
14 "$project": {
15 "_id": 0,
16 "title": 1
17 }
18 }
19])
1[
2 { title: 'Blacksmith Scene' },
3 { title: 'The Great Train Robbery' },
4 { title: 'The Land Beyond the Sunset' },
5 { title: 'A Corner in Wheat' },
6 { title: 'Winsor McCay, the Famous Cartoonist of the N.Y. Herald and His Moving Comics' },
7 { title: 'Traffic in Souls' },
8 { title: 'Gertie the Dinosaur' },
9 { title: 'In the Land of the Head Hunters' },
10 { title: 'The Perils of Pauline' },
11 { title: 'The Birth of a Nation' }
12]

MongoDB搜索结果包括标题为 Blacksmith Scene 和 A Corner in Wheat 的电影,尽管查询中的范围从字符 count 开始。当您使用默认分析器lucene.standard 进行索引, MongoDB Search 会为 title字段中的词语创建单独的词元,并将查询术语与各个词元进行匹配。具体来说, MongoDB Search 会创建以下词元,其中至少有一个(用 √ 表示)与查询条件匹配:

标题
标准分析器词元

Blacksmith Scene

blacksmith, scene √

A Corner in Wheat

a, corner, in, wheat √

如果使用 lucene.keyword分析器进行索引, MongoDB Search 将为 title字段中的整个字符串创建单个词元,从而对同一查询产生类似于以下的结果:

[
{ title: 'è Nous la Libertè' },
{ title: 'tom thumb' },
{ title: 'è nos amours' },
{ title: 'èke och hans vèrld' },
{ title: 'èdipussi' },
{ title: 's/y Glèdjen' },
{ title: 'èAy, Carmela!' },
{ title: 'èlisa' },
{ title: 'èxtasis' },
{ title: 'eXistenZ' }
]

The following examples use the queryString operator to query for movie titles using fuzzy, wildcard, and regular expressions. The queries include a $project stage to exclude all fields except title.

以下查询使用模糊 (~) 在 title 字段中进行模糊搜索,以便对包含类似 catch 的词且附带最多两个字符变体的电影标题进行搜索。

1db.movies.aggregate([
2 {
3 "$search": {
4 "queryString": {
5 "defaultPath": "title",
6 "query": "catch~2"
7 }
8 }
9 },
10 {
11 "$limit": 10
12 },
13 {
14 "$project": {
15 "_id": 0,
16 "title": 1
17 }
18 }
19])
1[
2 { title: 'Catch-22' },
3 { title: 'Catch That Girl' },
4 { title: 'Catch That Kid' },
5 { title: 'Catch a Fire' },
6 { title: 'Catch Me Daddy' },
7 { title: 'Death Watch' },
8 { title: 'Patch Adams' },
9 { title: "Batch '81" },
10 { title: 'Briar Patch' },
11 { title: 'Night Watch' }
12]

以下查询在 movies 集合的 title 字段中搜索 Marvel、Avengers、Iron Man 或 Captain America。

以下查询返回符合搜索条件的电影,并按以下时间段(年份)分类:

  • 2000,包括此存储桶的下限

  • 2005,2000 存储桶的不含上限以及此存储桶的包含下限

  • 2010,2005 存储桶的不含上限以及此存储桶的包含下限

  • 2015,2010 存储桶的不含上限以及此存储桶的包含下限

  • 2020,2015 存储桶的独占上限

1db.movies.aggregate([
2 {
3 "$searchMeta": {
4 "facet": {
5 "operator": {
6 "queryString": {
7 "defaultPath": "title",
8 "query": "\"Marvel\" OR \"Avengers\" OR \"Iron Man\" OR \"Captain America\""
9 }
10 },
11 "facets": {
12 "yearFacet": {
13 "type": "number",
14 "path": "year",
15 "boundaries": [1990, 2000, 2005, 2010, 2015, 2020 ]
16 }
17 }
18 }
19 }
20 }
21])