How to Use Autocomplete with Atlas Search
This tutorial describes how to create an Atlas Search index that enables
autocomplete queries. In the following examples, the index enables
autocomplete
operators to query the specified field values.
Both examples take you through the following steps:
Set up an Atlas Search index with autocomplete data type for the
sample_mflix.movies
collection.Run Atlas Search queries against the specified fields in the
sample_mflix.movies
collection for an inputted sequence of characters.
Before you begin, ensure that your Atlas cluster meets the requirements described in the Prerequisites.
Define an Index with Autocomplete
You can use the autocomplete
data type to index text values for
autocompletion. You can configure an autocomplete
field to satisfy
a variety of use cases. To learn more about the configuration options
available in the autocomplete
data type, such as tokenization
strategy and diacritic folding, see
autocomplete. You can use the autocomplete operator to query only fields indexed using
autocomplete.
You can also use the autocomplete
type to index:
Fields whose value is an array of strings. To learn more, see Index Array.
String fields inside an array of documents indexed as the embeddedDocuments type.
In this section, you will create an Atlas Search index that indexes the title
and
plot
fields for autocompletion using the edgeGram
tokenization strategy.
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
.Note
If 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.
Run an Autocomplete Query
➤ Use the Select your language drop-down menu to set the language of the example on this page.
The autocomplete operator performs a search
for a word or phrase that contains a sequence of characters from an
incomplete input string. You can use the autocomplete
operator with
search-as-you-type applications to predict words with increasing
accuracy as characters are entered in your application's search
field. autocomplete
returns results that contain predicted words
based on the tokenization strategy specified in the index definition
for autocompletion. The fields that you intend to query with the
autocomplete
operator must be indexed with the
autocomplete data type in the collection's
index definition.
Note
Atlas Search might return inaccurate results for queries with more than three words in a single string.
Continue Learning
Follow along with this video tutorial walk-through that demonstrates how to use the Atlas Search autocomplete operator and JavaScript to build a frontend form element that suggests data to the user.
Duration: 30 minutes