Docs Menu

Queries and Indexes

The relationship between search queries and search indexes dictates how efficiently and effectively you can find data within your MongoDB collections using Atlas Search.

Atlas Search queries specify the criteria for finding documents within a database. Atlas Search queries take the form of an aggregation pipeline that begins with the $search or $searchMeta pipeline stage. You can use operators, collectors, and search options inside the pipeline stages to implement complex search functionality like full-text search, relevance-based ranking, faceted search, filtering, and sorting.

Before you can run an Atlas Search query, you must create an Atlas Search index on the fields that you want to search. Search indexes are data structures that are optimized to quickly retrieve documents that meet the search criteria of your query. When you define a search index, you specify which fields to index and how these fields should be tokenized.

Effective search queries depend on properly defined search indexes. The fields you intend to search must be indexed, and your index configuration determines whether your search supports sorting, faceting, autocomplete, and other search functionality. You can iterate on both query and index design to balance search accuracy with performance.

This page describes how to plan your Atlas Search search experience and define an Atlas Search index and query to fit your search requirements.

When planning your Atlas Search implementation, start by defining the search experience you want to deliver:

  • Clearly identify what types of searches your application needs to perform. Are you building a search feature for a blog website that needs full-text search and autocomplete for article titles, or an e-commerce site that requires faceted search and filtering by product categories?

  • Determine how users will interact with your application. Prioritize features that will enhance the user experience, such as quick response times or accurate autocomplete suggestions.

Then, consider the following questions to help determine the structure of your Atlas Search indexes and queries based on those user needs:

Before you can search your data using Atlas Search, you must create one or more Atlas Search indexes to be used during your Atlas Search query. This section demonstrates how to apply your query preferences to the JSON configuration syntax of an Atlas Search index.

To use the JSON syntax in this section in your index definition, replace the placeholders with valid values and ensure that your full index definition contains the necessary options.

To learn how to add your Atlas Search index to your Atlas cluster, see the Atlas Search Quick Start.

1

If you know which fields you want to query in your collection, enable static mappings and specify the fields in your Atlas Search index definition. Otherwise, you can enable dynamic mappings to automatically index all the fields of supported types.

To learn more, see Static and Dynamic Mappings.

Note

If your collection contains documents that are 16MB or larger, Atlas Search fails to index your data. This issue can also occur when update operations on large documents cause the change stream event to exceed the 16MB BSON limit. To avoid this, consider the following best practices:

  • Structure your documents to minimize the size of sub-documents or arrays.

  • Avoid operations that update or replace large fields, sub-documents, or arrays.

To learn more, see Change Streams Production Recommendations and Reduce the Size of Large Documents.

1{
2 "mappings": {
3 "dynamic": true,
4 "fields": { // Optional, use this to configure individual fields
5 "<field-name>": {
6 "type": "<field-type>",
7 ...
8 },
9 ...
10 }
11 }
12}
1{
2 "mappings": {
3 "dynamic": false, // Optional, if omitted defaults to "false"
4 "fields": {
5 "<field-name>": {
6 "type": "<field-type>",
7 ...
8 },
9 ...
10 }
11 }
12}
2

If you have special language or parsing requirements, you can apply the following options to your index definition:

Specify which built-in analyzers to apply to the string fields you are indexing in the analyzer, searchAnalyzer, or fields.<field-name>.analyzer fields.

1{
2 "analyzer": "<index-analyzer-name>", // top-level index analyzer, used if no analyzer is set in the field mappings
3 "searchAnalyzer": "<search-analyzer-name>", // query text analyzer, typically the same as the index analyzer
4 "mappings": {
5 "dynamic": <boolean>,
6 "fields":{
7 "<field-name>": [
8 {
9 "type": "string",
10 "analyzer": "<field-analyzer-name>" // field-specific index analyzer
11 }
12 ]
13 }
14 }
15}

Define custom analyzers for your Atlas Search index in the analyzers field.

1{
2 "analyzers": [
3 {
4 "name": "<custom-analyzer-name>",
5 "tokenizer": {
6 "type": "<tokenizer-type>"
7 }
8 },
9 ...
10 ]
11}

Define synonyms for terms that have the same or similar meanings in the synonyms field.

1{
2 "synonyms": [
3 {
4 "name": "<synonym-mapping-name>",
5 "source": {
6 "collection": "<source-collection-name>"
7 },
8 "analyzer": "<synonym-mapping-analyzer>"
9 }
10 ]
11}
3

