定义
注意
span操作符已弃用。请改用短语。
spanspan操作符查找文本字段区域内的Atlas Search匹配文本。 您可以使用它以指定的精度查找彼此接近的字符串。 与其他操作符相比,span操作符的计算量更大,因为查询必须跟踪位置信息。span是术语级操作符,这意味着不分析query字段。术语级操作符适用于关键字分析器,因为query字段被视为包含特殊字符的单个术语。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操作符采用以下字段:
选项 | 类型 | 必需? | 说明 |
|---|---|---|---|
| 字符串 | 是 | 要搜索的索引字段。 |
| 字符串 | 是 | Atlas Search的术语或短语。 |
位置操作符
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 采用以下可选位置操作符。
contains
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位置操作符采用以下字段:
字段 | 类型 | 必需? | 说明 | ||||
|---|---|---|---|---|---|---|---|
| 文档 | 是 | One or more positional operators specified recursively or just the term operator. The following table shows the type of query that
| ||||
| 文档 | 是 | One or more positional operators specified recursively or just the term operator. The following table shows the type of query that
| ||||
| 文档 | no | 要应用于此Atlas Search结果的分数。 | ||||
| 字符串 | 是 | 要执行的查询类型和要返回的匹配结果。 值可以是以下之一:
|
例子
以下示例查询使用span.contains查找词语train与词语great和robbery一起出现的文档,其中great和robbery 5 title字段。
1 db.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 返回在词语 great 和 robbery(使用 big 指定)内包含术语train(使用 little 指定)的文档。如果您将第 6 行上的 spanToReturn 设立为 inner, MongoDB搜索将返回相同的文档,因为术语 train(使用 little 指定)出现在词语 great 和 robbery(使用 big)之内。
first
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位置操作符采用以下字段:
选项 | 类型 | 必需? | 说明 |
|---|---|---|---|
| int | no | 指定Atlas Search词语位置的数字。 如果为多个词语指定Atlas Search ,则最后一个词语应小于或等于此值。 如果省略,则默认为 |
| 文档 | 是 | Document that contains the positional operators or term operator options. |
| 文档 | no | 要应用于此Atlas Search结果的分数。 |
例子
以下示例查询使用 span.first 来查找 title 字段中出现指定string的文档。 endPositionLte 参数的值为 2,这意味着使用 term 操作符指定的Atlas Search词语必须是该字段中的第一个或第二个单词。
1 db.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字段的第一个或第二个位置包含搜索词 dance 的文档。
1 db.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搜索返回在 title字段的第一个或第二个位置包含搜索词 man 或 woman 的文档。MongoDB搜索不会在同一 title 中返回两个搜索词,因为该示例包含 or操作符clauses 来指定搜索词。
near
near 位置操作符匹配两个或多个包含彼此相邻的Atlas Search词的子句。 您可以使用Atlas Search 位置操作符 列表以递归方式指定 术语,也可以仅使用 术语 操作符。
语法
near位置操作符采用以下语法:
{ "$search": { "span": { "near": { "clauses": [ { <span-positional-or-term-operator-specification> }, ... ], "slop": <distance-number>, "inOrder": true|false } } } }
字段
near位置操作符采用以下字段:
字段 | 类型 | 必需? | 说明 |
|---|---|---|---|
| 文档数组 | 是 | Span clauses that must be near one another. Clauses can't be empty. Each document contains |
| 布尔 | no | 该标志指定Atlas Search是否必须按指定的顺序搜索子句中的术语并且不能重叠。 值可以是以下值之一:
如果省略,则默认值为 |
| 文档 | no | 要应用于此Atlas Search结果的分数。 |
| 整型 | no | 子句中术语之间的允许距离。 较低的值允许词语之间的位置距离较小,较大的值允许词语之间的距离较大,以满足查询要求。 默认值为 |
例子
以下示例查询使用 span.near Atlas Search字符串 prince 和 pauper 彼此接近的文档。 inOrder 参数设置为 false,因此Atlas Search词语可以按任意顺序排列。 slop 参数设置为 4,因此Atlas Search术语最多只能用 4 个单词分隔。
1 db.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 在 title字段中返回包含搜索词 prince 和 pauper 的文档,这些词之间的间隔少于四个单词。
or
or位置操作符匹配两个或多个子句中的任何一个。 您可以使用Atlas Search 位置操作符 列表以递归方式指定 术语,也可以仅使用 术语 操作符。
语法
or位置操作符采用以下语法:
{ "$search": { "span": { "or": { "clauses": [ { <span-positional-or-term-operator-specification> }, ... ], "score": { <scoring-options> } } } } }
字段
or位置操作符采用以下字段:
选项 | 类型 | 必需? | 说明 |
|---|---|---|---|
| 文档数组 | 是 | Span clauses that specify the search terms. One of the clauses must match, and clauses can't be empty. Each document must contain |
| 文档 | no | 要应用于此Atlas Search结果的分数。 |
例子
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.
1 db.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 返回 title字段中包含搜索词 city 或 country 的文档,但不会同时包含在同一 title 中。
subtract
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位置操作符采用以下字段:
选项 | 类型 | 必需? | 说明 |
|---|---|---|---|
| 文档 | 是 | Document that specifies the term or phrase matches to remove that overlap with the term or phrase matches specified in the |
| 文档 | 是 | Document that specifies the term matches to include using any positional operators or just the term operator. |
| 文档 | no | 要应用于此Atlas Search结果的分数。 |
例子
以下示例查询使用 span.subtract Atlas Search以下文档:title 字段包含单词 father 和 son(顺序不限),且彼此相差不超过 3 个单词。 它排除在father和son之间出现单词like的任何文档。
1 db.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 Search 不会返回标题为 Like Father Like Son 的文档,因为尽管 title字段包含单词 father 和 son,但它们之间还有 like,即 exclude 条件中的查询。