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

span (MongoDB Search 演算子)

注意

span 演算子は非推奨です。代わりに、というフレーズを使用してください。

span

span演算子は、テキストフィールドのリージョン内に一致するテキスト検索を検索します。 これを使用して、指定された精度で相互に近い文字列を検索できます。 span演算子は、クエリが位置情報を追跡する必要があるため、他の演算子よりも計算が集中しています。

span はタームレベルの演算子であり、 queryフィールドは分析されないことを意味します。 タームレベルの演算子はキーワード アナライザと連携します。 queryフィールドは特殊文字を含む 1 つのタームとして扱われるためです。

span クエリはスコアによってランク付けされません。

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

{
$search: {
"index": <index name>, // optional, defaults to "default"
"span": {
"term" | <positional-operator>": {
<operator-specification>
}
}
}
}

注意

span 検索クエリでは、複合演算子を使用できません。

検索するタームを指定するには、 term演算子を使用します。 term演算子は必須であり、 span位置演算子とともに使用する場合は、位置演算子の最も遠い子である必要があります。

term演算子の構文は次のとおりです。

"term": {
"path": "<path-to-field>",
"query": "<terms-to-search>"
}

term演算子は次のフィールドを取ります。

オプション
タイプ
必須
説明

path

string

はい

検索するインデックス付きフィールド。

query

string

はい

検索するタームまたはフレーズ。

You can use the positional operators to specify the position of the terms that you want to search with the term operator. The positional operators are of type document. You must specify at least one positional operator in your span operator query. The positional operators can take other span positional operators, recursively.

注意

例について

The examples on this page use the sample_mflix.movies collection. If you load the sample data and create a dynamic index named default on the movies collection , you can run the following $search sample queries against the collection. The sample queries use the $limit stage to limit the results to 5 documents and the $project stage to exclude all fields except the title field in the results.

span では、次の任意の位置演算子があります。

The contains positional operator matches terms that are contained within other terms. You can use positional operators recursively or just the term operator within contains to specify the search terms.

containsの位置演算子は、次の構文をとります。

{
"$search": {
"span": {
"contains": {
"spanToReturn": "inner"|"outer",
"little": { <positional-or-term-operator-specification> },
"big": { <positional-or-term-operator-specification> }
}
}
}
}

位置演算子containsは次のフィールドを取ります。

フィールド
タイプ
必須
説明

big

ドキュメント

はい

One or more positional operators specified recursively or just the term operator. The following table shows the type of query that span executes for big based on the value of spanToReturn.

outer

span は、 littleのタームを 1 つ以上含むbigのタームと一致します。

inner

span は、 bigからのターム内にあるlittleのタームに一致します。

little

ドキュメント

はい

One or more positional operators specified recursively or just the term operator. The following table shows the type of query that span executes for little based on the value of spanToReturn.

inner

span は、 bigからのターム内にあるlittleのタームに一致します。

outer

span は、 littleのタームを 1 つ以上含むbigのタームと一致します。

score

ドキュメント

no

この検索の結果に適用する スコア 。

spanToReturn

string

はい

実行するクエリのタイプと、返される一致する結果のタイプ。 値は次のいずれかになります。

  • inner - クエリで、 bigのターム内にあるlittleのタームに一致するものを実行します

  • outer - littleのタームが少なくとも 1 つ含まれるbigのタームと一致するクエリを実行します

次のクエリの例では、 span.containsを使用して、用語trainが用語greatrobberyとともに表示されるドキュメントを検索します。ここで、 greatrobberyは最大title 5の位置にある可能性があり、 フィールド

1db.movies.aggregate([
2 {
3 "$search": {
4 "span": {
5 "contains": {
6 "spanToReturn": "outer",
7 "little": {
8 "term": {
9 "path": "title",
10 "query": "train"
11 }
12 },
13 "big": {
14 "near": {
15 "clauses": [
16 {
17 "term": {
18 "path": "title",
19 "query": "great"
20 }
21 },
22 {
23 "term": {
24 "path": "title",
25 "query": "robbery"
26 }
27 }
28 ],
29 "slop": 5
30 }
31 }
32 }
33 }
34 }
35 },
36 {
37 "$limit": 5
38 },
39 {
40 "$project": {
41 "_id": 0,
42 "title": 1
43 }
44 }
45])
[
{ title: 'The Great Train Robbery' },
{ title: 'The Great Train Robbery' },
{ title: "The Great St. Trinian's Train Robbery" }
]

