Lesson 2: Create a Search Index Using the Autocomplete Field Type / Learn

Video Transcript (English)

Alright, now that you have a solid foundation in the concepts, let’s move on to the fun stuff: implementing autocomplete.

We’ll get one step closer to this goal in this video by indexing a field as an autocomplete type for our movie catalog.

Let’s get started.

As a first step, we need to determine which field from our movie documents we want to use.

Something to keep in mind when deciding which field to use is that autocomplete can only be used on string fields.

In our app, we expect users to search for movies by title. So let’s add the autocomplete functionality to the title field.

To create our search index, we’ll use the MongoDB Shell.

As you can see, the syntax isn’t much different from what we’ve done in the past.

We name the search index autocomplete.

We set the dynamic option to false, because we use static mappings to index autocomplete fields.

Now we need to define the title field.

First, we specify the type as autocomplete.

Next, we define the analyzer we’ll use, which for this example will be the standard analyzer.

Speaking of analyzers, you can use most of the built-in analyzers with autocomplete. But if you’re using a custom analyzer, there are some tokenizers and token filters that aren’t compatible.

For a full list of incompatible tokenizers and filters, check out MongoDB’s documentation.

After choosing the analyzer, we specify our tokenization strategy.

We can choose from edgeGram, nGram, and rightEdgeGram.

Because most movie titles in the catalog are in English and therefore are read from left to right, we’ll use edgeGram.

We’ll then set minGrams and maxGrams. We’ll stick with the same values we used previously, 4 and 7, respectively.

Keep in mind that the wider the range is between these two values, the larger the index will be, which could have an impact on performance.

Finally, we set foldDiacritics to true.

foldDiacritics is similar to the icuFolding token filter that we used to build a custom analyzer.

It determines whether or not autocomplete should ignore diacritics, which are the marks added to letters in some written languages, typically in the form of accents, dots, or lines.

When foldDiacritics is set to true, diacritics are ignored.

Now we can run the createSearchIndex command and let Atlas Search work its magic.

Once built, we’ll be able to use this search index when we start using the autocomplete operator in the next lesson.

Remember that Atlas Search indexes are eventually consistent and may take some time to become fully operational.

Nice!

We did all those steps in the MongoDB Shell, but if you prefer working in a GUI, you’re in luck - we can also create our search index by using the Atlas UI. Let’s check it out.

First, we go to the Atlas Search tab for the cluster we’re using and select “Create Search Index.”

In the visual editor, we choose a name for the index and the collection. Like before, we’ll name it autocomplete and use the movies collection.

Then we click Next.

Now, we’ll refine our index.

We’ll use the standard analyzer.

Let’s also go ahead and toggle dynamic mapping off because autocomplete doesn’t use it.

Now we can move on to defining our field mappings.

We can use a template or a customized configuration. Let’s use a customized configuration.

We first choose the field we want to use and the data type. In this case, we’re using the title field and the autocomplete data type.

Next, we keep the standard analyzer.

We also set the maxGrams to seven and the minGrams to four.

For tokenization, we use edgeGram.

And finally, we’ll set foldDiacritics to true.

Now we click Add.

We’re almost finished, but we need to save our changes.

Nice. Now we can create our search index.

Remember that it may take some time to build.

And we’re done - great work! Let’s recap what you learned in this video.

You learned that the autocomplete field type comes with a few options:

An analyzer, tokenization, minGrams, maxGrams, and foldDiacritics.

You also learned some considerations when adding autocomplete to a search index. These include

Analyzers and custom analyzer settings supported by autocomplete,

Tokenization strategies and settings,

And diacritics.

Don’t go anywhere, because next we’ll get to use the autocomplete operator. See you in the next lesson.