Navigation

queryString

queryString

The queryString operator supports querying a combination of indexed fields and values.

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}

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 defaultPath:

  • If you omit the field to search in the query.
  • If a value you specified in the query is not found in the field you specified in the query.
yes
query
string

One or more indexed fields and values to search. Fields and values are colon-delimited. For example, to search the plot field for the string baseball, use plot:baseball. The following operators are available to combine multiple search criteria:

You can combine the fields and values using the following:

AND
Indicates AND boolean operator. All search values must be present for a document to be included in the results.
OR
Indicates OR boolean operator. At least one of the search value must be present for a document to be included in the results.
()
Delimiters for subqueries. Use the parentheses to group fields and values to search.
Tip
See also:

Examples for some sample queries that use the operators and delimiters to search the sample movies collection.

yes
score
object

The score assigned to matching search results. You can modify the score using the following options:

  • boost: multiply the result score by the given number.
  • constant: replace the result score with the given number.

For information on using score in your query, see Scoring.

no

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.

The following example uses the queryString operator to query for movies with the title Rocky and either IV, 4, or Four.

Example

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.

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])

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" }

The following example uses the queryString operator to query for movies with the title The Italian in the Drama genre.

Example

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, and genres
  • Add a field named 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])

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}

The following example uses the queryString operator to query for movie plots that contain the terms captain or kirk and enterprise.

Example

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, and fullplot
  • Add a field named 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])

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 the query, contains both captain and kirk, although only either one is required to meet the criteria for a match. For the term enterprise, Atlas Search searches the fullplot field, which is the defaultPath, because the plot field does not contain enterprise, and finds enterprise in the fullplot field.
  • In the second document, the plot field contains all three search terms.
  • In the third document, the plot field contains kirk and enterprise.

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.

Example

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, and fullpath
  • Add a field named score
Give Feedback
←  phraserange →

On this page

  • Definition
  • Syntax
  • Options
  • Examples