定義
構文
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
では、次の用語を使用してクエリを作成します。
フィールド | タイプ | 説明 | 必要性 | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| string | デフォルトで検索するインデックス付きフィールド。 Atlas Search では、 フィールドを省略して | はい | ||||||||||||||||||||
| string | 検索する 1 つ以上のインデックス付きフィールドと値。 フィールドと値はコロンで区切られます。 たとえば、 次のように、フィールドと値を組み合わせることができます。
次の方法を使用して、ワイルドカードおよび正規式のクエリを実行できます。
| はい | ||||||||||||||||||||
| オブジェクト | 一致する検索結果に割り当てられたスコア。 デフォルトのスコアは、次のオプションを使用して変更できます。
配列の値をクエリする際、Atlas Search は、配列内でクエリに一致する値の数に関係なく、同一のスコアを割り当てます。 クエリで | no |
例
次の例では、sample_mflix
データベースの movies
コレクションを使用します。クラスターにサンプル データセットがある場合は、動的マッピングを使用してdefault
という名前の Atlas Search インデックスを作成し、クラスターでサンプル クエリを実行できます。
ブール演算子クエリ
基本的な例
次の例では、queryString
演算子を使用して、タイトルが Rocky
で、movies
コレクションのパス title
にある IV
、4
、または Four
のいずれかを持つ映画をクエリします。次のクエリでは、defaultPath
にフィールドが指定されていますが、query
にはフィールドが指定されていません。クエリには、title
以外のすべてのフィールドを除外する $project
ステージが含まれています。
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 ])
このクエリでは、 query
にフィールドがないため、Atlas Search はdefaultPath
で検索を実行します。 これにより、次の結果が返されます。
{ "title" : "Rocky IV" }
次の例では、queryString
演算子を使用して、タイトルが Rocky
であり、2
、3
、4
、5
のいずれの形式の数値も含まない映画を、movies
コレクションの title
パスでクエリします。次のクエリでは、defaultPath
にフィールドが指定されていますが、query
にはフィールドが指定されていません。クエリには、title
以外のすべてのフィールドを除外する $project
ステージが含まれています。
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
の映画のフィールドの組み合わせをクエリします。defaultPath
の plot
フィールドは、query
フィールドで指定されたどちらのフィールドにも一致する結果が見つからない場合にのみ検索されます。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 }
結果のドキュメントは、次の理由で一致します。
最初のドキュメントでは、
query
ごとに検索するフィールドであるplot
フィールドにcaptain
とkirk
の両方が含まれていますが、一致の条件を満たすにはどちらか 1 つだけが必要です。enterprise
というタームについて、Atlas Search はfullplot
フィールド(plot
フィールドにenterprise
が含まれていないためdefaultPath
)を検索し、fullplot
フィールドでenterprise
を見つけます。2 番目のドキュメントでは、
plot
フィールドに 3 つの検索タームがすべて含まれています。3 番目のドキュメントでは、
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
という用語が含まれていますが、一致条件を満たすために必要なのは 1 つだけで、plot
にはありませんが、defaultPath
の fullplot
には chess
という用語が表示されるため、結果のドキュメントは一致します。
次のクエリは、 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 は次のトークンを作成します。このトークンの少なくとも 1 つ(ルート で示)がクエリ条件に一致します。
タイトル | 標準アナライザ トークン |
---|---|
ブラックメソッド フィールド |
|
お茶の特権 |
|
lucene.keyword
アナライザを使用してインデックスを作成すると、Atlas Search はtitle
フィールドの string 全体に対して単一のトークンを作成し、同じクエリに対して次のような結果が得られます。
[ { 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
演算子を使用して、ファジー、ワイルドカード、正規表現を使用して映画タイトルをクエリします。 クエリには、 title
以外のすべてのフィールドを除外する$project
ステージが含まれています。
次のクエリでは、ファジー( ~
)を使用して、 title
フィールドで、最大 2 文字のバリエーションを持つ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 ]
次のクエリは、ワイルドカード式を使用して、 title
フィールドで、文字cou*t?*
を含む映画のタイトルを検索します。ここで、 *
は追加の文字数を、 ?
は任意の 1 文字を示します。
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
に 1 つまたは複数の文字が続く( country
の場合はr
、または結果のcounty
はy
のように)。
次のクエリは正規表現を使用して、title
フィールドで、任意の文字とその後に文字tal
で始まり、y
ian
の後に またはtal
のいずれかを含む映画タイトルを検索します。
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
)のクエリ条件に一致し、その後にtal
y
(結果のItaly
のように)またはian
(結果のItalian
のように)。
メタデータクエリ
次のクエリは、movies
コレクションの title
フィールドで Marvel
、Avengers
、Iron Man
、または Captain
America
を検索します。