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.

참고

컬렉션에 16MB 이상의 문서가 포함되어 있는 경우 Atlas Search가 데이터 색인에 실패합니다. 대용량 문서에 대한 업데이트 작업으로 인해 변경 스트림 이벤트가 16MB BSON 제한을 초과할 때 이 문제가 발생할 수 있습니다. 이를 방지하려면 다음 권장사항을 고려하세요.

  • 하위 문서나 배열의 크기를 최소화하도록 문서를 구조화하세요.

  • 큰 필드, 하위 문서 또는 배열을 업데이트하거나 교체하는 작업을 피하세요.

자세한 내용은 Change Streams 프로덕션 권장사항대용량 문서 크기 줄이기를 참조하세요.

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:

집계 파이프라인 단계
목적

Full Text Search의 검색 결과를 반환합니다.

검색 결과에 대한 메타데이터를 반환합니다.

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>
}
}
]

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:

옵션
사용 사례

텀을 원래의 컨텍스트에 따라 쿼리 결과의 필드로 표시합니다.

Atlas Search이 반환하는 각 문서의 점수에 대한 자세한 분석을 검색할 수 있습니다.

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:

옵션
사용 사례

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:

옵션
사용 사례

컬렉션에 대한 Atlas Search 인덱스 정의에 지정된 대로 mongot에 저장된 필드만 검색하여 Atlas Search 쿼리를 더 효율적으로 실행하십시오.

전용 검색 노드에서 여러 세그먼트에 걸쳐 검색을 병렬화하세요.

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: