Docs 菜单
Docs 主页
/
MongoDB Atlas
/ / / /

queryString

在此页面上

  • 定义
  • 语法
  • 选项
  • 示例
  • 布尔操作符查询
  • 范围查询
  • 通配符查询
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 中提供要搜索的字段,则 Atlas 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:使用给定的表达式替换结果分数。

当您查询数组中的值时,Atlas Search 不会根据与查询匹配的数组内的值数量更改匹配结果的分数。无论数组内有多少个匹配项,分数都将与单个匹配项相同。

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

no

以下示例使用 sample_mflix 数据库中的 movies 集合。如果集群上有样本数据集,则可以使用动态映射创建名为 default 的 Atlas Search 索引,并在集群上运行示例查询。

以下示例使用 queryString 操作符查询标题为 Rocky 以及 IV4Four 的电影。

例子

以下查询在 movies 集合的路径 title 搜索 RockyIV4Four 的组合。在以下查询中,在 defaultPath 中指定了字段,但在 query 中未指定任何字段。查询包括一个 $project 阶段,以排除 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])

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

{ "title" : "Rocky IV" }

以下示例使用 queryString 操作符查询标题为 Rocky 但不包括 IIIIIVIV 的电影。

例子

以下查询在 movies 集合中的路径 title 搜索string Rocky,而不是任何形式的数字 2345。在以下查询中,在 defaultPath 中指定了字段,但在 query 中未指定任何字段。该查询包括 $project 阶段,用于排除除 title 以外的所有字段。

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

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

[
{ title: 'Rocky' },
{ title: 'Rocky Marciano' },
{ title: 'Rocky Balboa' },
{ title: 'The Rocky Horror Picture Show' },
{ title: 'The Adventures of Rocky & Bullwinkle' }
]

以下示例使用 queryString 操作符查询标题为 The Italian 且类型为 Drama 的电影。

例子

以下查询在字段组合中搜索标题为 The ItalianDrama 类型的电影。仅当在 query 字段中指定的任一字段中找不到匹配结果时,才会搜索 defaultPath 中的 plot 字段。query 字段包含转义的双引号作为要搜索的短语的分隔符。

该查询包括 $project 阶段,用于:

  • 排除除_idtitleplotgenres之外的所有字段

  • 添加字段 score

1db.movies.aggregate([
2 {
3 $search: {
4 "queryString": {
5 "defaultPath": "plot",
6 "query": "title:\"The Italian\" AND genres:Drama"
7 }
8 }
9 },
10 {
11 $project: {
12 "_id": 1,
13 "title": 1,
14 "plot": 1,
15 "genres": 1,
16 score: { $meta: "searchScore" }
17 }
18 }
19])

此查询将返回以下结果:

1{
2 "_id" : ObjectId("573a1390f29313caabcd56df"),
3 "plot" : "An immigrant leaves his sweetheart in Italy to find a better life across the sea in the grimy slums of New York. They are eventually reunited and marry. But life in New York is hard and ...",
4 "genres" : [ "Drama" ],
5 "title" : "The Italian",
6 "score" : 4.975106716156006
7}
8{
9 "_id" : ObjectId("573a13b3f29313caabd3e36c"),
10 "plot" : "Set in 2002, an abandoned 5-year-old boy living in a rundown orphanage in a small Russian village is adopted by an Italian family.",
11 "genres" : [ "Drama" ],
12 "title" : "The Italian",
13 "score" : 4.975106716156006
14}
15{
16 "_id" : ObjectId("573a13c7f29313caabd756ee"),
17 "plot" : "A romantic fairy tale about a 19-year old orphan girl who, as her sole inheritance, gets an antique key that unlocks both an old Italian villa and the secrets of her family history.",
18 "genres" : [ "Comedy", "Drama", "Romance" ],
19 "title" : "The Italian Key",
20 "score" : 4.221206188201904
21}
22{
23 "_id" : ObjectId("573a13caf29313caabd7d1e4"),
24 "plot" : "A chronicle of the 1969 bombing at a major national bank in Milan and its aftermath.",
25 "genres" : [ "Drama" ],
26 "title" : "Piazza Fontana: The Italian Conspiracy",
27 "score" : 3.4441356658935547
28}

