The MongoDB Search query returns information about the $search query plan and execution statistics when the query is run with the explain method enabled. When you run a query with explain, MongoDB Search returns a BSON document containing statistics and metadata that describes how the query was run internally in Lucene.
Tip
Visualize Query Plans in the Playground
Generate and view explain plan summaries with the MongoDB Search Playground.
Syntax
db.<myCollection>.explain("<verbosity>").aggregate([ { $search: { "<operator>": { "<operator-options>" } } } ])
Verbosity
Verbosity mode controls the behavior of explain and the amount of information returned. Value can be one of the following, in order of decreasing verbosity:
Information about the query plan including the | |
Information about the query plan including the | |
queryPlanner (default) | Information about the query plan. Does not include the |
Output
The query with the explain method returns the following fields in the stages.$_internalSearchMongotRemote object:
Option | Type | Purpose |
|---|---|---|
| document | Contains the query that you ran. |
| document | Contains the |
| array of integers | Contains the size of each batch of documents that |
Explain Results
The explain method returns a BSON document with the following fields in the explain document.
Option | Type | Necessity | Purpose |
|---|---|---|---|
| document | Optional | Describes the execution statistics for the collector. If |
| document | Optional | Details about the highlight execution for the query. This is returned only if you specified highlighting in the query. |
| array of documents | Optional | Contains per index partition details. This is only returned if you configured two or more index partitions. |
| document | Optional | Contains useful metadata. |
| document | Optional | Describes the execution statistics for the query. This is not returned. If |
| document | Optional | Details related to retrieving per-document data from Lucene after query execution. This is not returned for |
| document | Optional | Details the resource usage of the query when it was executed. This is not returned for |
collectors
The collectors BSON document contains the following fields:
Field | Type | Necessity | Purpose |
|---|---|---|---|
| document | Required | Statistics of all collectors of the query. Statistics reported represent either the maximum value across all collectors used in the query or a sum of the statistic across all the sub-collectors. The timing statistics are summed to reflect the total time spent across all collectors for the entire query. To learn more, see allCollectorStats. |
| document | Optional | |
| document | Optional |
allCollectorStats
The allCollectorStats BSON document describes collector statistics across all collectors specified in the query, including facet and sort. It contains the following keys:
Field | Description |
|---|---|
| Tracks the duration and number of results collected by the collector. |
| Statistics tracking the total duration and the number of times a |
| Statistics tracking the total duration and number of times a scorer was set on the collector. |
facet
The facet is a BSON document that details query and execution statistics when you specify a facet in the query. It contains the following fields:
Option | Type | Necessity | Purpose |
|---|---|---|---|
| document | Optional | Shows statistics only for the |
| document | Optional | Shows statistics related to creating the internal Lucene object that holds all of the facet groupings. It contains |
| document | Required | Maps the field being faceted on to their cardinalities for both the documents that matched the query and across the entire Lucene index. It provides the following cardinality information for each field:
|
sort
The sort is a BSON document that details query and execution statistics when you specify a sort in the query. It contains the following fields:
Option | Type | Necessity | Purpose |
|---|---|---|---|
| document | Optional | Tracks statistics related to
|
| document | Required | Maps the field being sorted on to the list of data types present in the index for the field. |
| boolean | Optional | Indicates whether the query can benefit from the index sort. A query benefits when its sort fields match the index sort or form a prefix of it. If value is:
This is absent in the output if the query doesn't specify a sort or if the index doesn't have a sort specification. |
highlight
The highlight is a BSON document that details query and execution statistics when you specify highlighting in the query. It contains the following fields:
Option | Type | Necessity | Purpose |
|---|---|---|---|
| List<String> | Required | List of all the highlighted fields. If you specified a wildcard path in the |
| QueryExecutionArea | Optional | Invocation and timing statistics related to setting up and executing highlights. It contains the following fields:
|
indexPartitionExplain
the indexPartitionExplain contains Explain Results for each index partition. The top-level collectors and query are inside the explain information of each index partition and absent at the top-level.
metadata
The metadata contains helpful metadata, such as the following:
Field | Type | Necessity | Purpose |
|---|---|---|---|
| String | Optional | Current version of |
| String | Optional | Human readable label that identifies the |
| String | Optional | MongoDB Search index used in the query. |
| Document | Optional | Cursor options given to |
| Integer | Optional | Total number of index objects in the index including deleted documents. |
query
The query BSON document describes the execution statistics for the query. It contains the following fields:
Field | Type | Necessity | Purpose |
|---|---|---|---|
| string | Optional | Path to the operator, only if it isn't the root. |
| string | Required | Name of the Lucene Query that the MongoDB Search operator created. See |
| string | Optional | MongoDB Search analyzer used with the query. |
| document | Required | Lucene query information. See |
| document | Optional |
|
args
The explain response of a search command contains information about the query executed with that command. The response in the args field includes structured details of what Lucene queries MongoDB Search executed to satisfy a $search query.
This section contains:
Some of the Lucene queries that MongoDB Search operators create
Lucene query options that are included in the structured summary
Lucene query structured summary example for each Lucene query type
Note
About the Examples
The examples in this section are based on queries run against the sample datasets with the queryPlanner verbosity mode. In the example response, the:
mongotQueryfield shows the MongoDB Search operator and the query that was run.explain.typefield shows the Lucene Query that the operator created.
For complete examples, see Examples.
BooleanQueryFor Lucene
BooleanQuery, the structured summary includes details on the following options:FieldTypeNecessityDescriptionmustOptional
Clauses which must match.
mustNotOptional
Clauses which must not match.
shouldOptional
Clauses which should match.
filterOptional
Clauses which must all match.
minimumShouldMatchInteger
Optional
The minimum number of
shouldclauses which must match.The following example shows the
explainresponse for a query run against thesample_airbnb.listingsAndReviewscollection.1 { 2 "stages" : [ 3 { 4 "$_internalSearchMongotRemote" : { 5 "mongotQuery" : { 6 "compound" : { 7 "must" : [ { 8 "compound" : { 9 "should" : [ { 10 "text" : { 11 "query" : "historic", 12 "path" : "summary" 13 } 14 }, 15 { 16 "text" : { 17 "query" : "Portugal", 18 "path" : "address.country" 19 } 20 }, 21 { 22 "text" : { 23 "query" : "railway", 24 "path" : "transit" 25 } 26 } ] 27 } 28 } ], 29 "mustNot" : [ { 30 "text" : { 31 "query" : "Apartment", 32 "path" : "property_type" 33 } 34 } ] 35 } 36 }, 37 "explain" : { 38 "type" : "BooleanQuery", 39 "args" : { 40 "must" : [ { 41 "path" : "compound.must", 42 "type" : "BooleanQuery", 43 "args" : { 44 "must" : [ ], 45 "mustNot" : [ ], 46 "should" : [ 47 { 48 "path" : "compound.must.compound.should[0]", 49 "type" : "TermQuery", 50 "args" : { 51 "path" : "summary", 52 "value" : "historic" 53 } 54 }, 55 { 56 "path" : "compound.must.compound.should[1]", 57 "type" : "TermQuery", 58 "args" : { 59 "path" : "address.country", 60 "value" : "portugal" 61 } 62 }, 63 { 64 "path" : "compound.must.compound.should[2]", 65 "type" : "TermQuery", 66 "args" : { 67 "path" : "transit", 68 "value" : "railway" 69 } 70 } 71 ], 72 "filter" : [ ], 73 "minimumShouldMatch" : 0 74 } 75 } ], 76 "mustNot" : [ { 77 "path" : "compound.mustNot", 78 "type" : "TermQuery", 79 "args" : { 80 "path" : "property_type", 81 "value" : "apartment" 82 } 83 } ], 84 "should" : [ ], 85 "filter" : [ ], 86 "minimumShouldMatch" : 0 87 } 88 } 89 }, 90 ... 91 }, 92 ... 93 ], 94 ... 95 }
ConstantScoreQueryFor constant score queries, the structured summary includes details on the following options:
FieldTypeNecessityDescriptionqueryRequired
Child of the
ConstantScoreQuery.The following example shows the
explainresponse for a query run against thesample_airbnb.listingsAndReviewscollection.1 { 2 "stages" : [ 3 { 4 "$_internalSearchMongotRemote" : { 5 "mongotQuery" : { 6 "equals" : { 7 "path" : "host.host_identity_verified", 8 "value" : true 9 } 10 }, 11 "explain" : { 12 "type" : "ConstantScoreQuery", 13 "args" : { 14 "query" : { 15 "type" : "TermQuery", 16 "args" : { 17 "path" : "host.host_identity_verified", 18 "value" : "T" 19 } 20 } 21 } 22 } 23 } 24 }, 25 { 26 "$_internalSearchIdLookup" : { } 27 } 28 ], 29 ... 30 }
FunctionScoreQueryFor Lucene
FunctionScoreQueryqueries, the structured summary includes details on the following options:FieldTypeNecessityDescriptionscoreFunctionstring
Required
Scoring expression used in the query.
queryRequired
The query.
The following example shows the
explainresponse for a query run against thesample_airbnb.listingsAndReviewscollection.1 { 2 "stages" : [ 3 { 4 "$_internalSearchMongotRemote" : { 5 "mongotQuery" : { 6 "near" : { 7 "path" : "accomodates", 8 "origin" : 8, 9 "pivot" : 2 10 } 11 }, 12 "explain" : { 13 "type" : "BooleanQuery", 14 "args" : { 15 "must" : [ ], 16 "mustNot" : [ ], 17 "should" : [ 18 { 19 "type" : "BooleanQuery", 20 "args" : { 21 "must" : [ ], 22 "mustNot" : [ ], 23 "should" : [ 24 { 25 "type" : "FunctionScoreQuery", 26 "args" : { 27 "scoreFunction" : "expr(pivot / (pivot + abs(origin - value)))", 28 "query" : { 29 "type" : "LongDistanceFeatureQuery", 30 "args" : { }, 31 "stats" : { } 32 } 33 } 34 } 35 ], 36 "filter" : [ 37 { 38 "type" : "PointRangeQuery", 39 "args" : { 40 "path" : "accomodates", 41 "representation" : "double", 42 "gte" : 8.000000000000002, 43 "lte" : NaN 44 } 45 } 46 ], 47 "minimumShouldMatch" : 0 48 } 49 }, 50 { 51 "type" : "LongDistanceFeatureQuery", 52 "args" : { }, 53 "stats" : { } 54 } 55 ], 56 "filter" : [ ], 57 "minimumShouldMatch" : 0 58 } 59 } 60 }, 61 ... 62 }, 63 ... 64 ], 65 ... 66 }
LatLonPointDistanceQueryFor Lucene
LatLonPointDistanceQueryqueries, the response contains anstatsonly.The following example shows the
explainresponse for a query run against thesample_airbnb.listingsAndReviewscollection.1 { 2 "stages" : [ 3 { 4 "$_internalSearchMongotRemote" : { 5 "mongotQuery" : { 6 "geoWithin" : { 7 "path" : "address.location", 8 "circle" : { 9 "radius" : 4800, 10 "center" : { 11 "type" : "Point", 12 "coordinates" : [ 13 -122.419472, 14 37.765302 15 ] 16 } 17 } 18 } 19 }, 20 "explain" : { 21 "type" : "LatLonPointDistanceQuery", 22 "args" : { } 23 } 24 } 25 }, 26 ... 27 ], 28 ... 29 }
LatLonShapeQueryFor Lucene
LatLonShapeQueryqueries, the response contains anstatsonly.The following example shows the
explainresponse for a query run against thesample_airbnb.listingsAndReviewscollection.1 { 2 "stages" : [ 3 { 4 "$_internalSearchMongotRemote" : { 5 "mongotQuery" : { 6 "geoShape" : { 7 "path" : "address.location", 8 "relation" : "within", 9 "geometry" : { 10 "type" : "Polygon", 11 "coordinates" : [ 12 [ 13 [ -74.3994140625, 40.5305017757 ], 14 [ -74.7290039063, 40.5805846641 ], 15 [ -74.7729492188, 40.9467136651 ], 16 [ -74.0698242188, 41.1290213475 ], 17 [ -73.65234375, 40.9964840144 ], 18 [ -72.6416015625, 40.9467136651 ], 19 [ -72.3559570313, 40.7971774152 ], 20 [ -74.3994140625, 40.5305017757 ] 21 ] 22 ] 23 } 24 } 25 }, 26 "explain" : { 27 "type" : "LatLonShapeQuery", 28 "args" : { } 29 } 30 }, 31 ... 32 }, 33 ... 34 ], 35 ... 36 }
LongDistanceFeatureQueryFor Lucene
LongDistanceFeatureQuery, the response contains anstatsonly.The following example shows the
explainresponse for a query run against thesample_mflix.moviescollection.1 { 2 "stages" : [ 3 { 4 "$_internalSearchMongotRemote" : { 5 "mongotQuery" : { 6 "near" : { 7 "path" : "released", 8 "origin" : ISODate("1915-09-13T00:00:00Z"), 9 "pivot" : 7776000000 10 } 11 }, 12 "explain" : { 13 "type" : "LongDistanceFeatureQuery", 14 "args" : { } 15 } 16 }, 17 ... 18 }, 19 ... 20 ], 21 ... 22 }
MultiTermQueryConstantScoreWrapperFor Lucene
MultiTermQueryConstantScoreWrapperqueries, the structured summary includes details on the following arguments:FieldTypeNecessityDescriptionqueriesList<Explain Results>
Required
List of queries.
The following example shows the
explainresponse for a query run against thesample_airbnb.listingsAndReviewscollection.1 { 2 "stages" : [ 3 { 4 "$_internalSearchMongotRemote" : { 5 "mongotQuery" : { 6 "regex" : { 7 "path" : "access", 8 "query" : "full(.{0,5})", 9 "allowAnalyzedField" : true 10 } 11 }, 12 "explain" : { 13 "type" : "MultiTermQueryConstantScoreWrapper", 14 "args" : { 15 "queries" : [ 16 { 17 "type" : "DefaultQuery", 18 "args" : { 19 "queryType" : "RegexpQuery" 20 } 21 } 22 ] 23 } 24 } 25 }, 26 ... 27 }, 28 ... 29 ], 30 ... 31 }
PhraseQueryFor Lucene
PhraseQueryqueries, the structured summary includes details on the following arguments:FieldTypeNecessityDescriptionpathString
Required
Indexed field to search.
queryString
Required
String or strings to search for.
slopNumber
Required
Allowable distance between words in the
queryphrase.The following example shows the
explainresponse for a query run against thesample_airbnb.listingsAndReviewscollection.1 { 2 "stages" : [ 3 { 4 "$_internalSearchMongotRemote" : { 5 "mongotQuery" : { 6 "phrase" : { 7 "path" : "description", 8 "query" : "comfortable apartment", 9 "slop" : 2 10 } 11 }, 12 "explain" : { 13 "type" : "PhraseQuery", 14 "args" : { 15 "path" : "description", 16 "query" : "[comfortable, apartment]", 17 "slop" : 2 18 } 19 } 20 }, 21 ... 22 }, 23 ... 24 ], 25 ... 26 }
PointRangeQueryFor Lucene
PointRangeQueryqueries, the structured summary includes details on the following arguments:FieldTypeNecessityDescriptionpathString
Required
Indexed field to search.
representationString
Optional
Numeric representation. Queries over date-typed data do not include representation.
gteNumber
Optional
Lower bound of the query.
lteNumber
Optional
Upper bound of the query.
The following example shows the
explainresponse for a query run against thesample_airbnb.listingsAndReviewscollection.1 { 2 "stages" : [ 3 { 4 "$_internalSearchMongotRemote" : { 5 "mongotQuery" : { 6 "range" : { 7 "path" : "number_of_reviews", 8 "gt" : 5 9 } 10 }, 11 "explain" : { 12 "type" : "BooleanQuery", 13 "args" : { 14 "must" : [ ], 15 "mustNot" : [ ], 16 "should" : [ 17 { 18 "type" : "PointRangeQuery", 19 "args" : { 20 "path" : "number_of_reviews", 21 "representation" : "double", 22 "gte" : 5.000000000000001 23 } 24 }, 25 { 26 "type" : "PointRangeQuery", 27 "args" : { 28 "path" : "number_of_reviews", 29 "representation" : "int64", 30 "gte" : NumberLong(6) 31 } 32 } 33 ], 34 "filter" : [ ], 35 "minimumShouldMatch" : 0 36 } 37 } 38 }, 39 ... 40 }, 41 ... 42 ], 43 ... 44 }
TermQueryFor term queries, the structured summary includes details on the following arguments:
FieldTypeNecessityDescriptionpathString
Required
Indexed field to search.
valueString
Required
String to search for.
The following example shows the
explainresponse for a query run against thesample_airbnb.listingsAndReviewscollection.1 { 2 "stages" : [ 3 { 4 "$_internalSearchMongotRemote" : { 5 "mongotQuery" : { 6 "queryString" : { 7 "defaultPath" : "summary", 8 "query" : "quiet" 9 } 10 }, 11 "explain" : { 12 "type" : "TermQuery", 13 "args" : { 14 "path" : "summary", 15 "value" : "quiet" 16 } 17 } 18 }, 19 ... 20 }, 21 ... 22 ], 23 ... 24 }
DefaultLucene queries that are not explicitly defined by another Lucene query are serialized using the default query. The structured summary includes details on the following option:
FieldTypeNecessityDescriptionqueryTypeString
Required.
Type of Lucene query.
The following example shows the
explainresponse for a query run against thesample_airbnb.listingsAndReviewscollection.1 { 2 "stages" : [ 3 { 4 "$_internalSearchMongotRemote" : { 5 "mongotQuery" : { 6 "near" : { 7 "origin" : { 8 "type" : "Point", 9 "coordinates" : [ 10 -8.61308, 11 41.1413 12 ] 13 }, 14 "pivot" : 1000, 15 "path" : "address.location" 16 } 17 }, 18 "explain" : { 19 "type" : "DefaultQuery", 20 "args" : { 21 "queryType" : "LatLonPointDistanceFeatureQuery" 22 } 23 } 24 }, 25 ... 26 }, 27 ... 28 ], 29 ... 30 }
stats
The explain response for executionStats and allPlansExecution verbosity modes includes a stats field that contains information on how much time a query spends in various stages
of query execution.
Timing Breakdown
The timing breakdown describes execution statistics pertinent to an areas of query execution. The following fields show the timing breakdown:
Field | Type | Description |
|---|---|---|
| Long | |
| Document | Number of invocations of tasks included in this area. The value is a map of task names to their invocation count. |
Areas of Query
Statistics are available for the following areas of query:
Option | Description | |||||||
|---|---|---|---|---|---|---|---|---|
| Statistics related to the execution of the Lucene query. There are two tasks whose invocation counts are enumerated in this area:
The time spent in this area is related to the structure of the query, and is not based on the number of results that are iterated through and scored. For example: | |||||||
| Statistics related to iterating over and matching result documents. This statistic shows the time it takes to determine which document is the next match. Time spent matching results can vary significantly depending on the nature of the query. There are two tasks whose invocation counts are enumerated in this area:
For example: | |||||||
| Statistics related to scoring documents in the result set. There are two tasks whose invocation counts are enumerated in this area:
For example: |
resultMaterialization
The resultMaterialization document shows the amount of time mongot takes to accomplish the following:
Retrieve result data stored in Lucene in the form of either
_idorstoredSource.Serialize the data into BSON format before sending it to
mongod.
To learn more, see stats.
resourceUsage
The resourceUsage document shows the resource used for running the query. It contains the following fields:
Field | Type | Necessity | Purpose |
|---|---|---|---|
| Long | Required | Number of major page faults, which occur when the system can't find the required data in memory resulting in reading from the backing store such as disk, during query execution. |
| Long | Required | Number of minor page faults, which occur when the data is in the page cache, but hasn't yet been mapped to the process' page table. |
| Long | Required | Amount of CPU time, in milliseconds, spent in user space. |
| Long | Required | Amount of CPU time, in milliseconds, spent in system space. |
| Integer | Required | Maximum number of threads that |
| Integer | Required | Total number of batches that |
Examples
The following examples use the movies collection in the sample_mflix database.
Tip
If you've already loaded the sample dataset, refer to the MongoDB Search Quick Start tutorial to create an index definition and run MongoDB Search queries.
allPlansExecution
The following example uses different operators to query the title field with the allPlansExecution verbosity mode.
db.movies.explain("allPlansExecution").aggregate([ { $search: { "text": { "path": "title", "query": "yark", "fuzzy": { "maxEdits": 1, "maxExpansions": 100, } } } } ])
db.movies.explain("allPlansExecution").aggregate([ { $search: { "text": { "path": "title", "query": "prince" }, "highlight": { "path": "title", "maxNumPassages": 1, "maxCharsToExamine": 40 } } }, { $project: { "description": 1, "_id": 0, "highlights": { "$meta": "searchHighlights" } } } ])
db.movies.explain("allPlansExecution").aggregate([ { "$searchMeta": { "facet": { "operator": { "near": { "path": "released", "origin": ISODate("1921-11-01T00:00:00.000+00:00"), "pivot": 7776000000 } }, "facets": { "genresFacet": { "type": "string", "path": "genres" }, "yearFacet" : { "type" : "number", "path" : "year", "boundaries" : [1910,1920,1930,1940] } } } } } ])
db.movies.explain("allPlansExecution").aggregate([ { $search: { "text": { "path": "title", "query": "yark", "fuzzy": { "maxEdits": 1, "maxExpansions": 100, } } } } ])
queryPlanner
The following example uses different operators to query the title field with the queryPlanner verbosity mode.
db.movies.explain("queryPlanner").aggregate([ { $search: { "text": { "path": "title", "query": "yark", "fuzzy": { "maxEdits": 1, "maxExpansions": 100, } } } } ])
db.movies.explain("queryPlanner").aggregate([ { $search: { "text": { "path": "title", "query": "prince" }, "highlight": { "path": "title", "maxNumPassages": 1, "maxCharsToExamine": 40 } } }, { $project: { "description": 1, "_id": 0, "highlights": { "$meta": "searchHighlights" } } } ])
db.movies.explain("queryPlanner").aggregate([ { "$searchMeta": { "facet": { "operator": { "near": { "path": "released", "origin": ISODate("1921-11-01T00:00:00.000+00:00"), "pivot": 7776000000 } }, "facets": { "genresFacet": { "type": "string", "path": "genres" }, "yearFacet" : { "type" : "number", "path" : "year", "boundaries" : [1910,1920,1930,1940] } } } } } ])
db.movies.explain("queryPlanner").aggregate([ { $search: { index: "default", range: { path: "released", gt: ISODate("2015-01-01T00:00:00.000Z"), lt: ISODate("2015-12-31T00:00:00.000Z") }, sort: { released: -1 } } }, { $limit: 5 }, { $project: { _id: 0, title: 1, released: 1 } } ])
For queries that specify a $limit stage in the pipeline, the explain results include the mongotDocsRequested metric, which shows the number of documents that mongod requested from mongot.
Example
{ "mongotQuery": {}, "explain": {}, "limit": <int>, "sortSpec": {}, "mongotDocsRequested": <int>, }
executionStats
The following example uses the autocomplete to query the title field with the executionStats verbosity mode.
1 db.movies.explain("executionStats").aggregate([ 2 { 3 "$search": { 4 "autocomplete": { 5 "path": "title", 6 "query": "pre", 7 "fuzzy": { 8 "maxEdits": 1, 9 "prefixLength": 1, 10 "maxExpansions": 256 11 } 12 } 13 } 14 } 15 ])
To learn more about the explain response elements, see Explain Results.