MongoDB Search では、 trainlittlegreatと ( を使用して指定)内にタームrobberybig を使用して指定)を含むドキュメントが返されます。spanToReturn6行の をinner に設定すると、 MongoDB Searchtrain littlegreatrobberybigは同じドキュメントを返します。これは、ターム ( を使用して指定)が用語 と という用語({ を使用して指定: を使用して指定)に出現するためです。 )。

The first positional operator identifies the position of the search term by using a specified number. You can specify the search terms using positional operators recursively, or just the term operator. span matches documents where the position of the search term is less than or equal to the specified number.

firstの位置演算子は、次の構文をとります。

{
"$search": {
"span": {
"first": {
"endPositionLte": <term-position>,
"operator": { <span-positional-or-term-operator-specification> },
"score": { <score-options> }
}
}
}
}

位置演算子firstは次のフィールドを取ります。

オプション
タイプ
必須
説明

endPositionLte

整数

no

検索タームの位置を指定する数値。 複数のタームの検索を指定する場合、最後のタームはこの値以下である必要があります。 省略した場合、デフォルトは3になります。

operator

ドキュメント

はい

Document that contains the positional operators or term operator options.

score

ドキュメント

no

この検索の結果に適用する スコア 。

次のクエリの例では、 span.firstを使用して、指定された string がtitleフィールドに表示されるドキュメントを検索します。 endPositionLteパラメータの値は2です。つまり、 term演算子を使用して指定される検索タームは、 フィールドの最初または 2 番目の単語である必要があります。

1db.movies.aggregate([
2 {
3 "$search": {
4 "span": {
5 "first": {
6 "endPositionLte": 2,
7 "operator": {
8 "term": {
9 "path": "title",
10 "query": "dance"
11 }
12 }
13 }
14 }
15 }
16 },
17 {
18 "$limit": 5
19 },
20 {
21 "$project": {
22 "_id": 0,
23 "title": 1
24 }
25 }
26])
[
{ title: 'Dance Program' },
{ title: 'Slam Dance' },
{ title: 'Last Dance' },
{ title: 'War Dance' },
{ title: 'Delhi Dance' }
]

MongoDB Search では、titleフィールドの最初または 2 番目の位置に検索という単語 dance が含まれるドキュメントが返されます。

1db.movies.aggregate([
2 {
3 "$search": {
4 "span": {
5 "first": {
6 "endPositionLte": 2,
7 "operator": {
8 "or": {
9 "clauses": [
10 { "term": { "path": "title", "query": "man" } },
11 { "term": { "path": "title", "query": "woman" } }
12 ]
13 }
14 }
15 }
16 }
17 }
18 },
19 {
20 "$limit": 5
21 },
22 {
23 "$project": {
24 "_id": 0,
25 "title": 1
26 }
27 }
28])
[
{ title: "Everybody's Woman" },
{ title: 'Marked Woman' },
{ title: 'Wonder Man' },
{ title: 'Designing Woman' },
{ title: 'Watermelon Man' }
]

MongoDB Search は、titleフィールドの最初または 2 番目の位置に検索という単語 man または woman を含むドキュメントを返します。MongoDB Search では、検索タームを指定するために or 演算子 clauses が含まれているため、同じ title 内の両方の検索タームは返されません。

nearは、検索タームを含む 2 つ以上の句と一致します。 検索タームは、位置演算子のリストを再帰的に使用することも、ターム演算子のみを使用して指定することもできます。

nearの位置演算子は、次の構文をとります。

{
"$search": {
"span": {
"near": {
"clauses": [
{ <span-positional-or-term-operator-specification> },
...
],
"slop": <distance-number>,
"inOrder": true|false
}
}
}
}

位置演算子nearは次のフィールドを取ります。

フィールド
タイプ
必須
説明

clauses

ドキュメントの配列

はい

Span clauses that must be near one another. Clauses can't be empty. Each document contains span positional or just the term operator options.

inOrder

ブール値

no

句内のタームの検索が指定された順序に従い、重複してはなりませんという条件を指定するフラグ。

値は次のいずれかになります。

  • true - 句内の指定された順序で、重複することなくタームを検索します

  • false - 句内の任意の順序でタームを検索します

省略した場合、デフォルトは false になります。

score

ドキュメント

no

この検索の結果に適用する スコア 。

slop

integer

no

句内のターム間の許容距離。 値が低いほどターム間の位置的距離が短くなり、値が大きいとクエリを満たす単語間の距離が長くなります。 デフォルトは0です。つまり、一致と見なされるには、異なる 句内の単語が隣接している必要があります。

