AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

queryString 演算子

queryString

queryString演算子は、インデックス付きフィールドと値の組み合わせのクエリをサポートしています。 queryString演算子を使用して、 stringフィールドでテキスト、ワイルドカード、正規表現、ファジー、範囲検索を実行できます。

queryString の構文は次のとおりです。

1{
2 "$search": {
3 "index": <index name>, // optional, defaults to "default"
4 "queryString": {
5 "defaultPath": "<default-field-to-search>",
6 "query": "(<field-to-search>: (<search-values>) AND|OR (<search-values>)) AND|OR (<search-values>)"
7 }
8 }
9}

queryString では、次の用語を使用してクエリを作成します。

フィールド
タイプ
説明
必要性

defaultPath

string

デフォルトで検索するインデックスフィールド。MongoDB Search では、フィールドを省略して query で検索する場合にのみ、defaultPath のフィールドが検索されます。

はい

query

string

検索する 1 つ以上のインデックス付きフィールドと値。 フィールドと値はコロンで区切られます。 たとえば、 plotフィールドで string baseballを検索するには、 plot:baseballを使用します。 複数の検索条件を組み合わせるには、次の演算子を使用できます。

次のように、フィールドと値を組み合わせることができます。

  • AND: AND ブール値演算子を示します。ドキュメントが結果に含まれるには、すべての検索値が存在する必要があります。

  • OR: OR ブール値演算子を示します。ドキュメントを結果に含めるには、検索値の少なくとも1つが存在する必要があります。

  • NOT: NOT ブール値演算子を示します。ドキュメントが結果に含まれるためには、指定された検索値が存在しない必要があります。

  • TO: 検索する範囲を示します。[] は両端を含む範囲、{} は両端を含まない範囲、{] と [} は半開範囲に使用できます。ドキュメントが結果に含まれるには、指定された範囲内に収まる値が存在する必要があります。

  • ()サブクエリの区切り文字。括弧を使用して、検索するフィールドと値をグループ化します。

次の方法を使用して、ワイルドカードおよび正規式のクエリを実行できます。

  • ?: 任意の 1 文字と一致することを示します。

  • *: 0 文字以上の文字と一致することを示します。

    queryString演算子は、 *をクエリの最初の文字として含むワイルドカード クエリをサポートしていません。 queryString演算子は、 *の前にある文字をプレフィックスとして、または完全に一致する必要がある文字として扱います。

  • /: 正規表現の区切り文字。

  • ~指定された編集数まで検索期間に類似した文字列を見つけるためのファジー検索を示します。例として、{ "query":"foo":"stop"~2 } は以下と等しくなります。

    { "defaultPath": "foo", "query": "stop", "maxEdits": 2 }

    queryString演算子は、最大2編集のファジー検索のみをサポートします。 結果が多くの辞書タームと一致する可能性があるため、距離が長いと有用ではありません。

    stringに複数のタームとともに ~ を使用する場合、queryString 演算子は、指定されたターム数内のタームを近接検索します。 たとえば、 { "query":"foo":"New York"~2 }は次の値と等しいです。

    { "defaultPath": "foo", "query": "New York", "slop": 2 }

はい

score

オブジェクト

一致する検索結果に割り当てられたスコア。 デフォルトのスコアは、次のオプションを使用して変更できます。

  • boost: 結果のスコアに指定された数値を掛けます。

  • constant: 結果のスコアを指定された数値に置き換えます。

  • function: 結果のスコアを指定された式で置き換えます。

配列値をクエリする場合、配列内のクエリに一致する値が多いほど、 MongoDB Search はより高いスコアを割り当てます。

クエリで score を使用する方法については、「結果内のドキュメントのスコアリング」を参照してください。

no

デフォルトでは、text 演算子はbm25の類似性アルゴリズムを使用して、クエリとの関連性に応じてドキュメントにスコアを付けます。

MongoDB Search インデックス定義のstringフィールドのsimilarity.typeプロパティで別の類似性アルゴリズムを指定することにより、使用する類似性アルゴリズムを変更できます。string型の MongoDB Search インデックスを設定する方法については、「文字列フィールドのインデックス作成方法」を参照してください。

サポートされている類似性アルゴリズムの詳細については、 「スコアの詳細」を参照してください。

The following examples use the movies collection in the sample_mflix database. If you have the sample dataset on your cluster , you can create the MongoDB Search index named default with dynamic mappings and run the example queries on your cluster.

The following example uses the queryString operator to query for movies with the title Rocky and either IV, 4, or Four at path title in the movies collection. In the following query, a field is specified in the defaultPath, but no fields are specified in the query. The query includes a $project stage to exclude all fields except title.

1db.movies.aggregate([
2 {
3 $search: {
4 "queryString": {
5 "defaultPath": "title",
6 "query": "Rocky AND (IV OR 4 OR Four)"
7 }
8 }
9 },
10 {
11 $project: {
12 "_id": 0,
13 "title": 1
14 }
15 }
16])

このクエリでは、query にフィールドがないため、 MongoDB Search は defaultPath で検索を実行します。これにより、次の結果が返されます。

{ "title" : "Rocky IV" }

このセクションの例では、queryString 演算子を使用して、captain、kirk、chess という用語を含む映画のプロットをクエリします。この例では、同じ検索語を括弧でグループ化する方法の違いによって、検索結果に含まれるドキュメントがどのように異なるかを示しています。

The queries also includes a $project stage to:

  • すべてのフィールドを除外title 、 plot 、 fullpath

  • 次のフィールドを追加: score

次のクエリは、 plotフィールドでchessとcaptainまたはkirkのいずれかを検索します。

