queryString
定义
语法
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 | 字符串 | 要搜索的一个或多个索引字段和值。字段和值以冒号分隔。例如,要在 您可以使用以下方法组合字段和值:
您可以使用以下符号运行通配符和正则表达式查询:
| 是 | ||||||||||||||||||||
score | 对象 | 分配给匹配搜索词结果的分数。您可以使用以下选项修改默认分数:
当您查询数组中的值时,Atlas Search 不会根据与查询匹配的数组内的值数量更改匹配结果的分数。无论数组内有多少个匹配项,分数都将与单个匹配项相同。 有关在查询中使用 | no |
示例
以下示例使用 sample_mflix
数据库中的 movies
集合。如果集群上有样本数据集,则可以使用动态映射创建名为 default
的 Atlas Search 索引,并在集群上运行示例查询。
布尔操作符查询
基本示例
以下示例使用 queryString
操作符查询标题为 Rocky
以及 IV
、 4
或 Four
的电影。
例子
以下查询在 movies
集合的路径 title
搜索 Rocky
与 IV
、4
或 Four
的组合。在以下查询中,在 defaultPath
中指定了字段,但在 query
中未指定任何字段。查询包括一个 $project
阶段,以排除 title
以外的所有字段。
1 db.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
但不包括 II
、III
、VI
或 V
的电影。
例子
以下查询在 movies
集合中的路径 title
搜索string Rocky
,而不是任何形式的数字 2
、3
、4
或 5
。在以下查询中,在 defaultPath
中指定了字段,但在 query
中未指定任何字段。该查询包括 $project
阶段,用于排除除 title
以外的所有字段。
1 db.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 Italian
的 Drama
类型的电影。仅当在 query
字段中指定的任一字段中找不到匹配结果时,才会搜索 defaultPath
中的 plot
字段。query
字段包含转义的双引号作为要搜索的短语的分隔符。
该查询包括 $project
阶段,用于:
排除除
_id
、title
、plot
和genres
之外的所有字段添加字段
score
1 db.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
操作符查询包含术语 captain
或 kirk
和 enterprise
的电影情节。
例子
以下查询在 plot
字段中搜索术语 captain
或 kirk
和 enterprise
的组合。它包括一个 $limit
阶段,用于将输出限制为 3
个结果,还包括一个 $project
阶段,用于:
排除
title
、plot
和fullplot
以外的所有字段添加字段
score
1 db.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
进行搜索的字段)同时包含captain
和kirk
,尽管满足匹配条件只需要两个字段。对于术语enterprise
,Atlas Search 会搜索fullplot
字段,即defaultPath
,因为plot
字段不包含enterprise
,并在fullplot
字段中查找enterprise
。在第二个文档中,
plot
字段包含所有三个搜索词。在第三个文档中,
plot
字段包含kirk
和enterprise
。
高级示例
以下示例使用 queryString
操作符查询包含词语 captain
、kirk
和 chess
的电影情节。此示例展示了如何使用括号对相同的搜索词进行不同的分组,从而导致不同的文档被包含在搜索结果中。
例子
以下查询在 plot
字段中搜索词 captain
、kirk
和 chess
的组合。根据搜索词的分组方式,每次查询都会返回不同的结果。
查询还包括 $project
阶段:
排除
title
、plot
和fullpath
以外的所有字段添加字段
score
下面的查询在 plot
字段中搜索 chess
以及 captain
或 kirk
。
1 db.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
中。
以下查询在plot
字段中搜索captain
或同时搜索kirk
和chess
。
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
字段中找到术语kirk
和chess
,尽管这些术语并不一定存在才能满足匹配条件。
范围查询
以下示例使用 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 Scene
和 A Corner in Wheat
的电影,尽管查询中的范围从字符 count
开始。当您使用默认分析器 lucene.standard
建立索引时,Atlas Search 会为 title
字段中的术语创建单独的标记,并将查询术语与各个标记进行匹配。具体来说,Atlas Search 会创建以下令牌,其中至少有一个(用 √ 表示)符合查询条件:
标题 | 标准分析器词元 |
---|---|
Blacksmith Scene | blacksmith , scene √ |
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
的电影类型和 man
和 men
之间的文本值范围的标题,包括两者。
例子
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
字段按字母顺序包含 Man
和 Men
之间的术语( Man
、Maque
、 March
、 Maria
、 Matter
、 Member
和 Men
)。
通配符查询
以下示例使用 queryString
操作符通过模糊、通配符和正则表达式查询电影标题。查询包括 $project
阶段,以排除 title
以外的所有字段。
以下查询使用模糊 (~
) 在 title
字段中进行模糊搜索,以便对包含类似 catch
的词且附带最多两个字符变体的电影标题进行搜索。
1 db.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?*
的电影标题,其中 *
表示任何其他数量的字符,?
表示任何单个字符。
1 db.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
字段包含术语Country
或County
,它匹配以cou
开头的字符,后跟任意数量的字符(在结果中为n
)和然后是t
,后跟至少一个或多个字符( r
如country
中的字符, y
如county
中的字符)。
以下查询使用正则表达式在 title
字段中搜索电影标题,这些电影标题包含以任何字符开头的字符,后跟字符 tal
以及 tal
之后的字符 y
或 ian
。
1 db.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
字段包含术语 Italy
或 Italian
,这些术语与任何字符(在结果中为 I
)的查询条件相匹配,后跟带有 y
(如结果中的 Italy
)或 ian
(如结果中的 Italian
)的 tal
。