次のクエリ例では、 span.nearを使用して、string princepauperが相互に近くで見つかったドキュメントを検索します。 inOrderパラメータはfalseに設定されているため、検索タームは任意の順序で使用できます。 slopパラメータは4に設定されているため、検索タームは最大 4 単語のみで区切ることができます。

1db.movies.aggregate([
2 {
3 "$search" : {
4 "span": {
5 "near": {
6 "clauses": [
7 { "term": { "path": "title", "query": "prince" } },
8 { "term": { "path": "title", "query": "pauper" } }
9 ],
10 "slop": 4,
11 "inOrder": false
12 }
13 }
14 }
15 },
16 {
17 "$limit": 5
18 },
19 {
20 "$project": {
21 "_id": 0,
22 "title": 1
23 }
24 }
25])
[ { title: 'The Prince and the Pauper' } ]

MongoDB Search では、検索単語「prince」と「pauper」を含むドキュメントが、4 単語未満で区切られたドキュメントを titleフィールドに返します。

orは、2 つ以上の 句のいずれかに一致します。 検索タームは、位置演算子のリストを再帰的に使用することも、ターム演算子のみを使用して指定することもできます。

orの位置演算子は、次の構文をとります。

{
"$search": {
"span": {
"or": {
"clauses": [
{ <span-positional-or-term-operator-specification> },
...
],
"score": { <scoring-options> }
}
}
}
}

位置演算子orは次のフィールドを取ります。

オプション
タイプ
必須
説明

clauses

ドキュメントの配列

はい

Span clauses that specify the search terms. One of the clauses must match, and clauses can't be empty. Each document must contain span positional operators specified recursively or just the term operator options.

score

ドキュメント

no

この検索の結果に適用する スコア 。

The following example query uses span.or clauses to specify two term operator queries that search for documents in which the title field has either city or country.

1db.movies.aggregate([
2 {
3 "$search" : {
4 "span": {
5 "or": {
6 "clauses": [
7 { "term": { "path": "title", "query": "city" } },
8 { "term": { "path": "title", "query": "country" } }
9 ],
10 }
11 }
12 }
13 },
14 {
15 "$limit": 5
16 },
17 {
18 "$project": {
19 "_id": 0,
20 "title": 1
21 }
22 }
23])
[
{ title: 'Country' },
{ title: 'City Lights' },
{ title: 'King & Country' },
{ title: 'Fat City' },
{ title: 'Atlantic City' }
]

MongoDB Search では、検索単語 city または country を含むドキュメントが titleフィールドに返されますが、同じ title では両方が返されません。

The subtract positional operator removes matches that overlap with another match. You can specify the search terms using a list of positional operators recursively or just the term operator. The subtract clause can be used to exclude certain strings from your search results.

subtractの位置演算子は、次の構文をとります。

{
"$search": {
"span": {
"subtract": {
"include": { <span-positional-or-term-operator-specification> },
"exclude": { <span-positional-or-term-operator-specification> }
}
}
}
}

位置演算子subtractは次のフィールドを取ります。

オプション
タイプ
必須
説明

exclude

ドキュメント

はい

Document that specifies the term or phrase matches to remove that overlap with the term or phrase matches specified in the include field. You can specify the term or phrase using any span positional operators and the term operator.

include

ドキュメント

はい

Document that specifies the term matches to include using any positional operators or just the term operator.

score

ドキュメント

no

この検索の結果に適用する スコア 。

次のクエリ例では、 span.subtractを使用して、 titleフィールドにfathersonという単語が含まれているドキュメントを、相互に3単語以内に任意の順序で検索します。 ただし、 fathersonの間に単語likeが出現するドキュメントは除外されます。

1db.movies.aggregate([
2 {
3 "$search" : {
4 "span": {
5 "subtract": {
6 "include": {
7 "near": {
8 "clauses": [
9 { "term": { "path": "title", "query": "father" } },
10 { "term": { "path": "title", "query": "son" } }
11 ],
12 "inOrder": false,
13 "slop": 3
14 }
15 },
16 "exclude": { "term": { "path": "title", "query": "like" } }
17 }
18 }
19 }
20 },
21 {
22 "$limit": 5
23 },
24 {
25 "$project": {
26 "_id": 0,
27 "title": 1
28 }
29 }
30])
[
{ title: 'Father, Son & Holy Cow' },
{ title: 'My Father and My Son' },
{ title: 'Jimmy Rosenberg: The Father, the Son & the Talent' }
]

MongoDB SearchLike Father Like Son titlefathersonlikeexcludeでは次のドキュメントがフィールドれません:クエリを実行します。