以下示例使用 queryString 操作符查询包含术语 captainkirkenterprise 的电影情节。

例子

以下查询在 plot 字段中搜索术语 captainkirkenterprise 的组合。它包括一个 $limit 阶段,用于将输出限制为 3 个结果,还包括一个 $project 阶段,用于:

  • 排除 titleplotfullplot 以外的所有字段

  • 添加字段 score

1db.movies.aggregate([
2 {
3 $search: {
4 "queryString": {
5 "defaultPath": "fullplot",
6 "query": "plot:(captain OR kirk) AND enterprise"
7 }
8 }
9 },
10 {
11 $limit: 3
12 },
13 {
14 $project: {
15 "_id": 0,
16 "title": 1,
17 "plot": 1,
18 "fullplot": 1,
19 score: { $meta: "searchScore" }
20 }
21 }
22])

此查询将返回以下结果:

1{
2 "plot" : "Captain Picard, with the help of supposedly dead Captain Kirk, must stop a madman willing to murder on a planetary scale in order to enter an energy ribbon.",
3 "fullplot" : "In the late 23rd century, the gala maiden voyage of the third Starship Enterprise (NCC-1701-B) boasts such luminaries as Pavel Chekov, Montgomery Scott and the legendary Captain James T. Kirk as guests. But the maiden voyage turns to disaster as the unprepared ship is forced to rescue two transport ships from a mysterious energy ribbon. The Enterprise manages to save a handful of the ships' passengers and barely makes it out intact... but at the cost of Captain Kirk's life. Seventy-eight years later, Captain Jean-Luc Picard and the crew of the Enterprise-D find themselves at odds with the renegade scientist Tolian Soran... who is destroying entire star systems. Only one man can help Picard stop Soran's scheme... and he's been dead for seventy-eight years.",
4 "title" : "Star Trek: Generations",
5 "score" : 11.274821281433105
6}
7{
8 "plot" : "Captain Kirk and his crew must deal with Mr. Spock's long-lost half-brother who hijacks the Enterprise for an obsessive search for God at the center of the galaxy.",
9 "fullplot" : "When the newly-christened starship Enterprise's shakedown cruise goes poorly, Captain Kirk and crew put her into Spacedock for repairs. But an urgent mission interrupts their Earth-bound shore leave. A renegade Vulcan named Sybok has taken three ambassadors hostage on Nimbus III, the Planet of Galactic Peace. This event also attracts the attention of a Klingon captain who wants to make a name for himself and sets out to pursue the Enterprise. Sybok's ragtag army captures the Enterprise and takes her on a journey to the center of the galaxy in search of the Supreme Being.",
10 "title" : "Star Trek V: The Final Frontier",
11 "score" : 9.889547348022461
12}
13{
14 "plot" : "When an alien spacecraft of enormous power is spotted approaching Earth, Admiral Kirk resumes command of the Starship Enterprise in order to intercept, examine and hopefully stop the intruder.",
15 "fullplot" : "A massive alien spacecraft of enormous power is approaching Earth, destroying everything in its path. The only star ship in range is the USS Enterprise still in dry-dock after a major overhaul. As Captain Willard Decker readies his ship and his crew to face this menace, Admiral James T. Kirk arrives with orders to take command of the Enterprise and intercept the alien intruder. But it has been three years since Kirk last commanded the Enterprise on its historic five year mission... is he up to the task of saving the Earth?",
16 "title" : "Star Trek: The Motion Picture",
17 "score" : 8.322310447692871
18}

上述结果中的文档匹配的原因是:

  • 在第一个文档中,plot 字段(按照 query 进行搜索的字段)同时包含 captainkirk,尽管满足匹配条件只需要两个字段。对于术语 enterprise,Atlas Search 会搜索 fullplot 字段,即 defaultPath,因为 plot 字段不包含 enterprise,并在 fullplot 字段中查找 enterprise

  • 在第二个文档中,plot 字段包含所有三个搜索词。

  • 在第三个文档中,plot 字段包含 kirkenterprise

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

例子

