Docs Menu

Docs HomeMongoDB Atlas

How to Use Autocomplete with Atlas Search

On this page

  • Define an Index with Autocomplete
  • Run an Autocomplete Query
  • Continue Learning

This tutorial describes how to create an Atlas Search index that enables autocomplete queries. We will create an Atlas Search index for indexing values of the title field in the sample_mflix.movies collection. The index enables autocomplete operators to query the title field. Then we will run a search for a sequence of characters against the title field for movie title suggestions that match the incomplete input string. It takes you through the following steps:

  1. Set up an Atlas Search index with autocomplete data type for the sample_mflix.movies collection.

  2. Run Atlas Search queries against the title field in the sample_mflix.movies collection for a sequence of characters that appear in the movie titles.

Before you begin, ensure that your Atlas cluster meets the requirements described in the Prerequisites.

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.

The following limitations apply:

Important

Atlas Search doesn't support indexing more than approximately two billion index objects, where each indexed document counts as a single object against this hard limit. Using the autocomplete type could result in indexing objects in excess of this limit, which will cause an index to transition to a failed state. If you have large arrays that might generate two billion objects, we recommend sharding clusters that contain indexes with autocomplete field type.

In this section, you will create an Atlas Search index that indexes the title field for autocompletion using the edgeGram tokenization strategy.

1
  1. If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it is not already displayed, select your desired project from the Projects menu in the navigation bar.

  3. Click your cluster's name.

  4. Click the Search tab.

2
3
  • For a guided experience, select Visual Editor.

  • To edit the raw index definition, select JSON Editor.

4
  1. In the Index Name field, enter default.

    Note

    If you name your index default, you don't need to specify an index parameter when using the $search pipeline stage. Otherwise, you must specify the index name using the index parameter.

  2. In the Database and Collection section, find the sample_mflix database, and select the movies collection.

5

You can use the Visual Editor or the JSON Editor in the Atlas user interface to create the index.

  1. Click Next.

  2. Click Refine Your Index.

  3. Click Add Field.

  4. Configure the following field for autocompletion:

    UI Field Name
    Configuration
    Field Name
    Enter title.
    Enable Dynamic Mapping
    Toggle to Off.
  5. Click Add Data Type, and select Autocomplete.

  6. Review the following default settings:

    UI Field Name
    Configuration
    Max Grams
    15
    Min Grams
    2
    Tokenization
    edgeGram
    Fold Diacritics
    true
  7. Click Save Changes.

  8. Click Create Search Index.

    Note

    The You're All Set! modal window appears to let you know your index is building.

  9. Click the Close button.

  1. Replace the default definition with the following and click Next.

    {
    "mappings": {
    "dynamic": false,
    "fields": {
    "title": [
    {
    "type": "autocomplete",
    "tokenization": "edgeGram",
    "minGrams": 2,
    "maxGrams": 15,
    "foldDiacritics": true
    }
    ]
    }
    }
    }
  2. Click Create Search Index.

6

The index should take about one minute to build. While it is building, the Status column reads Build in Progress. When it is finished building, the Status column reads Active.


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.

In this section, you will connect to your Atlas cluster and the run the sample queries against the title field in the sample_mflix.movies collection using the autocomplete operator. The sample queries use a sequence of characters to find movie titles that begin with the input character string. Atlas Search returns results that begin with the specified query string because the title field is indexed using the edgeGram tokenization strategy.

In these results, the characters ger appear at the beginning of a word in all the titles. Atlas Search returns Gèraldine and Rece do gèry in the results because we set foldDiacritics to true.

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

←  TutorialsHow to Run Compound Queries with Weighted Fields →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.