Overview
In this guide, you can learn how to query an Atlas Search index and use advanced full-text
search functionality in your Ruby driver applications. You can query a search index by
using a $search
aggregation pipeline stage.
To learn more about the $search
pipeline stage, see the $search guide in the MongoDB Server manual.
Note
Only Available on Atlas for MongoDB v4.2 and Later
The $search
aggregation-pipeline operator is available only for collections hosted
on MongoDB Atlas clusters running MongoDB v4.2 or later that are
covered by an Atlas search index.
To learn more about the required setup and the functionality of this operator,
see the Atlas Search documentation.
Sample Data
The examples in this guide use the sample_mflix.movies
collection
from the Atlas sample datasets. To learn how to create a
free MongoDB Atlas cluster and load the sample datasets, see
Get Started with the Ruby Driver.
Create an Atlas Search Index
Before you can perform a search on an Atlas collection, you must first create an Atlas Search index on the collection. An Atlas Search index is a data structure that categorizes data in a searchable format. To learn how to create an Atlas Search index, see the Atlas Search and Atlas Vector Search Indexes guide.
Search Your Data
To use the $search
aggregation pipeline stage, you must specify an Atlas Search query
operator that indicates the type of query you want to run. You can optionally
use a collector to specify the values and ranges of the query output. To view a
table of all the operators and collectors available with Atlas Search, see the
Operators and Collectors page
in the Atlas documentation.
The following example uses the compound
operator to combine several operators into a
single query. To learn more, see the Compound Operator guide in the Atlas documentation.
The query has the following search criteria:
The
genres
field must not containComedy
.The
title
field must contain the stringNew York
.
The query also includes the following stages:
$limit
, to limit the output to 10 results.$project
, to exclude all fields excepttitle
and add a field namedscore
.
pipeline = [ { "$search" => { "index" => "index_name", "compound" => { "mustNot" => [ { "text" => { "query" => ["Comedy"], "path" => "genres" } } ], "must" => [ { "text" => { "query" => ["New York"], "path" => "title" } } ] } } }, { "$limit" => 10 }, { "$project" => { "_id" => 0, "title" => 1, "score" => { "$meta" => "searchScore" } } } ] result = collection.aggregate(pipeline) puts result.to_a
{'title': 'New York, New York', 'score': 6.786379814147949} {'title': 'New York', 'score': 6.258603096008301} {'title': 'New York Doll', 'score': 5.381444931030273} {'title': 'Escape from New York', 'score': 4.719935417175293} {'title': 'Autumn in New York', 'score': 4.719935417175293} {'title': 'Sleepless in New York', 'score': 4.719935417175293} {'title': 'Gangs of New York', 'score': 4.719935417175293} {'title': 'Sherlock Holmes in New York', 'score': 4.203253746032715} {'title': 'New York: A Documentary Film', 'score': 4.203253746032715} {'title': 'An Englishman in New York', 'score': 4.203253746032715}
Additional Information
To learn more about the available Atlas Search operators, see the Operators and Collectors guide in the MongoDB Atlas documentation.
For more information about Atlas Search, and to view more query examples, see the Atlas Search documentation.
If you'd like to perform vector searches on your data stored in Atlas, you must use Atlas Vector Search. To learn more about Atlas Vector Search, see the Atlas Vector Search documentation.