queryString¶
Definition¶
queryString
¶The
queryString
operator supports querying a combination of indexed fields and values.
Syntax¶
queryString
has the following syntax:
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>|<field-to-search>: <search-values>)" 7 } 8 } 9 }
Options¶
queryString
uses the following terms to construct a query:
Field | Type | Description | Necessity | ||||||
---|---|---|---|---|---|---|---|---|---|
defaultPath | string | The indexed field to search by default. Atlas Search only searches the
field in
| yes | ||||||
query | string | One or more indexed fields and values to search. Fields and
values are colon-delimited. For example, to search the You can combine the fields and values using the following:
Tip See Also: Examples for some sample queries that
use the operators and delimiters to search the sample
| yes | ||||||
score | object | The score assigned to matching search results. You can modify the score using the following options:
For information on using | no |
Examples¶
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 Atlas Search
default
index and run the queries on your cluster. The
Tutorial: Create and Query an Atlas Search Index contains instructions for loading the
sample dataset, creating an
index definition, and running Atlas Search
queries.
Example 1¶
The following example uses the queryString
operator to query for
movies with the title Rocky
and either IV
, 4
, or Four
.
The following query searches for the combination of 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
.
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 ])
For the above query, Atlas Search performs a search at defaultPath
because there are no fields in the query
. It returns the
following results:
{ "title" : "Rocky IV" }
Example 2¶
The following example uses the queryString
operator to query for
movies with the title The Italian
in the Drama
genre.
The following query searches a combination of fields for movies with
the title The Italian
in the Drama
genre. The plot
field
in the defaultPath
will be searched only if no matching results
can be found in either of the fields specified in the query
field. The query
field contains escaped double quotes as
delimiters for the phrase to search.
The query includes a $project
stage to:
- Exclude all fields except
_id
,title
,plot
, andgenres
- Add a field named
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 ])
The above query returns the following results:
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 }
Example 3¶
The following example uses the queryString
operator to query for
movie plots that contain the terms captain
or kirk
and
enterprise
.
The following query searches for a combination of terms, captain
or kirk
and enterprise
, in the plot
field. It includes a
$limit
stage to limit the output to 3 results
and a $project
stage to:
- Exclude all fields except
title
,plot
, andfullplot
- Add a field named
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 ])
The above query returns the following results:
1 { "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.", 2 "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.", 3 "title" : "Star Trek: Generations", 4 "score" : 11.274821281433105 } 5 { "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.", 6 "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.", 7 "title" : "Star Trek V: The Final Frontier", 8 "score" : 9.889547348022461 } 9 { "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.", 10 "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?", 11 "title" : "Star Trek: The Motion Picture", 12 "score" : 8.322310447692871 }
The documents in the above results match because:
- In the first document, the
plot
field, which is the field to search per thequery
, contains bothcaptain
andkirk
, although only either one is required to meet the criteria for a match. For the termenterprise
, Atlas Search searches thefullplot
field, which is thedefaultPath
, because theplot
field does not containenterprise
, and findsenterprise
in thefullplot
field. - In the second document, the
plot
field contains all three search terms. - In the third document, the
plot
field containskirk
andenterprise
.
Example 4¶
The following example uses the queryString
operator to query for
movie plots that contain the terms captain
, kirk
, and
chess
. This example shows how different grouping of the same search
terms using parentheses can result in different documents to be
included in the search results.
The following queries search for a combination of the terms,
captain
, kirk
, and chess
, in the plot
field. It
includes a $project
stage to:
- Exclude all fields except
title
,plot
, andfullpath
- Add a field named
score
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 ])
The above query returns the following results:
{ "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 }
The document in the above results is a match because the
plot
contains the terms captain
and kirk
, although
only one is needed to meet the criteria for a match, and the
term chess
, although not in the plot
, appears in the
fullplot
, which is the defaultPath
.