以下查询在 plot 字段中搜索词 captainkirkchess 的组合。根据搜索词的分组方式,每次查询都会返回不同的结果。

查询还包括 $project 阶段:

  • 排除 titleplotfullpath 以外的所有字段

  • 添加字段 score

下面的查询在 plot 字段中搜索 chess 以及 captainkirk

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 中包含术语 captainkirk,虽然只需要一个术语就能满足匹配条件,而且术语 chess 虽然不在 plot 中,但出现在 fullplot 中,也就是 defaultPath 中。

以下查询在plot字段中搜索captain或同时搜索kirkchess

db.movies.aggregate([
{
$search: {
"queryString": {
"defaultPath": "fullplot",
"query": "plot:captain OR (kirk AND chess)"
}
}
},
{
$limit: 5
},
{
$project: {
"_id": 0,
"title": 1,
"plot": 1,
"fullplot": 1,
score: { $meta: "searchScore" }
}
}
])

此查询将返回以下结果:

{
"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" : 9.227973937988281
}
{
"plot" : "Captain Picard, with the help of supposedly dead Captain Kirk, must stop a madman willing to murder on a planetary scale in order to enter an energy ribbon.",
"title" : "Star Trek: Generations",
"fullplot" : "In the late 23rd century, the gala maiden voyage of the third Starship Enterprise (NCC-1701-B) boasts such luminaries as Pavel Chekov, Montgomery Scott and the legendary Captain James T. Kirk as guests. But the maiden voyage turns to disaster as the unprepared ship is forced to rescue two transport ships from a mysterious energy ribbon. The Enterprise manages to save a handful of the ships' passengers and barely makes it out intact... but at the cost of Captain Kirk's life. Seventy-eight years later, Captain Jean-Luc Picard and the crew of the Enterprise-D find themselves at odds with the renegade scientist Tolian Soran... who is destroying entire star systems. Only one man can help Picard stop Soran's scheme... and he's been dead for seventy-eight years.",
"score" : 3.3556222915649414
}
{
"plot" : "An army cadet accompanies an irascible, blind captain on a week-long trip from Turin to Naples. The captain, Fausto, who wants no pity, brooks no disagreement, and charges into every ...",
"title" : "Scent of a Woman",
"fullplot" : "An army cadet accompanies an irascible, blind captain on a week-long trip from Turin to Naples. The captain, Fausto, who wants no pity, brooks no disagreement, and charges into every situation, nicknames the youth Ciccio (\"Babyfat\"), and spends the next few days ordering him about and generally behaving badly in public. In Rome, Fausto summons a priest to ask for his blessing; in Naples, where Fausto joins a blind lieutenant for drinking and revelry, the two soldiers talk quietly and seriously about \"going through with it.\" Also in Naples is Sara, in love with Fausto, but treated cruelly by him. What do the blind soldiers plan? Can Sara soften Fausto's hardened heart?",
"score" : 3.2553727626800537
}
{
"plot" : "A seductive woman falls in love with a mysterious ship's captain.",
"title" : "Pandora and the Flying Dutchman",
"fullplot" : "Albert Lewin's interpretation of the legend of the Flying Dutchman. In a little Spanish seaport named Esperanza, during the 30s, appears Hendrick van der Zee, the mysterious captain of a yacht (he is the only one aboard). Pandora is a beautiful woman (who men kill and die for). She's never really fallen in love with any man, but she feels very attracted to Hendrick... We are soon taught that Hendrick is the Flying Dutchman, this sailor of the 17th century that has been cursed by God to wander over the seas until the Doomsday... unless a woman is ready to die for him...",
"score" : 3.2536067962646484
}
{
"plot" : "The adventures of pirate Captain Red and his first mate Frog.",
"title" : "Pirates",
"fullplot" : "Captain Red runs a hardy pirate ship with the able assistance of Frog, a dashing young French sailor. One day Capt. Red is captured and taken aboard a Spanish galleon, but thanks to his inventiveness, he raises the crew to mutiny, takes over the ship, and kidnaps the niece of the governor of Maracaibo. The question is, can he keep this pace up?",
"score" : 3.2536067962646484
}

