How to Run Partial Match Atlas Search Queries
This tutorial describes how to create an index on the
sample_mflix.movies
collection and run partial string queries
against the plot
field. To return matches for partial string
queries, you can use one of the following operators:
- The autocomplete operator, which allows you to search for a word or phrase that contains the sequence of characters that you specify with your query.
- The phrase operator, which allows you to search for documents that contain the terms in your query string at the distance you specify between the terms.
- The regex operator, which allows you to search for strings using regular expression.
- The wildcard operator, which allows you to search using special characters in your query to match any character.
This tutorial takes you through the following steps:
- Set up an Atlas Search index on the
plot
field in thesample_mflix.movies
collection. - Run Atlas Search query for a partial string against the
plot
field in thesample_mflix.movies
collection using autocomplete, phrase, regex, and wildcard operators.
Before you begin, ensure that your Atlas cluster meets the requirements described in the Prerequisites.
Create the Atlas Search Index
In this section, you will create an Atlas Search index on the plot
field
in the sample_mflix.movies
collection for running partial match
queries against those fields.
Navigate to the Atlas Search page for your project.
- If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
- If it is not already displayed, select your desired project from the Projects menu in the navigation bar.
- Click your cluster's name.
- Click the Search tab.
Enter the Index Name, and set the Database and Collection.
In the Index Name field, enter
default
.NoteIf you name your index
default
, you don't need to specify anindex
parameter when using the $search pipeline stage. Otherwise, you must specify the index name using theindex
parameter.- In the Database and Collection section, find the
sample_mflix
database, and select themovies
collection.
Define an index on the title
and plot
fields.
You can use the Visual Editor or the JSON Editor in the Atlas user interface to create the index.
Visual Editor
- Click Next.
- Click Refine Your Index.
- Click Add Field.
Configure the following fields:
UI Field NameConfigurationField NameEnterplot
.Enable Dynamic MappingToggle to Off.Specify the data type and the settings for the operator you want to use to run the query.
- Click Save Changes.
Click Create Search Index.
NoteThe You're All Set! modal window appears to let you know your index is building.
- Click the Close button.
JSON Editor
Replace the default index definition with the following example index definition for the operator you intend to use for running the query and click Next.
- Click Create Search Index.
Run a Case-Sensitive Partial Match Query
➤ Use the Select your language drop-down menu on this page to set the language of the examples in this section.
You can use the autocomplete, phrase, regex, and wildcard operators to run a case-sensitive partial match query. This tutorial uses these operators to search for movies whose plot contain the specified partial string. The query uses the following pipeline stages:
$search
stage to search the collection$limit
stage to limit the output to3
results$project
stage to exclude all fields excepttitle
andplot
In this section, you will connect to your Atlas cluster and run the
sample query using the operator against the plot
field in the
sample_mflix.movies
collection.