Search Fundamentals / Creating an Atlas Search Query

6:26
With your Atlas search index perfectly crafted and ready, the next step is to make your data truly discoverable. Atlas search queries allow you to execute rich, flexible search operations such as text matching, faceted search, and relevance based scoring directly on your indexed data. In this video, we'll show you how to build a search query that leverages your search indexes. We'll build a search query for a feature of our Emflix app. Let's quickly review where we are in the process of implementing Atlas search features in mFlix. We've been working on the keyword movie feature, which allows users to search the movies database for movies matching keywords in the title and plot description. They'll also be able to specify a date range for the movie release. We created the title plot released index for this feature, and now we need to build a search query that uses that index. To do this, we need to use MongoDB's aggregation framework. Atlas Search enables the search stage to return documents from a collection and the search meta stage to return metadata about the search results. We'll use the search meta stage to build the second feature later. But to build this feature, we'll be using the search stage because we want this query to return contents from movie documents. Remember, search and metasearch stages must be the first stage in an aggregation pipeline or in a sub pipeline. We've included the name of the index that we created for this feature in the pipeline. Next, we need to add query operators or collectors. These are the fundamental building blocks of a search stage. Query operators define the criteria for matching documents. They work by targeting individual fields or paths within your Atlas search index to specify exactly what you're searching for. Atlas Search provides numerous operators for different search scenarios, but we'll focus on a few in this demo that are very commonly used. Text, range, and compound operators. Query collectors are used to return a document representing the metadata results of your Atlas search query. We'll take a closer look at collectors later when we work on our second feature for our Emflix app. First, we'll add the text operator to our search stage, which performs a full text search on the fields that we specify. We'll use this operator to match the user's query terms with content from the title and plot fields. We can also add the fuzzy option, allowing queries to return results that match terms approximately. This way, users can misspell a search term and still get relevant results. Let's test it out by running the query with the term investigation spelled correctly, and then run it again spelled incorrectly. When we run our query with the keyword investigation spelled correctly, these are some of our results. Looks good. Note that we see different forms of the word investigation, like investigating and investigator, in both the title and the plot fields of these results. When we run the same query but misspell investigation, we also get results that are relevant to the search term. This is exactly what we want our keyword movie feature to do. Note that there are many ways that we can tune the results of fuzzy search that go beyond the scope of this video. If you're interested in learning more about the fuzzy option, check out our documentation. Okay. So now users can search the title and plot for key terms. Users also need to be able to specify a date range for their movie selection. In this case, how do we include more than one query operator within a search query? That's where the compound operator comes in. The compound operator in MongoDB Atlas Search is used to combine multiple query operators and logical rules within a single search stage. This operator works through distinct clauses that define how different search conditions should be evaluated and combined. These clauses include must clauses that require documents to match specified conditions, must not clauses that exclude documents matching certain criteria, should clauses that boost relevance scores for matching documents without requiring them, and filter clauses that restrict results without affecting scoring. Atlas Search uses these clauses along with relevance scores to determine which documents best match your search criteria. You can also combine multiple clauses with a single compound operator. This allows you to create a precise search logic that both requires certain conditions and explicitly excludes others. When implementing logical clauses, it's important to experiment to make sure that users get best results. In our case, the keyword movie feature needs to use exact matching for search terms and similar matching for the date range. We'll use a combination of a must clause and the range operator to do this. Let's change our search query by adding the compound operator to our search stage with a must clause. This way, any document returned will be an exact match for the criteria in this clause. Within the must clause, we have the text operator that we tested earlier. We've also added the range operator. It uses the released field as the path and searches for movies that fall within a specified date range. For this query, we're looking for movies that were released between the years two thousand and twenty eleven. Lastly, we'll add two stages to the pipeline after the search stage. First, we'll add a limit stage, so we only return the top three results. And then we'll add a projection stage, so we're only returning the title, plot, and released fields. When we run the investigation search query again, we get these three results. All results are relevant to the search term and fall within the release dates that we specified. Nice work. We've successfully created our keyword movie search feature for the app. Before we move on, let's recap what we covered in this video. You learned that in order to build a search query with Atlas Search, we need to use either the search stage or the search meta stage with MongoDB's aggregation framework. Within a search stage, we can use query operators to define the criteria for matching documents. We showed you how to use the compound operator with logical clauses to combine multiple query operators within a single search stage. Finally, we completed our work on feature one by building and testing a search query that uses the title plot released index.