上述结果中的文档匹配,因为plot字段包含词语captain 。 第一个文档获得更高的分数,因为 Atlas Search 还在fullplot字段中找到术语kirkchess ,尽管这些术语并不一定存在才能满足匹配条件。

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

例子

1 db.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]

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

标题
标准分析器词元
Blacksmith Scene
blacksmithscene
A Corner in Wheat
a, corner, in, wheat

如果您使用 lucene.keyword 分析器建立索引,Atlas 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' }
]

以下示例使用 queryString 操作符按字母升序查询 drama 的电影类型和 manmen 之间的文本值范围的标题,包括两者。

例子

1 db.movies.aggregate([
2 {
3 "$search": {
4 "queryString": {
5 "defaultPath": "plot",
6 "query": "title:[man TO men] AND genres: Drama"
7 }
8 }
9 },
10 {
11 "$limit": 10
12 },
13 {
14 "$project": {
15 "_id": 0,
16 "title": 1,
17 "genres": 1
18 }
19 }
20 ])
1[
2 { genres: [ 'Drama' ], title: 'The Wedding March' },
3 { genres: [ 'Drama' ], title: 'Maria Chapdelaine' },
4 { genres: [ 'Drama' ], title: 'Of Mice and Men' },
5 { genres: [ 'Drama' ], title: 'All the King's Men' },
6 { genres: [ 'Drama' ], title: 'Wuya yu maque' },
7 { genres: [ 'Drama' ], title: 'The Men' },
8 { genres: [ 'Drama' ], title: 'The Member of the Wedding' },
9 { genres: [ 'Drama' ], title: 'The Great Man' },
10 { genres: [ 'Drama' ], title: 'A Matter of Dignity' },
11 { genres: [ 'Drama' ], title: 'The Last Angry Man' }
12]

结果中的文档匹配,因为 genres 字段包含术语 drama,而电影title 字段按字母顺序包含 ManMen 之间的术语( ManMaqueMarchMariaMatterMemberMen)。

以下示例使用 queryString 操作符通过模糊、通配符和正则表达式查询电影标题。查询包括 $project 阶段,以排除 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]

以下查询使用通配符表达式,在Atlas Search title 字段中查找包含字符 cou*t?* 的电影标题,其中 * 表示任何其他数量的字符,? 表示任何单个字符。

1db.movies.aggregate([
2 {
3 "$search": {
4 "queryString": {
5 "defaultPath": "title",
6 "query": "cou*t?*"
7 }
8 }
9 },
10 {
11 "$limit": 5
12 },
13 {
14 "$project": {
15 "_id": 0,
16 "title": 1
17 }
18 }
19])
1[
2 { title: 'A Day in the Country' },
3 { title: 'Diary of a Country Priest' },
4 { title: 'Cry, the Beloved Country' },
5 { title: 'The Country Girl' },
6 { title: 'Raintree County' }
7]

结果中的文档匹配,因为title字段包含术语CountryCounty ,它匹配以cou开头的字符,后跟任意数量的字符(在结果中为n )和然后是t ,后跟至少一个或多个字符( rcountry中的字符, ycounty中的字符)。

以下查询使用正则表达式在 title 字段中搜索电影标题,这些电影标题包含以任何字符开头的字符,后跟字符 tal 以及 tal 之后的字符 yian

1db.movies.aggregate([
2 {
3 "$search": {
4 "queryString": {
5 "defaultPath": "title",
6 "query": "/.tal(y|ian)/"
7 }
8 }
9 },
10 {
11 "$limit": 5
12 },
13 {
14 "$project": {
15 "_id": 0,
16 "title": 1
17 }
18 }
19])
1[
2 { title: 'The Italian' },
3 { title: 'Journey to Italy' },
4 { title: 'Divorce Italian Style' },
5 { title: 'Marriage Italian Style' },
6 { title: 'Jealousy, Italian Style' }
7]

结果中的文档之所以匹配,是因为 title 字段包含术语 ItalyItalian,这些术语与任何字符(在结果中为 I)的查询条件相匹配,后跟带有 y(如结果中的 Italy)或 ian(如结果中的 Italian)的 tal

后退

短语