Video Transcript (English)
Welcome back! We’ve arrived at the heart of this unit: using the autocomplete operator in an Atlas Search query.
In this lesson, we’ll create a search stage that leverages the autocomplete operator.
We’ll also explore how token order can affect the results of a query. Are you ready? Let’s get to it.
If you’ve completed the other units in this course, then you’re familiar with creating queries in Atlas Search using other operators. The good news is that the autocomplete operator isn’t much different.
We need the name of the index we just created.
And we set the path to title because that's the field we’re targeting.
Next, we have the fuzzy option, which helps users if they misspell their search terms or make a typo.
Like most people, I am not immune to typos but we’re going to skip fuzzy since we've covered it in previous units.
If you want to learn more about the fuzzy option, check out our unit on advanced Atlas Search queries.
After that, we have the score option.
This option allows us to manipulate the score assigned by Atlas Search to a result. We can boost the score by using a numerical factor, assigning results a constant value, or using an expression or function to transform the score.
We won't use the score option in this unit, so check out the MongoDB Atlas Search documentation for more information.
Up next is the tokenOrder option, which determines how the order of the tokens will affect our search results.
Setting the order to “any” means that tokens in the query can appear in any order in the target field, which is “title” in this case.
Or we can set it to “sequential,” which means the query tokens must appear next to each other in the specified order.
To show the difference between these two settings, let’s set our tokenOrder to “any” and the query “Wars Star”.
We also add a projection stage to focus the output on what we want to see.
When we run the query, the returned titles all contain the search terms “Wars” and “Star”
So you can see that using the “any” value is useful if the order of the search terms doesn’t matter.
But, in our case, we want the sequence of tokens in the query to match the sequence of tokens in the title.
So let’s change the tokenOrder value to sequential and see what happens.
This time, we don’t have any results!
This is because there are no movies in our movie catalog with the title “Wars Star”.
As you can see, tokenOrder is one way we can control the results that users will get. In later lessons, we’ll explore other ways to tune our results.
But before we move on, let’s quickly recap what you learned in this lesson.
First, you learned how to create a search query that leverages the autocomplete operator.
You also learned the tokenOrder option determines the role token order plays when matching tokens.
See you in the next lesson for more fun with autocomplete.