If you want to optimize your query performance on a large dataset, you can add the following options to your index definition to limit the amount of data that your Atlas Search query must traverse:

Use the numPartitions option to configure partitions for your index. When you partition your index, Atlas Search automatically distributes the index objects between sub-indexes in an optimal way.

1{
2 "numPartitions": <integer>,
3}

Use the storedSource option to specify fields in the source document that Atlas Search must store.

1{
2 "storedSource": true | false | {
3 "include" | "exclude": [
4 "<field-name>",
5 ...
6 ]
7 }
8}

After you create an Atlas Search index for all the fields that you want to search in your collection, you can run an Atlas Search query. This section demonstrates how to apply your goals for your application's search experience to the JSON syntax of an Atlas Search query.

To use the JSON syntax in this section in your Atlas Search query aggregation pipeline, replace the placeholders with valid values and ensure that your full query pipeline contains the required $search fields or $searchMeta fields.

To learn how to run a Search query, see the Atlas Search Quick Start.

1

The first stage of your Atlas Search query aggregation pipeline must be either the $search or $searchMeta stage, depending on whether you're searching for documents or metadata:

Aggregation Pipeline Stage
Purpose

Return the search results of a full-text search.

Return metadata about your search results.

2

To define your search criteria, you must apply one or more operators or collectors to your $search or $searchMeta pipeline stage.

Atlas Search operators allow you to locate and retrieve relevant data from your Atlas cluster according to content, format, or data tyoe. To learn which operators support searches for each field type, see the table in the operators reference section. You must specify one or more indexed search fields in the operator's query path parameter:

1{
2 $search: {
3 "<operator-name>"|"<collector-name>": {
4 <operator-specification>|<collector-specification>
5 }
6 }
7}
[
{
_id: <result-document-id>,
...
},
{
_id: <result-document-id>,
...
},
...
]
1{
2 $searchMeta: {
3 "<operator-name>"|"<collector-name>": {
4 <operator-specification>|<collector-specification>
5 }
6 }
7}
[
{
count: {
total: <results-count>
}
}
]

Tip

You can combine multiple operators into one operation using the compound operator. You can also use the compound operator's filter clause to filter for query output that matches a given clause.

3

If you want to retrieve metadata from your Atlas Search query, you can apply one of the following configurations to choose between the count or facet type of metadata results document:

To return the total or lower-bounded count of your search results, set the count option in your aggregation stage.

The $searchMeta stage returns the count metadata results, while the $search stage stores the metadata results in the $$SEARCH_META aggregation variable and returns only the search results. For an example of how to retrieve the count metadata results from the $$SEARCH_META variable, see Count Results.

1{
2 "$search" | "$searchMeta": {
3 "<operator-name>": {
4 <operator-specifications>
5 },
6 "count": {
7 "type": "lowerBound" | "total",
8 "threshold": <number-of-documents> // Optional
9 }
10 }
11}

To run a facet query, which groups results by values or ranges and returns the count for each of these groups, use the facet collector in your aggregation stage.

The $searchMeta stage returns facet metadata results, while the $search stage stores the metadata results in the $$SEARCH_META aggregation variable and returns only the search results. For an example of how to retrieve the facet metadata results from the $$SEARCH_META variable, see Facet Results.

1{
2 "$search" | "$searchMeta": {
3 "facet": {
4 "facets": {
5 <facet-definitions>
6 }
7 }
8 }
9}
4

You can retrieve additional information about your $search stage results using the following options:

Option
Use Case

Display your search terms in their original context as fields in your query result.

Retrieve a detailed breakdown of the score for each document Atlas Search returns.

Track and provide analytics information for your query search terms.

Retrieve analytics about which Lucene queries Atlas Search executed to satify your query, and how much time your query spends in the various stages of execution.

5

You can implement special ordering functionality for your $search results with the following options:

Option
Use Case

Modify the relevance score of the documents in the results to ensure Atlas Search returns relevant results.

Sort your results by number, string, and date fields, or by score.

Set a reference point to stop or start your ordered results

6

Optimize query performance using the following $search options:

Option
Use Case

Run your Atlas Search query more efficiently by only retrieving fields stored on mongot as specified in your Atlas Search index definition for a collection.

Parallelize search across segments on dedicated search nodes.

To learn how to build and run an Atlas Search index and Atlas Search query, see the Atlas Search Quick Start.

To learn more about the Atlas Search query configuration options mentioned in this tutorial, see the following reference pages:

To learn more about the Atlas Search index configuration options mentioned in this tutorial, see the following reference pages: