パイプライン ステージ
MongoDB Search queries take the form of an aggregation pipeline stage. MongoDB Search provides $search and $searchMeta stages, both of which must be the first stage in any query pipeline, including the $lookup and $unionWith sub-pipelines. These stages can be used in conjunction with other aggregation pipeline stages in your query pipeline.
選択したパイプライン ステージに基づいて、クエリはフルテキスト検索の検索結果、または検索結果に関するメタデータのいずれかを返します。
集計パイプライン ステージ | 目的 |
|---|---|
全文検索の検索結果を返します。 | |
検索結果に関するメタデータを返します。 |
演算子とコレクター
MongoDB Search はクエリ 演算子 と コレクション も備えています。これらツールは $search と $searchMeta 集計パイプラインステージ内で使用できます。MongoDB Search 演算子を使用すると、クラスターのコレクションから関連データを検索して取得できます。コレクターは、メタデータの検索結果を表すドキュメントを返します。
MongoDB Search 演算子を使用して、ターム、フレーズ、地理的形状とポイント、数値、類似ドキュメント、シノニム(同意語)などをクエリできます。
You can also search using regex and wildcard expressions. The MongoDB Search compound operator allows you to combine multiple operators inside your $search stage to perform a complex search and filter of data based on what must, must not, or should be present in the documents returned by MongoDB Search. You can use the compound operator to also match or filter documents in the $search stage itself. Running $match after $search is less performant than running $search with the compound operator.
演算子とコレクターの詳細については、「演算子とコレクター」を参照してください。
クエリ処理
mongod同じノードで とmongotWhen you run a query, MongoDB Search uses the configured read preference to identify the node on which to run the query. The query first goes to the MongoDB process, which is
mongodfor a replica set cluster ormongosfor a sharded cluster.For a replica set cluster, the MongoDB process routes the query to the
mongoton the same node. For sharded clusters, your cluster data is partitioned acrossmongodinstances and eachmongotknows about the data on themongodon the same node only. Therefore, you can't run MongoDB Search queries that target a particular shard.mongosdirects the queries to all shards, making these scatter gather queries. If you use zones to distribute a sharded collection over a subset of the shards in the cluster, MongoDB Search routes the query to the zone that contains the shards for the collection that you are querying and runs your$searchqueries on just the shards where the collection is located.MongoDB Search は検索とスコアリングを実行し、一致する結果のドキュメントID とその他の検索メタデータを
mongodに返します。次に、mongodは一致する結果のクエリをドキュメント全体で暗黙的に実行し、その結果をクライアントに返します。クエリで
$search同時実行オプションを使用すると、MongoDB Search はクエリ内並列処理を有効にします。詳細については、「セグメント間でのクエリ実行の並列化」を参照してください。mongod異なるノードの とmongotWhen you run a query, the query first goes to the
mongodbased on the configured read preference. By default, themongodprocess routes the search query through a load balancer on the same node, which distributes the requests across all of themongotprocesses. Alternatively, if you set thesearchNodePreferenceparameter for your$searchstage, MongoDB Search runs your query against the same search node each time.MongoDB Search
mongotプロセスは検索とスコアリングを実行し、一致する結果のドキュメントID とメタデータをmongodに返します。次に、mongodはドキュメント全体で一致する結果の検索を実行し、その結果をクライアントに返します。クエリで$search同時実行 オプションを使用すると、 MongoDB Search はクエリ内並列処理を有効にします。詳細については、セグメント間でのクエリ実行の並列化 を参照してください。
スコアリング
MongoDB Search は関連性ベースのスコアを結果セット内の各ドキュメントに関連付けます。関連性を基準にスコアリングすることで、 MongoDB Search は最高スコアから最低スコアの順にドキュメントを返すことができます。MongoDB Search は、クエリタームがドキュメントに頻繁に表示される場合は高く、クエリタームがコレクション内の多数のドキュメントにわたり表示される場合は低くなります。MongoDB Search では、boost、decay などのスコアの変更関数を使用することで、関連性ベースのデフォルトスコアをカスタマイズすることもできます。結果内のスコアをカスタマイズする方法の詳細については、「結果内のドキュメントのスコアリング」を参照してください。
サポートされているクライアント
次のクライアントを使用して、 MongoDB Search クエリを作成および実行できます。
クエリのトラブルシューティング
空の結果セット
mongot はエラーを返しませんが、 $searchクエリが次の場合には空の結果セットを返します。
存在しないインデックスを参照します。クエリでインデックス名を指定しない場合、 MongoDB Search はデフォルトで
defaultという名前のインデックスを使用します。defaultという名前のインデックスがない場合、または指定したインデックスが存在しない場合、 MongoDB Search はエラーを返さず、空の結果セットを返します。indexオプションを使用して、名前で有効なインデックスを指定できます。Uses the
textoperator on a field path which is not indexed as astringtype. If a field is indexed as a MongoDB Search field type other thanstring, such asstringFacetorautocomplete, MongoDB Search doesn't return an error and returns an empty result set. You must index the fields withstringBSON data type values as string type to query the fields using the text operator.
PlanExecutor エラー
mongot は、 $searchクエリが次の場合にPlanExecutorエラーを返します。
誤ったデータ型としてインデックス付けされたフィールドを指定します。この場合、 クエリを実行すると、 MongoDB Search は、誤ってインデックス作成されたフィールドとその正しいデータ型 を識別するエラー メッセージを返します。(例: )。
PlanExecutor error during aggregation :: caused by :: Cannot facet on field "genres" because it was not indexed as a "stringFacet" field. 例、
facetstringnumber、 、またはdateフィールドに対して (MongoDB Search 演算子)クエリを実行するには、stringFacetnumber、 などの対応するMongoDB Searchフィールドタイプを使用してフィールドのインデックスを作成します。それぞれ とdateです。詳細については、サポートされているデータ型とサポートされていないデータ型 を参照してください。