1db.movies.aggregate([
2 {
3 $search: {
4 "queryString": {
5 "defaultPath": "fullplot",
6 "query": "plot:(captain OR kirk) AND chess"
7 }
8 }
9 },
10 {
11 $project: {
12 "_id": 0,
13 "title": 1,
14 "plot": 1,
15 "fullplot": 1,
16 score: { $meta: "searchScore" }
17 }
18 }
19])
{
"fullplot" : "When the crew of the Enterprise is called back home, they find an unstoppable force of terror from within their own organization has detonated the fleet and everything it stands for, leaving our world in a state of crisis. With a personal score to settle, Captain Kirk leads a manhunt to a war-zone world to capture a one-man weapon of mass destruction. As our heroes are propelled into an epic chess game of life and death, love will be challenged, friendships will be torn apart, and sacrifices must be made for the only family Kirk has left: his crew.",
"plot" : "After the crew of the Enterprise find an unstoppable force of terror from within their own organization, Captain Kirk leads a manhunt to a war-zone world to capture a one-man weapon of mass destruction.",
"title" : "Star Trek Into Darkness",
"score" : 7.968792915344238
}

plot には captain と kirk という用語が含まれていますが、一致条件を満たすために必要なのは 1 つだけで、plot にはありませんが、defaultPath の fullplot には chess という用語が表示されるため、結果のドキュメントは一致します。

次の例では、 queryString演算子を使用して、 countから任意の文字(ワイルドカード文字*を使用して定義)までのテキスト値の範囲を、アルファベットの昇順で映画のタイトルに対してクエリします。

1db.movies.aggregate([
2 {
3 "$search": {
4 "queryString": {
5 "defaultPath": "plot",
6 "query": "title:[count TO *]"
7 }
8 }
9 },
10 {
11 "$limit": 10
12 },
13 {
14 "$project": {
15 "_id": 0,
16 "title": 1
17 }
18 }
19])
1[
2 { title: 'Blacksmith Scene' },
3 { title: 'The Great Train Robbery' },
4 { title: 'The Land Beyond the Sunset' },
5 { title: 'A Corner in Wheat' },
6 { title: 'Winsor McCay, the Famous Cartoonist of the N.Y. Herald and His Moving Comics' },
7 { title: 'Traffic in Souls' },
8 { title: 'Gertie the Dinosaur' },
9 { title: 'In the Land of the Head Hunters' },
10 { title: 'The Perils of Pauline' },
11 { title: 'The Birth of a Nation' }
12]

MongoDB Search 結果には、クエリの範囲は文字 count から始まりますが、Blacksmith Scene と A Corner in Wheat というタイトルの映画が含まれます。デフォルトのアナライザ、lucene.standard でインデックスを作成すると、 MongoDB Search は titleフィールドのターム用に個別のトークンを作成し、クエリタームを個々のトークンと照合します。具体的には、 MongoDB Search は次のトークンを作成します。このトークンの少なくとも 1 つ(ルート で示)がクエリ条件に一致します。

タイトル
標準アナライザ トークン

ブラックメソッド フィールド

blacksmith, sceneルート

お茶の特権

a, corner, in, wheat √

lucene.keywordアナライザを使用してインデックスを作成すると、 MongoDB Search は titleフィールドの string 全体を対象に単一のトークンを作成し、同じクエリに対して次のような結果が得られます。

[
{ title: 'è Nous la Libertè' },
{ title: 'tom thumb' },
{ title: 'è nos amours' },
{ title: 'èke och hans vèrld' },
{ title: 'èdipussi' },
{ title: 's/y Glèdjen' },
{ title: 'èAy, Carmela!' },
{ title: 'èlisa' },
{ title: 'èxtasis' },
{ title: 'eXistenZ' }
]

The following examples use the queryString operator to query for movie titles using fuzzy, wildcard, and regular expressions. The queries include a $project stage to exclude all fields except title.

次のクエリでは、ファジー( ~ )を使用して、 titleフィールドで、最大 2 文字のバリエーションを持つcatchに類似するタームを含む映画タイトルをファジー検索します。

1db.movies.aggregate([
2 {
3 "$search": {
4 "queryString": {
5 "defaultPath": "title",
6 "query": "catch~2"
7 }
8 }
9 },
10 {
11 "$limit": 10
12 },
13 {
14 "$project": {
15 "_id": 0,
16 "title": 1
17 }
18 }
19])
1[
2 { title: 'Catch-22' },
3 { title: 'Catch That Girl' },
4 { title: 'Catch That Kid' },
5 { title: 'Catch a Fire' },
6 { title: 'Catch Me Daddy' },
7 { title: 'Death Watch' },
8 { title: 'Patch Adams' },
9 { title: "Batch '81" },
10 { title: 'Briar Patch' },
11 { title: 'Night Watch' }
12]

次のクエリは、movies コレクションの title フィールドで Marvel、Avengers、Iron Man、または Captain America を検索します。

次のクエリは、検索条件に一致する次のバケット(年)の映画を返します。

  • 2000 (このバケットの下限値を含む)

  • 2005: 2000 バケットの 排他的上限であり、このバケットの下限

  • 2010: 2005 バケットの 排他的上限であり、このバケットの下限

  • 2015: 2010 バケットの 排他的上限であり、このバケットの下限

  • 2020: 2015 の排他的上限

1db.movies.aggregate([
2 {
3 "$searchMeta": {
4 "facet": {
5 "operator": {
6 "queryString": {
7 "defaultPath": "title",
8 "query": "\"Marvel\" OR \"Avengers\" OR \"Iron Man\" OR \"Captain America\""
9 }
10 },
11 "facets": {
12 "yearFacet": {
13 "type": "number",
14 "path": "year",
15 "boundaries": [1990, 2000, 2005, 2010, 2015, 2020 ]
16 }
17 }
18 }
19 }
20 }
21])