How to Run Atlas Search Compound Queries with Weighted Fields
On this page
This tutorial demonstrates how to add weights to your search fields to boost or bury the documents in the results or a category of results. It demonstrates how to assign one or more values to a field to return results with a higher or lower score.
Specifically, the tutorial shows how to create an index with dynamic
mapping on the sample_mflix.movies
collection. It shows how to run
compound queries and alter the score using constant
,
function
, and boost
. It takes you through
the following steps:
Set up an Atlas Search index with dynamic mapping for the
sample_mflix.movies
collection.Run the following Atlas Search queries:
Query the
year
field and alter the score using specific a word in thetitle
field to boost the document in the results.Query the
title
andplot
fields and alter the score based on a specific genre in thegenres
field to bury results in that genre.
Before you begin, ensure that your Atlas cluster meets the requirements described in the Prerequisites.
To create an Atlas Search index, you must have Project Data Access Admin
or higher access to the project.
Create the Atlas Search Index With Dynamic Mapping
In this section, you will create an Atlas Search index that uses dynamic
mapping to index the fields in the sample_mflix.movies
collection.
In Atlas, go to the Clusters page for your project.
If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
If it's not already displayed, select your desired project from the Projects menu in the navigation bar.
If it's not already displayed, click Clusters in the sidebar.
The Clusters page displays.
Go to the Atlas Search page for your cluster.
You can go the Atlas Search page from the sidebar, the Data Explorer, or your cluster details page.
In the sidebar, click Atlas Search under the Services heading.
From the Select data source dropdown, select your cluster and click Go to Atlas Search.
The Atlas Search page displays.
Click the Browse Collections button for your cluster.
Expand the database and select the collection.
Click the Search Indexes tab for the collection.
The Atlas Search page displays.
Click the cluster's name.
Click the Atlas Search tab.
The Atlas Search page displays.
Enter the Index Name, and set the Database and Collection.
In the Index Name field, enter
compound-query-custom-score-tutorial
.If you name your index
default
, you don't need to specify anindex
parameter in the $search pipeline stage. If you give a custom name to your index, you must specify this name in theindex
parameter.In the Database and Collection section, find the
sample_mflix
database, and select themovies
collection.
Specify an index definition.
You can create an Atlas Search index that uses dynamic mappings or static mappings. To learn more about dynamic and static mappings, see Static and Dynamic Mappings.
The following index definition dynamically indexes the fields of
supported types in the movies
collection. You can use the Atlas Search Visual Editor or the
Atlas Search JSON Editor in the Atlas user interface to create the
index.
Visual Editor
Click Next.
Review the
"default"
index definition for themovies
collection.
JSON Editor
Click Next.
Review the index definition.
Your index definition should look similar to the following:
{ "mappings": { "dynamic": true } } The above index definition dynamically indexes the fields of supported types in each document in the
movies
collection.Click Next.
Run Compound Queries
You use the compound operator to combine two or more operators into a single query. Atlas Search assigns a score based on relevance, in order from highest score to lowest, to every document that it returns for your query. The queries demonstrate how to boost or bury the documents in the results.
➤ Use the Select your language drop-down menu to set the language of the examples in this section.
Tip
Atlas Search provides a sample compound query template with guidance. To learn more, see View Query Guidance Template.
Run a Compound Query to Boost Results
In this section, you connect to your Atlas cluster and run
the sample queries using the compound operator against the
title
and year
fields in the sample_mflix.movies
collection.
The sample queries use custom scoring to alter the relevance score
returned by Atlas Search for movie titles that contain the term snow
.
In Atlas, go to the Clusters page for your project.
If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
If it's not already displayed, select your desired project from the Projects menu in the navigation bar.
If it's not already displayed, click Clusters in the sidebar.
The Clusters page displays.
Go to the Atlas Search page for your cluster.
You can go the Atlas Search page from the sidebar, the Data Explorer, or your cluster details page.
In the sidebar, click Atlas Search under the Services heading.
From the Select data source dropdown, select your cluster and click Go to Atlas Search.
The Atlas Search page displays.
Click the Browse Collections button for your cluster.
Expand the database and select the collection.
Click the Search Indexes tab for the collection.
The Atlas Search page displays.
Click the cluster's name.
Click the Atlas Search tab.
The Atlas Search page displays.
Run the following Atlas Search queries with the compound
operator on the movies
collection.
Copy and paste the following query into the Query Editor, and then click the Search button in the Query Editor.
The following examples use the compound
operator with subqueries
to search for movies between the years 2013
to 2015
with the
term snow
in the title
field.
The following query:
Uses the following
compound
operator clauses:Specifies the highlight option to return snippets of text from the
title
field that match the query. The snippets contain matching text specified withtype: 'hit'
, and adjacent text specified withtype: 'text'
.
[ { $search: { index: "compound-query-custom-score-tutorial", compound: { filter: [{ range: { path: "year", gte: 2013, lte: 2015 } }], should: [{ text: { query: "snow", path: "title", score: {constant: {value: 5}} } }] }, highlight:{ path: "title" } } } ]
SCORE: 5 _id: "573a13d7f29313caabda38ad" Snow in Paradise Matching fields: title SCORE: 5 _id: "573a13e2f29313caabdbeded" Dead Snow 2: red vs. Matching fields: title SCORE: 5 _id: "573a13e6f29313caabdc66c4" The Snow White Murder Case Matching fields: title SCORE: 5 _id: "573a13edf29313caabdd37bd" Snow on the Blades Matching fields: title SCORE: 0 _id: "573a13acf29313caabd29366" No highlights found. Matching fields: unknown SCORE: 0 _id: "573a13adf29313caabd2b765" No highlights found. Matching fields: unknown SCORE: 0 _id: "573a13b0f29313caabd333e7" No highlights found. Matching fields: unknown SCORE: 0 _id: "573a13b0f29313caabd3486a" No highlights found. Matching fields: unknown SCORE: 0 _id: "573a13b1f29313caabd3719d" No highlights found. Matching fields: unknown SCORE: 0 _id: "573a13b2f29313caabd3abb9" No highlights found. Matching fields: unknown
The first four documents in the results have a higher score
because the should
clause in the query specifies a preference for
documents with snow
in the title. The should
clause also
alters the score for the query term snow
using the constant
option.
The following query:
Uses the following
compound
operator clauses:Specifies the highlight option to return snippets of text from the
title
field that match the query. The snippets contain matching text specified withtype: 'hit'
, and adjacent text specified withtype: 'text'
.
[ { $search: { index: "compound-query-custom-score-tutorial", compound: { must: [{ range: { path: "year", gte: 2013, lte: 2015 } }], should: [{ text: { query: "snow", path: "title", score: {boost: {value: 2}} } }] }, highlight:{ path: "title" } } } ]
SCORE: 6.7722930908203125 _id: "573a13d7f29313caabda38ad" Snow in Paradise Matching fields: title SCORE: 6.063445568084717 _id: "573a13edf29313caabdd37bd" Snow on the Blades Matching fields: title SCORE: 5.509652137756348 _id: "573a13e6f29313caabdc66c4" The Snow White Murder Case Matching fields: title SCORE: 5.065053939819336 _id: "573a13e2f29313caabdbeded" Dead Snow 2: Red vs. Matching fields: title SCORE: 1 _id: "573a13acf29313caabd29366" No highlights found. Matching fields: unknown SCORE: 1 _id: "573a13adf29313caabd2b765" No highlights found. Matching fields: unknown SCORE: 1 _id: "573a13b0f29313caabd333e7" No highlights found. Matching fields: unknown SCORE: 1 _id: "573a13b0f29313caabd3486a" No highlights found. Matching fields: unknown SCORE: 1 _id: "573a13b1f29313caabd3719d" No highlights found. Matching fields: unknown SCORE: 1 _id: "573a13b2f29313caabd3abb9" No highlights found. Matching fields: unknown
The first four documents in the results have a higher score
because the should
clause in the query specifies a preference for
documents with snow
in the title. The should
clause also
alters the score for the query term snow
using the boost
option.
The following query:
Uses the following
compound
operator clauses with theboost
option to prioritize some fields more than other fields:must
clause with the text operator to prioritize the genrecomedy
the most, followed by the termsnow
in thetitle
field. Theboost
option applies weights to the fields.should
clause with the range operator to search for movies between the years2013
to2015
.
Note
The
boost
option applies different weights to the fields to prioritize the fields.
[ { $search: { index: "compound-query-custom-score-tutorial", compound: { must: [{ text: { query: "comedy", path: "genres", score: {boost: {value: 9}} } }, { text: { query: "snow", path: "title", score: {boost: {value: 5}} } }], should: [{ range: { path: "year", gte: 2013, lte: 2015, score: {boost: {value: 3}} } }] } } } ]
SCORE: 21.872983932495117 _id: "573a13c2f29313caabd6874c" plot: "A ski vacation turns horrific for a group of medical students, as they…" genres: Array runtime: 91 SCORE: 21.043487548828125 _id: "573a139ff29313caabcffff8" fullplot: "When an entire town in upstate New York is closed down by an unexpecte…" imdb: Object year: 2000 SCORE: 21.043487548828125 _id: "573a13a6f29313caabd16b02" plot: "When a Miami dentist inherits a team of sled dogs, he's got to learn t…" genres: Array runtime: 99 SCORE: 19.523927688598633 _id: "573a13a1f29313caabd06765" fullplot: "Our two young lovers meet on a series of snowy days in high school. Ye…" imdb: Object runtime: 1999 SCORE: 17.426334381103516 _id: "573a13e2f29313caabdbeded" plot: "Still on the run from a group of Nazi zombies, a man seeks the aid of …" genres: Array runtime: 100 SCORE: 16.367326736450195 _id: "573a13c2f29313caabd6688e" countries: Array genres: Array runtime: 108 SCORE: 15.537829399108887 _id: "573a13b1f29313caabd36d7d" plot: "A love-struck Italian poet is stuck in Iraq at the onset of an America…" genres: Array runtime: 110 SCORE: 14.4263334274292 _id: "573a1395f29313caabce1925" plot: "An ice-skating Snow White finds refuge from the Wicked Queen with the …" genres: Array runtime: 107
The following query:
Uses the following
compound
operator clauses:must
clause with the range operator to search for movies between the years2013
to2015
.should
clause with the text operator to query for the termsnow
in thetitle
field and alter thescore
with thefunction
option. Thefunction
option adds the following using an arithmetic expression:The relevance score of the query for the search term
The value of the numeric field named
imdb.rating
or the number2
for those documents that do not have theimdb.rating
field.
Specifies the highlight option to return snippets of text from the
title
field that match the query. The snippets contain matching text specified withtype: 'hit'
, and adjacent text specified withtype: 'text'
.
[ { $search: { index: "compound-query-custom-score-tutorial", compound: { must: [{ range: { path: "year", gte: 2013, lte: 2015, } }], should: [{ text: { query: "snow", path: "title", score: { function: { add: [{ path: { value: "imdb.rating", undefined: 2 } }, { score: "relevance" }] } } } }] }, highlight: { path: "title" } } } ]
SCORE: 10.454826354980469 _id: "573a13e6f29313caabdc66c4" The Snow White Murder Case Matching fields: title SCORE: 10.3317232131958 _id: "573a13edf29313caabdd37bd" Snow on the Blades Matching fields: title SCORE: 10.032526969909668 _id: "573a13e2f29313caabdbeded" Dead Snow 2: Red vs. Matching fields: title SCORE: 8.386146545410156 _id: "573a13d7f29313caabda38ad" Snow in Paradise Matching fields: title SCORE: 1 _id: "573a13acf29313caabd29366" No highlights found. Matching fields: unknown SCORE: 1 _id: "573a13adf29313caabd2b765" No highlights found. Matching fields: unknown SCORE: 1 _id: "573a13b0f29313caabd333e7" No highlights found. Matching fields: unknown SCORE: 1 _id: "573a13b0f29313caabd3486a" No highlights found. Matching fields: unknown SCORE: 1 _id: "573a13b1f29313caabd3719d" No highlights found. Matching fields: unknown SCORE: 1 _id: "573a13b2f29313caabd3abb9" No highlights found. Matching fields: unknown
The first four documents in the results have a higher score
because the should
clause in the query specifies a preference for
documents with snow
in the title. The should
clause also
alters the score for the query term snow
using the function
option.
Expand your query results.
The Search Tester might not display all the fields in the documents it returns. To view all the fields, including the field that you specify in the query path, expand the document in the results.
Connect to your cluster in mongosh
.
Open mongosh
in a terminal window and
connect to your cluster. For detailed instructions on connecting,
see Connect via mongosh
.
Use the sample_mflix
database.
Run the following command at mongosh
prompt:
use sample_mflix
Run the following Atlas Search queries with the compound
operator on the movies
collection.
The following examples use the compound
operator with subqueries
to search for movies between the years 2013
to 2015
with the
term snow
in the title
field.
This query uses the following pipeline stages:
$search
to query the collection. The query:Uses the following
compound
operator clauses:Specifies the highlight option to return snippets of text from the
title
field that match the query. The snippets contain matching text specified withtype: 'hit'
, and adjacent text specified withtype: 'text'
.
$limit
stage to limit the output to10
results.$project
stage to:Exclude all fields except
title
andyear
Add a
score
field
db.movies.aggregate([ { "$search": { "index": "compound-query-custom-score-tutorial", "compound": { "filter": [{ "range": { "path": "year", "gte": 2013, "lte": 2015 } }], "should": [{ "text": { "query": "snow", "path": "title", "score": {"constant": {"value": 5}} } }] }, "highlight": { "path": "title" } } }, { "$limit": 10 }, { "$project": { "_id": 0, "title": 1, "year": 1, "score": { "$meta": "searchScore" }, "highlights": { "$meta": "searchHighlights" } } } ])
Atlas Search returns the following results for
constant
:
[ { title: 'Snow in Paradise', year: 2014, score: 5, highlights: [ { score: 1.382846713066101, path: 'title', texts: [ { value: 'Snow', type: 'hit' }, { value: ' in Paradise', type: 'text' } ] } ] }, { title: 'Dead Snow 2: Red vs. Dead', year: 2014, score: 5, highlights: [ { score: 1.3924485445022583, path: 'title', texts: [ { value: 'Dead ', type: 'text' }, { value: 'Snow', type: 'hit' }, { value: ' 2: Red vs. ', type: 'text' } ] } ] }, { title: 'The Snow White Murder Case', year: 2014, score: 5, highlights: [ { score: 1.3525336980819702, path: 'title', texts: [ { value: 'The ', type: 'text' }, { value: 'Snow', type: 'hit' }, { value: ' White Murder Case', type: 'text' } ] } ] }, { title: 'Snow on the Blades', year: 2014, score: 5, highlights: [ { score: 1.3766303062438965, path: 'title', texts: [ { value: 'Snow', type: 'hit' }, { value: ' on the Blades', type: 'text' } ] } ] }, { year: 2013, title: 'The Secret Life of Walter Mitty', score: 0, highlights: [] }, { title: 'Jurassic World', year: 2015, score: 0, highlights: [] }, { title: 'Action Jackson', year: 2014, score: 0, highlights: [] }, { year: 2013, title: 'In Secret', score: 0, highlights: [] }, { title: 'The Stanford Prison Experiment', year: 2015, score: 0, highlights: [] }, { year: 2014, title: 'The Giver', score: 0, highlights: [] } ]
The first four documents in the results have a higher score
because the should
clause in the query specifies a preference for
documents with snow
in the title. The should
clause also
alters the score for the query term snow
using the constant
option.
This query uses the following pipeline stages:
$search
to query the collection. The query:Uses the following
compound
operator clauses:Specifies the highlight option to return snippets of text from the
title
field that match the query. The snippets contain matching text specified withtype: 'hit'
, and adjacent text specified withtype: 'text'
.
$limit
stage to limit the output to10
results.$project
stage to:Exclude all fields except
title
andyear
Add a
score
field
db.movies.aggregate([ { "$search": { "index": "compound-query-custom-score-tutorial", "compound": { "must": [{ "range": { "path": "year", "gte": 2013, "lte": 2015 } }], "should": [{ "text": { "query": "snow", "path": "title", "score": {"boost": {"value": 2}} } }] }, "highlight": { "path": "title" } } }, { "$limit": 10 }, { "$project": { "_id": 0, "title": 1, "year": 1, "score": { "$meta": "searchScore" }, "highlights": { "$meta": "searchHighlights" } } } ])
Atlas Search returns the following results for
boost
:
[ { title: 'Snow in Paradise', year: 2014, score: 6.7722930908203125, highlights: [ { score: 1.382846713066101, path: 'title', texts: [ { value: 'Snow', type: 'hit' }, { value: ' in Paradise', type: 'text' } ] } ] }, { title: 'Snow on the Blades', year: 2014, score: 6.063445568084717, highlights: [ { score: 1.3766303062438965, path: 'title', texts: [ { value: 'Snow', type: 'hit' }, { value: ' on the Blades', type: 'text' } ] } ] }, { title: 'The Snow White Murder Case', year: 2014, score: 5.509652137756348, highlights: [ { score: 1.3525336980819702, path: 'title', texts: [ { value: 'The ', type: 'text' }, { value: 'Snow', type: 'hit' }, { value: ' White Murder Case', type: 'text' } ] } ] }, { title: 'Dead Snow 2: Red vs. Dead', year: 2014, score: 5.065053939819336, highlights: [ { score: 1.3924485445022583, path: 'title', texts: [ { value: 'Dead ', type: 'text' }, { value: 'Snow', type: 'hit' }, { value: ' 2: Red vs. ', type: 'text' } ] } ] }, { year: 2013, title: 'The Secret Life of Walter Mitty', score: 1, highlights: [] }, { title: 'Jurassic World', year: 2015, score: 1, highlights: [] }, { title: 'Action Jackson', year: 2014, score: 1, highlights: [] }, { year: 2013, title: 'In Secret', score: 1, highlights: [] }, { title: 'The Stanford Prison Experiment', year: 2015, score: 1, highlights: [] }, { year: 2014, title: 'The Giver', score: 1, highlights: [] } ]
The first four documents in the results have a higher score
because the should
clause in the query specifies a preference for
documents with snow
in the title. The should
clause also
alters the score for the query term snow
using the boost
option.
This query uses the following pipeline stages:
$search
to query the collection. The query uses the followingcompound
operator clauses with theboost
option to prioritize some fields more than other fields:must
clause with the text operator to prioritize the genrecomedy
the most, followed by the termsnow
in thetitle
field. Theboost
option applies weights to the fields.should
clause with the range operator to search for movies between the years2013
to2015
.
Note
The
boost
option applies different weights to the fields to prioritize the fields.$limit
stage to limit the output to10
results.$project
stage to:Exclude all fields except
title
,year
, andgenres
Add a
score
field
db.movies.aggregate([ { "$search": { "index": "compound-query-custom-score-tutorial", "compound": { "must": [{ "text": { "path": "genres", "query": "comedy", "score": {"boost": {"value": 9}} } }, { "text": { "path": "title", "query": "snow", "score": {"boost": {"value": 5}} } }], "should": [{ "range": { "path": "year", "gte": 2013, "lte": 2015, "score": {"boost": {"value": 3}} } }] } } }, { "$limit": 10 }, { "$project": { "_id": 0, "title": 1, "year": 1, "genres": 1, "score": { "$meta": "searchScore" } } } ])
[ { genres: [ 'Comedy', 'Horror' ], title: 'Dead Snow', year: 2009, score: 21.872983932495117 }, { year: 2000, genres: [ 'Adventure', 'Comedy', 'Family' ], title: 'Snow Day', score: 21.043487548828125 }, { genres: [ 'Adventure', 'Comedy', 'Family' ], title: 'Snow Dogs', year: 2002, score: 21.043487548828125 }, { year: 1999, genres: [ 'Comedy', 'Romance' ], title: 'Let It Snow', score: 19.523927688598633 }, { genres: [ 'Action', 'Comedy', 'Horror' ], title: 'Dead Snow 2: Red vs. Dead', year: 2014, score: 17.426334381103516 }, { genres: [ 'Comedy', 'Drama' ], title: 'Snow White and Russian Red', year: 2009, score: 16.367326736450195 }, { genres: [ 'Comedy', 'Drama', 'Romance' ], title: 'The Tiger and the Snow', year: 2005, score: 15.537829399108887 }, { genres: [ 'Adventure', 'Comedy', 'Family' ], title: 'Snow White and the Three Stooges', year: 1961, score: 14.4263334274292 } ]
This query uses the following pipeline stages:
$search
to query the collection. The query:Uses the following
compound
operator clauses:must
clause with the range operator to search for movies between the years2013
to2015
.should
clause with the text operator to query for the termsnow
in thetitle
field and alter thescore
with thefunction
option. Thefunction
option adds the following using an arithmetic expression:The relevance score of the query for the search term
The value of the numeric field named
imdb.rating
or the number2
for those documents that do not have theimdb.rating
field.
Specifies the highlight option to return snippets of text from the
title
field that match the query. The snippets contain matching text specified withtype: 'hit'
, and adjacent text specified withtype: 'text'
.
$limit
stage to limit the output to10
results.$project
stage to:Exclude all fields except
title
andyear
Add a
score
field
db.movies.aggregate([ { "$search": { "index": "compound-query-custom-score-tutorial", "compound": { "must": [{ "range": { "path": "year", "gte": 2013, "lte": 2015 } }], "should": [{ "text": { "query": "snow", "path": "title", "score": { "function": { "add": [{ "path": { "value": "imdb.rating", "undefined": 2 } }, { "score": "relevance" }] } } } }] }, "highlight": { "path": "title" } } }, { "$limit": 10 }, { "$project": { "_id": 0, "title": 1, "year": 1, "score": { "$meta": "searchScore" }, "highlights": { "$meta": "searchHighlights" } } } ])
Atlas Search returns the following results for
function
:
[ { title: 'The Snow White Murder Case', year: 2014, score: 10.454826354980469, highlights: [ { score: 1.3525336980819702, path: 'title', texts: [ { value: 'The ', type: 'text' }, { value: 'Snow', type: 'hit' }, { value: ' White Murder Case', type: 'text' } ] } ] }, { title: 'Snow on the Blades', year: 2014, score: 10.3317232131958, highlights: [ { score: 1.3766303062438965, path: 'title', texts: [ { value: 'Snow', type: 'hit' }, { value: ' on the Blades', type: 'text' } ] } ] }, { title: 'Dead Snow 2: Red vs. Dead', year: 2014, score: 10.032526969909668, highlights: [ { score: 1.3924485445022583, path: 'title', texts: [ { value: 'Dead ', type: 'text' }, { value: 'Snow', type: 'hit' }, { value: ' 2: Red vs. ', type: 'text' } ] } ] }, { title: 'Snow in Paradise', year: 2014, score: 8.386146545410156, highlights: [ { score: 1.382846713066101, path: 'title', texts: [ { value: 'Snow', type: 'hit' }, { value: ' in Paradise', type: 'text' } ] } ] }, { year: 2013, title: 'The Secret Life of Walter Mitty', score: 1, highlights: [] }, { title: 'Jurassic World', year: 2015, score: 1, highlights: [] }, { title: 'Action Jackson', year: 2014, score: 1, highlights: [] }, { year: 2013, title: 'In Secret', score: 1, highlights: [] }, { title: 'The Stanford Prison Experiment', year: 2015, score: 1, highlights: [] }, { year: 2014, title: 'The Giver', score: 1, highlights: [] } ]
The first four documents in the results have a higher score
because the should
clause in the query specifies a preference for
documents with snow
in the title. The should
clause also
alters the score for the query term snow
using the function
option.
Connect to your cluster in MongoDB Compass.
Open MongoDB Compass and connect to your cluster. For detailed instructions on connecting, see Connect via Compass.
Run an Atlas Search compound query that alters the score using the constant
option.
The query performs the following tasks:
Uses the following
compound
operator clauses:Specifies the highlight option to return snippets of text from the
title
field that match the query. The snippets contain matching text specified withtype: 'hit'
, and adjacent text specified withtype: 'text'
.Uses the following pipeline stages:
To run this query in MongoDB Compass:
Click the Aggregations tab.
Click Select..., then configure each of the following pipeline stages by selecting the stage from the dropdown and adding the query for that stage. Click Add Stage to add additional stages.
Pipeline StageQuery$search
{ "index": "compound-query-custom-score-tutorial", "compound": { "filter": [{ "range": { "path": "year", "gte": 2013, "lte": 2015 } }], "should": [{ "text": { "query": "snow", "path": "title", "score": {"constant": {"value": 5}} } }] }, "highlight": { "path": "title" } } $limit
10 $project
{ "_id": 0, "title": 1, "year": 1, "score": { "$meta": "searchScore" }, "highlights": { "$meta": "searchHighlights" } } If you enabled Auto Preview, MongoDB Compass displays the following documents next to the
$project
pipeline stage:{ title: 'Snow in Paradise', year: 2014, score: 5, highlights: [{ score: 1.382846713066101, path: 'title', texts: [ { value: 'Snow', type: 'hit' }, { value: ' in Paradise', type: 'text' } ] }] }, { title: 'Dead Snow 2: Red vs. Dead', year: 2014, score: 5, highlights: [{ score: 1.3924485445022583, path: 'title', texts: [ { value: 'Dead ', type: 'text' }, { value: 'Snow', type: 'hit' }, { value: ' 2: Red vs. ', type: 'text' } ] }] }, { title: 'The Snow White Murder Case', year: 2014, score: 5, highlights: [{ score: 1.3525336980819702, path: 'title', texts: [ { value: 'The ', type: 'text' }, { value: 'Snow', type: 'hit' }, { value: ' White Murder Case', type: 'text' } ] }] }, { title: 'Snow on the Blades', year: 2014, score: 5, highlights: [{ score: 1.3766303062438965, path: 'title', texts: [ { value: 'Snow', type: 'hit' }, { value: ' on the Blades', type: 'text' } ] }] }, { year: 2013, title: 'The Secret Life of Walter Mitty', score: 0, highlights: [] }, { title: 'Jurassic World', year: 2015, score: 0, highlights: [] }, { title: 'Action Jackson', year: 2014, score: 0, highlights: [] }, { year: 2013, title: 'In Secret', score: 0, highlights: [] }, { title: 'The Stanford Prison Experiment', year: 2015, score: 0, highlights: [] }, { year: 2014, title: 'The Giver', score: 0, highlights: [] } The first four documents in the results have a higher score because the
should
clause in the query specifies a preference for documents withsnow
in the title. Theshould
clause also alters the score for the query termsnow
using theconstant
option.
Run Atlas Search compound queries that alter the score using the boost
option.
Click the Aggregations tab.
Click Select..., then configure each of the following pipeline stages by selecting the stage from the dropdown and adding the query for that stage. Click Add Stage to add additional stages.
The query uses the following pipeline stages:
$search
to perform the following tasks:Queries using the following
compound
operator clauses:Specifies the highlight option to return snippets of text from the
title
field that match the query. The snippets contain matching text specified withtype: 'hit'
, and adjacent text specified withtype: 'text'
.
$limit stage to limit the output to 10 results
$project stage to:
Exclude all fields except
title
andyear
Add two fields named
score
andhighlights
Pipeline StageQuery$search
{ "index": "compound-query-custom-score-tutorial", "compound": { "must": [{ "range": { "path": "year", "gte": 2013, "lte": 2015 } }], "should": [{ "text": { "query": "snow", "path": "title", "score": {"boost": {"value": 2}} } }] }, "highlight": { "path": "title" } } $limit
10 $project
{ "_id": 0, "title": 1, "year": 1, "score": { "$meta": "searchScore" }, "highlights": { "$meta": "searchHighlights" } } This query uses the following pipeline stages:
$search
to perform the following tasks:Queries using the
compound
operator clauses with theboost
option to prioritize some fields more than other fields:must
clause with the text operator to prioritize the genrecomedy
the most, followed by the termsnow
in thetitle
field. Theboost
option applies weights to the fields.should
clause with the range operator to search for movies between the years2013
to2015
.
Note
The
boost
option applies different weights to the fields to prioritize the fields.
$limit stage to limit the output to 10 results
$project stage to:
Exclude all fields except
title
andyear
Add two fields named
score
andhighlights
must
clause with the text operator to prioritize the genrecomedy
the most, followed by the termsnow
in thetitle
field. Theboost
option applies weights to the fields.should
clause with the range operator to search for movies between the years2013
to2015
.
Note
The
boost
option applies different weights to the fields to prioritize the fields.Pipeline StageQuery$search
{ "index": "compound-query-custom-score-tutorial", "compound": { "must": [{ "text": { "path": "genres", "query": "comedy", "score": {"boost": {"value": 9}} } }, { "text": { "path": "title", "query": "snow", "score": {"boost": {"value": 5}} } }], "should": [{ "range": { "path": "year", "gte": 2013, "lte": 2015, "score": {"boost": {"value": 3}} } }] } } $limit
10 $project
{ "_id": 0, "title": 1, "year": 1, "genres": 1, "score": { "$meta": "searchScore" } } If you enabled Auto Preview, MongoDB Compass displays the following documents next to the
$project
pipeline stage:{ title: 'Snow in Paradise', year: 2014, score: 6.7722930908203125, highlights: [{ score: 1.382846713066101, path: 'title', texts: [ { value: 'Snow', type: 'hit' }, { value: ' in Paradise', type: 'text' } ] }] }, { title: 'Snow on the Blades', year: 2014, score: 6.063445568084717, highlights: [{ score: 1.3766303062438965, path: 'title', texts: [ { value: 'Snow', type: 'hit' }, { value: ' on the Blades', type: 'text' } ] }] }, { title: 'The Snow White Murder Case', year: 2014, score: 5.509652137756348, highlights: [{ score: 1.3525336980819702, path: 'title', texts: [ { value: 'The ', type: 'text' }, { value: 'Snow', type: 'hit' }, { value: ' White Murder Case', type: 'text' } ] }] }, { title: 'Dead Snow 2: Red vs. Dead', year: 2014, score: 5.065053939819336, highlights: [{ score: 1.3924485445022583, path: 'title', texts: [ { value: 'Dead ', type: 'text' }, { value: 'Snow', type: 'hit' }, { value: ' 2: Red vs. ', type: 'text' } ] }] }, { year: 2013, title: 'The Secret Life of Walter Mitty',score: 1, highlights: [] }, { title: 'Jurassic World', year: 2015, score: 1, highlights: [] }, { title: 'Action Jackson', year: 2014, score: 1, highlights: [] }, { year: 2013, title: 'In Secret', score: 1, highlights: [] }, { title: 'The Stanford Prison Experiment', year: 2015, score: 1, highlights: [] }, { year: 2014, title: 'The Giver', score: 1, highlights: [] } The first four documents in the results have a higher score because the
should
clause in the query specifies a preference for documents withsnow
in the title. Theshould
clause also alters the score for the query termsnow
using theboost
option.[ { genres: [ 'Comedy', 'Horror' ], title: 'Dead Snow', year: 2009, score: 21.872983932495117 }, { year: 2000, genres: [ 'Adventure', 'Comedy', 'Family' ], title: 'Snow Day', score: 21.043487548828125 }, { genres: [ 'Adventure', 'Comedy', 'Family' ], title: 'Snow Dogs', year: 2002, score: 21.043487548828125 }, { year: 1999, genres: [ 'Comedy', 'Romance' ], title: 'Let It Snow', score: 19.523927688598633 }, { genres: [ 'Action', 'Comedy', 'Horror' ], title: 'Dead Snow 2: Red vs. Dead', year: 2014, score: 17.426334381103516 }, { genres: [ 'Comedy', 'Drama' ], title: 'Snow White and Russian Red', year: 2009, score: 16.367326736450195 }, { genres: [ 'Comedy', 'Drama', 'Romance' ], title: 'The Tiger and the Snow', year: 2005, score: 15.537829399108887 }, { genres: [ 'Adventure', 'Comedy', 'Family' ], title: 'Snow White and the Three Stooges', year: 1961, score: 14.4263334274292 } ]
Run an Atlas Search compound query that alters the score using the function
option.
The query uses the following pipeline stages:
$search
stage to perform the following tasks:Queries using the following
compound
operator clauses:must
clause with the range operator to search for movies between the years2013
to2015
.should
clause with the text operator to query for the termsnow
in thetitle
field and alter thescore
with thefunction
option. Thefunction
option adds the following using an arithmetic expression:The relevance score of the query for the search term
The value of the numeric field named
imdb.rating
or the number2
for those documents that do not have theimdb.rating
field.
Specifies the highlight option to return snippets of text from the
title
field that match the query. The snippets contain matching text specified withtype: 'hit'
, and adjacent text specified withtype: 'text'
.
$limit stage to limit the output to 10 results
$project stage to:
Exclude all fields except
title
andyear
Add two fields named
score
andhighlights
To run this query in MongoDB Compass:
Click the Aggregations tab.
Click Select..., then configure each of the following pipeline stages by selecting the stage from the dropdown and adding the query for that stage. Click Add Stage to add additional stages.
Pipeline StageQuery$search
{ "index": "compound-query-custom-score-tutorial", "compound": { "must": [{ "range": { "path": "year", "gte": 2013, "lte": 2015 } }], "should": [{ "text": { "query": "snow", "path": "title", "score": { "function": { "add": [{ "path": { "value": "imdb.rating", "undefined": 2 } }, { "score": "relevance" }] } } } }]}, "highlight":{ "path": "title" } } $limit
10 $project
{ "_id": 0, "title": 1, "year": 1, "score": { "$meta": "searchScore" }, "highlights": { "$meta": "searchHighlights" } } If you enabled Auto Preview, MongoDB Compass displays the following documents next to the
$project
pipeline stage:{ title: 'The Snow White Murder Case', year: 2014, score: 10.454826354980469, highlights: [{ score: 1.3525336980819702, path: 'title', texts: [ { value: 'The ', type: 'text' }, { value: 'Snow', type: 'hit' }, { value: ' White Murder Case', type: 'text' } ] }] }, { title: 'Snow on the Blades', year: 2014, score: 10.3317232131958, highlights: [{ score: 1.3766303062438965, path: 'title', texts: [ { value: 'Snow', type: 'hit' }, { value: ' on the Blades', type: 'text' } ] }] }, { title: 'Dead Snow 2: Red vs. Dead', year: 2014, score: 10.032526969909668, highlights: [{ score: 1.3924485445022583, path: 'title', texts: [ { value: 'Dead ', type: 'text' }, { value: 'Snow', type: 'hit' }, { value: ' 2: Red vs. ', type: 'text' } ] }] }, { title: 'Snow in Paradise', year: 2014, score: 8.386146545410156, highlights: [{ score: 1.382846713066101, path: 'title', texts: [ { value: 'Snow', type: 'hit' }, { value: ' in Paradise', type: 'text' } ] }] }, { year: 2013, title: 'The Secret Life of Walter Mitty', score: 1, highlights: [] }, { title: 'Jurassic World', year: 2015, score: 1, highlights: [] }, { title: 'Action Jackson', year: 2014, score: 1, highlights: [] }, { year: 2013, title: 'In Secret', score: 1, highlights: [] }, { title: 'The Stanford Prison Experiment', year: 2015, score: 1, highlights: [] }, { year: 2014, title: 'The Giver', score: 1, highlights: [] } The first four documents in the results have a higher score because the
should
clause in the query specifies a preference for documents withsnow
in the title. Theshould
clause also alters the score for the query termsnow
using thefunction
option.
Run an Atlas Search compound query that alters the score using the constant
option.
Create a new directory called
compound-constant-example
and initialize your project with thedotnet new
command.mkdir compound-constant-example cd compound-constant-example dotnet new console Add the .NET/C# Driver to your project as a dependency.
dotnet add package MongoDB.Driver Replace the contents of the
Program.cs
file with the following code.The code example performs the following tasks:
Imports
mongodb
packages and dependencies.Establishes a connection to your Atlas cluster.
Uses the following compound clauses to query the collection:
Specifies the highlight option to return snippets of text from the
title
field that match the query. The snippets contain matching text specified withtype: 'hit'
, and adjacent text specified withtype: 'text'
.Uses the following pipeline stages:
Iterates over the cursor to print the documents that match the query.
1 using MongoDB.Bson; 2 using MongoDB.Bson.Serialization.Attributes; 3 using MongoDB.Bson.Serialization.Conventions; 4 using MongoDB.Driver; 5 using MongoDB.Driver.Search; 6 7 public class CompoundConstantExample 8 { 9 private const string MongoConnectionString = "<connection-string>"; 10 11 public static void Main(string[] args) 12 { 13 // allow automapping of the camelCase database fields to our MovieDocument 14 var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() }; 15 ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true); 16 17 // connect to your Atlas cluster 18 var mongoClient = new MongoClient(MongoConnectionString); 19 var mflixDatabase = mongoClient.GetDatabase("sample_mflix"); 20 var moviesCollection = mflixDatabase.GetCollection<MovieDocument>("movies"); 21 22 // define and run pipeline 23 var results = moviesCollection.Aggregate() 24 .Search(Builders<MovieDocument>.Search.Compound() 25 .Filter(Builders<MovieDocument>.Search.Range(movie => movie.Year, SearchRangeBuilder.Gte(2013).Lte(2015))) 26 .Should(Builders<MovieDocument>.Search.Text(movie => movie.Title, "snow", score: new SearchScoreDefinitionBuilder<MovieDocument>().Constant(5))), 27 new SearchHighlightOptions<MovieDocument>(movie => movie.Title), 28 indexName: "compound-query-custom-score-tutorial") 29 .Project<MovieDocument>(Builders<MovieDocument>.Projection 30 .Include(movie => movie.Title) 31 .Include(movie => movie.Year) 32 .Exclude(movie => movie.Id) 33 .MetaSearchScore(movie => movie.Score) 34 .MetaSearchHighlights("highlights")) 35 .Limit(10) 36 .ToList(); 37 38 // print results 39 foreach (var movie in results) 40 { 41 Console.WriteLine(movie.ToJson()); 42 } 43 } 44 } 45 46 [ ]47 public class MovieDocument 48 { 49 [ ]50 public ObjectId Id { get; set; } 51 public string Title { get; set; } 52 public int Year { get; set; } 53 [ ]54 public List<SearchHighlight> Highlights { get; set; } 55 public double Score { get; set; } 56 } Before you run the sample, replace
<connection-string>
with your Atlas connection string. Ensure that your connection string includes your database user's credentials. To learn more, see Connect via Drivers.Compile and run the
Program.cs
file.dotnet run compound-constant-example.csproj { "title" : "Snow in Paradise", "year" : 2014, "highlights" : [{ "path" : "title", "score" : 1.3828467130661011, "texts" : [{ "type" : "Hit", "value" : "Snow" }, { "type" : "Text", "value" : " in Paradise" }] }], "score" : 5.0 } { "title" : "Dead Snow 2: Red vs. Dead", "year" : 2014, "highlights" : [{ "path" : "title", "score" : 1.3924485445022583, "texts" : [{ "type" : "Text", "value" : "Dead " }, { "type" : "Hit", "value" : "Snow" }, { "type" : "Text", "value" : " 2: Red vs. " }] }], "score" : 5.0 } { "title" : "The Snow White Murder Case", "year" : 2014, "highlights" : [{ "path" : "title", "score" : 1.3525336980819702, "texts" : [{ "type" : "Text", "value" : "The " }, { "type" : "Hit", "value" : "Snow" }, { "type" : "Text", "value" : " White Murder Case" }] }], "score" : 5.0 } { "title" : "Snow on the Blades", "year" : 2014, "highlights" : [{ "path" : "title", "score" : 1.3766303062438965, "texts" : [{ "type" : "Hit", "value" : "Snow" }, { "type" : "Text", "value" : " on the Blades" }] }], "score" : 5.0 } { "title" : "The Secret Life of Walter Mitty", "year" : 2013, "highlights" : [], "score" : 0.0 } { "title" : "Jurassic World", "year" : 2015, "highlights" : [], "score" : 0.0 } { "title" : "Action Jackson", "year" : 2014, "highlights" : [], "score" : 0.0 } { "title" : "In Secret", "year" : 2013, "highlights" : [], "score" : 0.0 } { "title" : "The Stanford Prison Experiment", "year" : 2015, "highlights" : [], "score" : 0.0 } { "title" : "The Giver", "year" : 2014, "highlights" : [], "score" : 0.0 } The first four documents in the results have a higher score because the
should
clause in the query specifies a preference for documents withsnow
in the title. Theshould
clause also alters the score for the query termsnow
using theconstant
option.
Run Atlas Search compound queries that alter the score using the boost
option.
Create a new directory called
compound-boost-example
and initialize your project with thedotnet new
command.mkdir compound-boost-example cd compound-boost-example dotnet new console Add the .NET/C# Driver to your project as a dependency.
dotnet add package MongoDB.Driver Replace the contents of the
Program.cs
file with the following code.The code example performs the following tasks:
Imports
mongodb
packages and dependencies.Establishes a connection to your Atlas cluster.
Iterates over the cursor to print the documents that match the query.
The query uses the following pipeline stages:
$search
stage to query the collection. The query:Uses the following
compound
operator clauses:
Specifies the highlight option to return snippets of text from the
title
field that match the query. The snippets contain matching text specified withtype: 'hit'
, and adjacent text specified withtype: 'text'
.
$limit stage to limit the output to 10 results
$project stage to:
Exclude all fields except
title
andyear
Add two fields named
score
andhighlights
1 using MongoDB.Bson; 2 using MongoDB.Bson.Serialization.Attributes; 3 using MongoDB.Bson.Serialization.Conventions; 4 using MongoDB.Driver; 5 using MongoDB.Driver.Search; 6 7 public class CompoundBoostSingleExample 8 { 9 private const string MongoConnectionString = "<connection-string>"; 10 11 public static void Main(string[] args) 12 { 13 // allow automapping of the camelCase database fields to our MovieDocument 14 var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() }; 15 ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true); 16 17 // connect to your Atlas cluster 18 var mongoClient = new MongoClient(MongoConnectionString); 19 var mflixDatabase = mongoClient.GetDatabase("sample_mflix"); 20 var moviesCollection = mflixDatabase.GetCollection<MovieDocument>("movies"); 21 22 // define and run pipeline 23 var results = moviesCollection.Aggregate() 24 .Search(Builders<MovieDocument>.Search.Compound() 25 .Must(Builders<MovieDocument>.Search.Range(movie => movie.Year, SearchRangeBuilder.Gte(2013).Lte(2015))) 26 .Should(Builders<MovieDocument>.Search.Text(movie => movie.Title, "snow", score: new SearchScoreDefinitionBuilder<MovieDocument>().Boost(2))), 27 new SearchHighlightOptions<MovieDocument>(movie => movie.Title), 28 indexName: "compound-query-custom-score-tutorial") 29 .Project<MovieDocument>(Builders<MovieDocument>.Projection 30 .Include(movie => movie.Title) 31 .Include(movie => movie.Year) 32 .Exclude(movie => movie.Id) 33 .MetaSearchScore(movie => movie.Score) 34 .MetaSearchHighlights("highlights")) 35 .Limit(10) 36 .ToList(); 37 38 // print results 39 foreach (var movie in results) 40 { 41 Console.WriteLine(movie.ToJson()); 42 } 43 } 44 } 45 46 [ ]47 public class MovieDocument 48 { 49 [ ]50 public ObjectId Id { get; set; } 51 public string Title { get; set; } 52 public int Year { get; set; } 53 [ ]54 public List<SearchHighlight> Highlights { get; set; } 55 public double Score { get; set; } 56 } This query uses the following
compound
operator clauses with theboost
option to prioritize some fields more than other fields:must
clause with the text operator to prioritize the genrecomedy
the most, followed by the termsnow
in thetitle
field. Theboost
option applies weights to the fields.should
clause with the range operator to search for movies between the years2013
to2015
.
Note
The
boost
option applies different weights to the fields to prioritize the fields.1 using MongoDB.Bson; 2 using MongoDB.Bson.Serialization.Attributes; 3 using MongoDB.Bson.Serialization.Conventions; 4 using MongoDB.Driver; 5 using MongoDB.Driver.Search; 6 7 public class CompoundBoostMultipleExample 8 { 9 private const string MongoConnectionString = "<connection-string>"; 10 11 public static void Main(string[] args) 12 { 13 // allow automapping of the camelCase database fields to our MovieDocument 14 var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() }; 15 ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true); 16 17 // connect to your Atlas cluster 18 var mongoClient = new MongoClient(MongoConnectionString); 19 var mflixDatabase = mongoClient.GetDatabase("sample_mflix"); 20 var moviesCollection = mflixDatabase.GetCollection<MovieDocument>("movies"); 21 22 // define and run pipeline 23 var results = moviesCollection.Aggregate() 24 .Search(Builders<MovieDocument>.Search.Compound() 25 .Must(Builders<MovieDocument>.Search.Text(movie => movie.Genres, "comedy", score: new SearchScoreDefinitionBuilder<MovieDocument>().Boost(9))) 26 .Must(Builders<MovieDocument>.Search.Text(movie => movie.Title, "snow", score: new SearchScoreDefinitionBuilder<MovieDocument>().Boost(5))) 27 .Should(Builders<MovieDocument>.Search.Range(movie => movie.Year, SearchRangeBuilder.Gte(2013).Lte(2015), score: new SearchScoreDefinitionBuilder<MovieDocument>().Boost(3))), 28 indexName: "compound-query-custom-score-tutorial") 29 .Project<MovieDocument>(Builders<MovieDocument>.Projection 30 .Include(movie => movie.Genres) 31 .Include(movie => movie.Title) 32 .Include(movie => movie.Year) 33 .Exclude(movie => movie.Id) 34 .MetaSearchScore(movie => movie.Score)) 35 .Limit(10) 36 .ToList(); 37 38 // print results 39 foreach (var movie in results) 40 { 41 Console.WriteLine(movie.ToJson()); 42 } 43 } 44 } 45 46 [ ]47 public class MovieDocument 48 { 49 [ ]50 public ObjectId Id { get; set; } 51 public string [] Genres { get; set; } 52 public string Title { get; set; } 53 public int Year { get; set; } 54 public double Score { get; set; } 55 } Before you run the sample, replace
<connection-string>
with your Atlas connection string. Ensure that your connection string includes your database user's credentials. To learn more, see Connect via Drivers.Compile and run the
Program.cs
file.dotnet run compound-boost-example.csproj { "title" : "Snow in Paradise", "year" : 2014, "highlights" : [{ "path" : "title", "score" : 1.3828467130661011, "texts" : [{ "type" : "Hit", "value" : "Snow" }, { "type" : "Text", "value" : " in Paradise" }] }], "score" : 6.7722930908203125 } { "title" : "Snow on the Blades", "year" : 2014, "highlights" : [{ "path" : "title", "score" : 1.3766303062438965, "texts" : [{ "type" : "Hit", "value" : "Snow" }, { "type" : "Text", "value" : " on the Blades" }] }], "score" : 6.0634455680847168 } { "title" : "The Snow White Murder Case", "year" : 2014, "highlights" : [{ "path" : "title", "score" : 1.3525336980819702, "texts" : [{ "type" : "Text", "value" : "The " }, { "type" : "Hit", "value" : "Snow" }, { "type" : "Text", "value" : " White Murder Case" }] }], "score" : 5.5096521377563477 } { "title" : "Dead Snow 2: Red vs. Dead", "year" : 2014, "highlights" : [{ "path" : "title", "score" : 1.3924485445022583, "texts" : [{ "type" : "Text", "value" : "Dead " }, { "type" : "Hit", "value" : "Snow" }, { "type" : "Text", "value" : " 2: Red vs. " }] }], "score" : 5.0650539398193359 } { "title" : "The Secret Life of Walter Mitty", "year" : 2013, "highlights" : [], "score" : 1.0 } { "title" : "Jurassic World", "year" : 2015, "highlights" : [], "score" : 1.0 } { "title" : "Action Jackson", "year" : 2014, "highlights" : [], "score" : 1.0 } { "title" : "In Secret", "year" : 2013, "highlights" : [], "score" : 1.0 } { "title" : "The Stanford Prison Experiment", "year" : 2015, "highlights" : [], "score" : 1.0 } { "title" : "The Giver", "year" : 2014, "highlights" : [], "score" : 1.0 } The first four documents in the results have a higher score because the
should
clause in the query specifies a preference for documents withsnow
in the title. Theshould
clause also alters the score for the query termsnow
using theboost
option.dotnet run compound-boost-example.csproj { "genres" : ["Comedy", "Horror"], "title" : "Dead Snow", "year" : 2009, "score" : 21.872983932495117 } { "genres" : ["Adventure", "Comedy", "Family"], "title" : "Snow Day", "year" : 2000, "score" : 21.043487548828125 } { "genres" : ["Adventure", "Comedy", "Family"], "title" : "Snow Dogs", "year" : 2002, "score" : 21.043487548828125 } { "genres" : ["Comedy", "Romance"], "title" : "Let It Snow", "year" : 1999, "score" : 19.523927688598633 } { "genres" : ["Action", "Comedy", "Horror"], "title" : "Dead Snow 2: Red vs. Dead", "year" : 2014, "score" : 17.426334381103516 } { "genres" : ["Comedy", "Drama"], "title" : "Snow White and Russian Red", "year" : 2009, "score" : 16.367326736450195 } { "genres" : ["Comedy", "Drama", "Romance"], "title" : "The Tiger and the Snow", "year" : 2005, "score" : 15.537829399108887 } { "genres" : ["Adventure", "Comedy", "Family"], "title" : "Snow White and the Three Stooges", "year" : 1961, "score" : 14.426333427429199 }
Run an Atlas Search compound query that alters the score using the function
option.
Create a new directory called
compound-function-example
and initialize your project with thedotnet new
command.mkdir compound-function-example cd compound-function-example dotnet new console Add the .NET/C# Driver to your project as a dependency.
dotnet add package MongoDB.Driver Replace the contents of the
Program.cs
file with the following code.The code example performs the following tasks:
Imports
mongodb
packages and dependencies.Establishes a connection to your Atlas cluster.
Uses the following pipeline stages to query the collection:
must
clause with the range operator to search for movies between the years2013
to2015
.should
clause with the text operator to query for the termsnow
in thetitle
field and alter thescore
with thefunction
option. Thefunction
option adds the following using an arithmetic expression:The relevance score of the query for the search term
The value of the numeric field named
imdb.rating
or the number2
for those documents that do not have theimdb.rating
field.
Uses the following pipeline stages:
Specifies the highlight option to return snippets of text from the
title
field that match the query. The snippets contain matching text specified withtype: 'hit'
, and adjacent text specified withtype: 'text'
.Iterates over the cursor to print the documents that match the query.
1 using MongoDB.Bson; 2 using MongoDB.Bson.Serialization.Attributes; 3 using MongoDB.Bson.Serialization.Conventions; 4 using MongoDB.Driver; 5 using MongoDB.Driver.Search; 6 7 public class CompoundFunctionExample 8 { 9 private const string MongoConnectionString = "<connection-string>"; 10 11 public static void Main(string[] args) 12 { 13 // allow automapping of the camelCase database fields to our MovieDocument 14 var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() }; 15 ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true); 16 17 // connect to your Atlas cluster 18 var mongoClient = new MongoClient(MongoConnectionString); 19 var mflixDatabase = mongoClient.GetDatabase("sample_mflix"); 20 var moviesCollection = mflixDatabase.GetCollection<MovieDocument>("movies"); 21 22 var scoreFunction = Builders<MovieDocument>.SearchScore.Function(Builders<MovieDocument>.SearchScoreFunction.Add(Builders<MovieDocument>.SearchScoreFunction.Path(movie => movie.Imdb.Rating, 2), Builders<MovieDocument>.SearchScoreFunction.Relevance())); 23 // define and run pipeline 24 var results = moviesCollection.Aggregate() 25 .Search(Builders<MovieDocument>.Search.Compound() 26 .Must(Builders<MovieDocument>.Search.Range(movie => movie.Year, SearchRangeBuilder.Gte(2013).Lte(2015))) 27 .Should(Builders<MovieDocument>.Search.Text(movie => movie.Title, "snow", score: scoreFunction)), 28 new SearchHighlightOptions<MovieDocument>(movie => movie.Title), 29 indexName: "compound-query-custom-score-tutorial") 30 .Project<MovieDocument>(Builders<MovieDocument>.Projection 31 .Include(movie => movie.Genres) 32 .Include(movie => movie.Title) 33 .Include(movie => movie.Year) 34 .Exclude(movie => movie.Id) 35 .MetaSearchScore(movie => movie.Score) 36 .MetaSearchHighlights("highlights")) 37 .Limit(10) 38 .ToList(); 39 40 // print results 41 foreach (var movie in results) 42 { 43 Console.WriteLine(movie.ToJson()); 44 } 45 } 46 } 47 48 [ ]49 public class MovieDocument 50 { 51 [ ]52 public ObjectId Id { get; set; } 53 public string [] Genres { get; set; } 54 public IMDB Imdb { get; set; } 55 public string Title { get; set; } 56 public int Year { get; set; } 57 [ ]58 public List<SearchHighlight> Highlights { get; set; } 59 public double Score { get; set; } 60 } 61 62 [ ]63 public class IMDB 64 { 65 public double Rating { get; set; } 66 } Before you run the sample, replace
<connection-string>
with your Atlas connection string. Ensure that your connection string includes your database user's credentials. To learn more, see Connect via Drivers.Compile and run the
Program.cs
file.dotnet run compound-function-example.csproj { "genres" : ["Drama", "Mystery"], "title" : "The Snow White Murder Case", "year" : 2014, "highlights" : [{ "path" : "title", "score" : 1.3525336980819702, "texts" : [{ "type" : "Text", "value" : "The " }, { "type" : "Hit", "value" : "Snow" }, { "type" : "Text", "value" : " White Murder Case" }] }], "score" : 10.454826354980469 } { "genres" : ["Action", "Drama", "History"], "title" : "Snow on the Blades", "year" : 2014, "highlights" : [{ "path" : "title", "score" : 1.3766303062438965, "texts" : [{ "type" : "Hit", "value" : "Snow" }, { "type" : "Text", "value" : " on the Blades" }] }], "score" : 10.331723213195801 } { "genres" : ["Action", "Comedy", "Horror"], "title" : "Dead Snow 2: Red vs. Dead", "year" : 2014, "highlights" : [{ "path" : "title", "score" : 1.3924485445022583, "texts" : [{ "type" : "Text", "value" : "Dead " }, { "type" : "Hit", "value" : "Snow" }, { "type" : "Text", "value" : " 2: Red vs. " }] }], "score" : 10.032526969909668 } { "genres" : ["Thriller"], "title" : "Snow in Paradise", "year" : 2014, "highlights" : [{ "path" : "title", "score" : 1.3828467130661011, "texts" : [{ "type" : "Hit", "value" : "Snow" }, { "type" : "Text", "value" : " in Paradise" }] }], "score" : 8.3861465454101562 } { "genres" : ["Adventure", "Comedy", "Drama"], "title" : "The Secret Life of Walter Mitty", "year" : 2013, "highlights" : [], "score" : 1.0 } { "genres" : ["Action", "Adventure", "Sci-Fi"], "title" : "Jurassic World", "year" : 2015, "highlights" : [], "score" : 1.0 } { "genres" : ["Action", "Comedy", "Drama"], "title" : "Action Jackson", "year" : 2014, "highlights" : [], "score" : 1.0 } { "genres" : ["Crime", "Drama", "Thriller"], "title" : "In Secret", "year" : 2013, "highlights" : [], "score" : 1.0 } { "genres" : ["Drama", "Thriller"], "title" : "The Stanford Prison Experiment", "year" : 2015, "highlights" : [], "score" : 1.0 } { "genres" : ["Drama", "Sci-Fi"], "title" : "The Giver", "year" : 2014, "highlights" : [], "score" : 1.0 } The first four documents in the results have a higher score because the
should
clause in the query specifies a preference for documents withsnow
in the title. Theshould
clause also alters the score for the query termsnow
using thefunction
option.
Run an Atlas Search compound query that alters the score using the constant
option.
Create a file named
compound-constant-query.go
.Copy and paste the code example into the
compound-constant-query.go
file.The code example performs the following tasks:
Imports
mongodb
packages and dependencies.Establishes a connection to your Atlas cluster.
Uses the following compound clauses to query the collection:
Specifies the highlight option to return snippets of text from the
title
field that match the query. The snippets contain matching text specified withtype: 'hit'
, and adjacent text specified withtype: 'text'
.Uses the following pipeline stages:
Iterates over the cursor to print the documents that match the query.
1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "time" 7 8 "go.mongodb.org/mongo-driver/bson" 9 "go.mongodb.org/mongo-driver/mongo" 10 "go.mongodb.org/mongo-driver/mongo/options" 11 ) 12 13 func main() { 14 // connect to your Atlas cluster 15 client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("<connection-string>")) 16 if err != nil { 17 panic(err) 18 } 19 defer client.Disconnect(context.TODO()) 20 21 // set namespace 22 collection := client.Database("sample_mflix").Collection("movies") 23 24 // define pipeline stages 25 searchStage := bson.D{{"$search", bson.M{ 26 "index": "compound-query-custom-score-tutorial", 27 "compound": bson.M{ 28 "filter": bson.M{ 29 "range": bson.M{ 30 "path": "year", "gte": 2013, "lte": 2015, 31 }, 32 }, 33 "should": bson.D{ 34 {"text", bson.M{ 35 "path": "title", "query": "snow", "score": bson.M{ 36 "constant": bson.D{{"value", 5}}, 37 }}}}, 38 }, 39 "highlight": bson.D{{"path", "title"}}, 40 }}} 41 limitStage := bson.D{{"$limit", 10}} 42 projectStage := bson.D{{"$project", bson.D{{"title", 1}, {"year", 1}, {"_id", 0}, {"score", bson.D{{"$meta", "searchScore"}}}, {"highlights", bson.D{{"$meta", "searchHighlights"}}}}}} 43 44 // specify the amount of time the operation can run on the server 45 opts := options.Aggregate().SetMaxTime(5 * time.Second) 46 47 // run pipeline 48 cursor, err := collection.Aggregate(context.TODO(), mongo.Pipeline{searchStage, limitStage, projectStage}, opts) 49 if err != nil { 50 panic(err) 51 } 52 53 // print results 54 var results []bson.D 55 if err = cursor.All(context.TODO(), &results); err != nil { 56 panic(err) 57 } 58 for _, result := range results { 59 fmt.Println(result) 60 } 61 } Before you run the sample, replace
<connection-string>
with your Atlas connection string. Ensure that your connection string includes your database user's credentials. To learn more, see Connect via Drivers.Run the following command to query your collection:
go run compound-constant-query.go [{title Snow in Paradise} {year 2014} {score 5} {highlights [[{score 1.382846713066101} {path title} {texts [[{value Snow} {type hit}] [{value in Paradise} {type text}]]}]]}] [{title Dead Snow 2: Red vs. Dead} {year 2014} {score 5} {highlights [[{score 1.3924485445022583} {path title} {texts [[{value Dead } {type text}] [{value Snow} {type hit}] [{value 2: Red vs. } {type text}]]}]]}] [{title The Snow White Murder Case} {year 2014} {score 5} {highlights [[{score 1.3525336980819702} {path title} {texts [[{value The } {type text}] [{value Snow} {type hit}] [{value White Murder Case} {type text}]]}]]}] [{title Snow on the Blades} {year 2014} {score 5} {highlights [[{score 1.3766303062438965} {path title} {texts [[{value Snow} {type hit}] [{value on the Blades} {type text}]]}]]}] [{year 2013} {title The Secret Life of Walter Mitty} {score 0} {highlights []}] [{title Jurassic World} {year 2015} {score 0} {highlights []}] [{title Action Jackson} {year 2014} {score 0} {highlights []}] [{year 2013} {title In Secret} {score 0} {highlights []}] [{title The Stanford Prison Experiment} {year 2015} {score 0} {highlights []}] [{year 2014} {title The Giver} {score 0} {highlights []}] The first four documents in the results have a higher score because the
should
clause in the query specifies a preference for documents withsnow
in the title. Theshould
clause also alters the score for the query termsnow
using theconstant
option.
Run an Atlas Search compound query that alters the score using the boost
option.
Create a file named
compound-boost-query.go
.Copy and paste the code example into the
compound-boost-query.go
file.The code examples perform the following tasks:
Imports
mongodb
packages and dependencies.Establishes a connection to your Atlas cluster.
Iterates over the cursor to print the documents that match the query.
The query uses the following pipeline stages:
$search
stage to query the collection. The query:Uses the following
compound
clauses to query the collection:Specifies the highlight option to return snippets of text from the
title
field that match the query. The snippets contain matching text specified withtype: 'hit'
, and adjacent text specified withtype: 'text'
.
$limit stage to limit the output to 10 results
$project stage to:
Exclude all fields except
title
andyear
Add two fields named
score
andhighlights
1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "time" 7 8 "go.mongodb.org/mongo-driver/bson" 9 "go.mongodb.org/mongo-driver/mongo" 10 "go.mongodb.org/mongo-driver/mongo/options" 11 ) 12 13 // define structure of movies collection 14 type MovieCollection struct { 15 title string `bson:"Title,omitempty"` 16 } 17 18 func main() { 19 var err error 20 // connect to the Atlas cluster 21 ctx := context.Background() 22 client, err := mongo.Connect(ctx, options.Client().ApplyURI("<connection-string>")) 23 if err != nil { 24 panic(err) 25 } 26 defer client.Disconnect(ctx) 27 // set namespace 28 collection := client.Database("sample_mflix").Collection("movies") 29 // define pipeline 30 searchStage := bson.D{{"$search", bson.M{ 31 "index": "compound-query-custom-score-tutorial", 32 "compound": bson.M{ 33 "must": bson.M{ 34 "range": bson.M{ 35 "path": "year", "gte": 2013, "lte": 2015, 36 }, 37 }, 38 "should": bson.D{ 39 {"text", bson.M{ 40 "path": "title", "query": "snow", "score": bson.M{ 41 "boost": bson.D{{"value", 2}}, 42 }, 43 }}, 44 }, 45 }, 46 }}} 47 limitStage := bson.D{{"$limit", 10}} 48 projectStage := bson.D{{"$project", bson.D{{"title", 1}, {"year", 1}, {"_id", 0}, {"score", bson.D{{"$meta", "searchScore"}}}}}} 49 // specify the amount of time the operation can run on the server 50 opts := options.Aggregate().SetMaxTime(5 * time.Second) 51 // run pipeline 52 cursor, err := collection.Aggregate(ctx, mongo.Pipeline{searchStage, limitStage, projectStage}, opts) 53 if err != nil { 54 panic(err) 55 } 56 // print results 57 var results []bson.D 58 if err = cursor.All(context.TODO(), &results); err != nil { 59 panic(err) 60 } 61 for _, result := range results { 62 fmt.Println(result) 63 } 64 } This query uses the following pipeline stages:
$search
stage to query the collection. The query:Uses the
compound
operator clauses with theboost
option to prioritize some fields more than other fields:must
clause with the text operator to prioritize the genrecomedy
the most, followed by the termsnow
in thetitle
field. Theboost
option applies weights to the fields.should
clause with the range operator to search for movies between the years2013
to2015
.
Note
The
boost
option applies different weights to the fields to prioritize the fields.
$limit stage to limit the output to 10 results
$project stage to:
Exclude all fields except
title
andyear
Add two fields named
score
andhighlights
1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "time" 7 8 "go.mongodb.org/mongo-driver/bson" 9 "go.mongodb.org/mongo-driver/mongo" 10 "go.mongodb.org/mongo-driver/mongo/options" 11 ) 12 13 // define structure of movies collection 14 type MovieCollection struct { 15 title string `bson:"Title,omitempty"` 16 } 17 18 func main() { 19 var err error 20 // connect to the Atlas cluster 21 ctx := context.Background() 22 client, err := mongo.Connect(ctx, options.Client().ApplyURI("<connection-string>")) 23 if err != nil { 24 panic(err) 25 } 26 defer client.Disconnect(ctx) 27 // set namespace 28 collection := client.Database("sample_mflix").Collection("movies") 29 // define pipeline 30 searchStage := bson.D{{"$search", bson.M{ 31 "index": "compound-query-custom-score-tutorial", 32 "compound": bson.M{ 33 "must": bson.A{ 34 bson.M{ 35 "text": bson.M{ 36 "path": "genres", "query": "comedy", "score": bson.M{ 37 "boost": bson.D{{"value", 9}}, 38 }, 39 }, 40 }, 41 bson.M{ 42 "text": bson.M{ 43 "path": "title", "query": "snow", "score": bson.M{ 44 "boost": bson.D{{"value", 5}}, 45 }, 46 }, 47 }, 48 }, 49 "should": bson.M{ 50 "range": bson.M{ 51 "path": "year", "gte": 2013, "lte": 2015, "score": bson.M{ 52 "boost": bson.D{{"value", 3}}, 53 }, 54 }, 55 }, 56 }, 57 }}} 58 limitStage := bson.D{{"$limit", 10}} 59 projectStage := bson.D{{"$project", bson.D{{"title", 1}, {"year", 1}, {"genres", 1}, {"_id", 0}, {"score", bson.D{{"$meta", "searchScore"}}}}}} 60 // specify the amount of time the operation can run on the server 61 opts := options.Aggregate().SetMaxTime(5 * time.Second) 62 // run pipeline 63 cursor, err := collection.Aggregate(ctx, mongo.Pipeline{searchStage, limitStage, projectStage}, opts) 64 if err != nil { 65 panic(err) 66 } 67 // print results 68 var results []bson.D 69 if err = cursor.All(context.TODO(), &results); err != nil { 70 panic(err) 71 } 72 for _, result := range results { 73 fmt.Println(result) 74 } 75 } Before you run the sample, replace
<connection-string>
with your Atlas connection string. Ensure that your connection string includes your database user's credentials. To learn more, see Connect via Drivers.Run the following command to query your collection:
go run compound-boost-query.go [{title Snow in Paradise} {year 2014} {score 6.7722930908203125} {highlights [[{score 1.382846713066101} {path title} {texts [[{value Snow} {type hit}] [{value in Paradise} {type text}]]}]]}] [{title Snow on the Blades} {year 2014} {score 6.063445568084717} {highlights [[{score 1.3766303062438965} {path title} {texts [[{value Snow} {type hit}] [{value on the Blades} {type text}]]}]]}] [{title The Snow White Murder Case} {year 2014} {score 5.509652137756348} {highlights [[{score 1.3525336980819702} {path title} {texts [[{value The } {type text}] [{value Snow} {type hit}] [{value White Murder Case} {type text}]]}]]}] [{title Dead Snow 2: Red vs. Dead} {year 2014} {score 5.065053939819336} {highlights [[{score 1.3924485445022583} {path title} {texts [[{value Dead } {type text}] [{value Snow} {type hit}] [{value 2: Red vs. } {type text}]]}]]}] [{year 2013} {title The Secret Life of Walter Mitty} {score 1} {highlights []}] [{title Jurassic World} {year 2015} {score 1} {highlights []}] [{title Action Jackson} {year 2014} {score 1} {highlights []}] [{year 2013} {title In Secret} {score 1} {highlights []}] [{title The Stanford Prison Experiment} {year 2015} {score 1} {highlights []}] [{year 2014} {title The Giver} {score 1} {highlights []}] The first four documents in the results have a higher score because the
should
clause in the query specifies a preference for documents withsnow
in the title. Theshould
clause also alters the score for the query termsnow
using theboost
option.go run compound-boost-query.go