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
使用以下词条来构造查询:
字段 | 类型 | 说明 | 必要性 | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 字符串 | 默认搜索的索引字段。如果您没有在 | 是 | ||||||||||||||||||||
| 字符串 | 要搜索的一个或多个索引字段和值。字段和值以冒号分隔。例如,要在 您可以使用以下方法组合字段和值:
您可以使用以下符号运行通配符和正则表达式查询:
| 是 | ||||||||||||||||||||
| 对象 | 分配给匹配搜索词结果的分数。您可以使用以下选项修改默认分数:
查询大量值时,无论大量中有多少个值与查询匹配, Atlas Search都会分配相同的分数。 有关在查询中使用 | no |
示例
以下示例使用movies
sample_mflix
数据库中的 集合。如果集群上有示例数据集,则可以使用动态映射创建名为 的Atlasdefault
Search索引,并在集群上运行示例查询。
布尔操作符查询
基本示例
以下示例使用 queryString
操作符查询 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
操作符查询路径 title
处标题为 Rocky
而不是任何形式的数字 2
、3
、4
或 5
的电影 movies
集合。在以下查询中,在 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 ])
[ { title: 'Rocky' }, { title: 'Rocky Marciano' }, { title: 'Rocky Balboa' }, { title: 'The Rocky Horror Picture Show' }, { title: 'The Adventures of Rocky & Bullwinkle' } ]
以下示例使用 queryString
操作符查询字段组合以查找 Drama
类型中标题为 The
Italian
的电影。仅当在 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 ])
{ "_id" : ObjectId("573a1390f29313caabcd56df"), "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 ...", "genres" : [ "Drama" ], "title" : "The Italian", "score" : 4.975106716156006 } { "_id" : ObjectId("573a13b3f29313caabd3e36c"), "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.", "genres" : [ "Drama" ], "title" : "The Italian", "score" : 4.975106716156006 } { "_id" : ObjectId("573a13c7f29313caabd756ee"), "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.", "genres" : [ "Comedy", "Drama", "Romance" ], "title" : "The Italian Key", "score" : 4.221206188201904 } { "_id" : ObjectId("573a13caf29313caabd7d1e4"), "plot" : "A chronicle of the 1969 bombing at a major national bank in Milan and its aftermath.", "genres" : [ "Drama" ], "title" : "Piazza Fontana: The Italian Conspiracy", "score" : 3.4441356658935547 }
以下示例使用 queryString
操作符在 plot
字段中查询词语、captain
或 kirk
和 enterprise
的组合。它包括一个用于将输出限制为 3
个结果的$limit
阶段和一个用于执行以下操作的$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 ])
{ "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.", "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.", "title" : "Star Trek: Generations", "score" : 11.274821281433105 } { "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.", "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.", "title" : "Star Trek V: The Final Frontier", "score" : 9.889547348022461 } { "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.", "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?", "title" : "Star Trek: The Motion Picture", "score" : 8.322310447692871 }
结果中的文档匹配的原因是:
在第一个文档中,
plot
字段(按照query
进行搜索的字段)同时包含captain
和kirk
,尽管满足匹配条件只需要两个字段。对于术语enterprise
,Atlas Search 会搜索fullplot
字段,即defaultPath
,因为plot
字段不包含enterprise
,并在fullplot
字段中查找enterprise
。在第二个文档中,
plot
字段包含所有三个搜索词。在第三个文档中,
plot
字段包含kirk
和enterprise
。
高级示例
本部分中的示例使用 queryString
操作符查询包含术语 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
。
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 $limit: 5 12 }, 13 { 14 $project: { 15 "_id": 0, 16 "title": 1, 17 "plot": 1, 18 "fullplot": 1, 19 score: { $meta: "searchScore" } 20 } 21 } 22 ])
{ "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 |
|
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
。
元数据查询
以下查询在 movies
集合中的 title
字段中搜索 Marvel
、Avengers
、Iron Man
或 Captain
America
。