“文档” 菜单
文档首页
/
MongoDB 阿特拉斯
/ / / /

span

在此页面上

  • 定义
  • 语法
  • 术语操作符
  • 语法
  • 字段
  • 位置操作符
  • contains
  • first
  • near
  • or
  • subtract

注意

span操作符已弃用。请改用短语运算符。

span

span 操作符查找文本字段区域内的Atlas Search匹配文本。 您可以使用它以指定的精度查找彼此接近的字符串。 与其他操作符相比, span操作符的计算量更大,因为查询必须跟踪位置信息。

span 是术语级操作符,这意味着不分析query字段。术语级操作符适用于关键字分析器,因为query字段被视为包含特殊字符的单个术语。

span 查询不按分数排名。

span 通过以下语法实现:

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

注意

span 搜索查询不能使用复合操作符。

您可以使用 term 操作符将搜索词指定为Atlas Search 。 term操作符是必需的,当您将其与span 位置操作符一起使用时,它必须是位置操作符的最内层子操作符。

term操作符的语法如下:

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

term操作符采用以下字段:

选项
类型
必需?
说明
path
字符串
要搜索的索引字段。
query
字符串
Atlas Search的术语或短语。

您可以使用位置操作符来指定要使用Atlas Search 术语 操作符在 中搜索的术语的位置。位置操作符的类型为document 。 您必须在span操作符查询中指定至少一个位置操作符。 位置操作符可以递归地获取其他span位置操作符。

注意

关于示例

此页面上的示例使用sample_mflix.movies集合。 如果 加载样本数据 并在default 电影集合 上 创建 名为 的 动态 索引,则可以对该集合运行以下$search 样本查询。示例查询使用$limit阶段将结果限制为5文档,并使用$project阶段排除结果中除title字段之外的所有字段。

span 采用以下可选位置操作符。

contains位置操作符匹配其他术语中包含的术语。 您可以递归使用位置操作符,也可以仅使用 contains 中的术语操作符来指定Atlas Search术语。

contains位置操作符采用以下语法:

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

contains位置操作符采用以下字段:

字段
类型
必需?
说明
big
文档

以递归方式指定的一个或多个位置操作符或仅指定术语操作符。 下表显示了span spanToReturn big查询类型。

outer
span匹配big中至少包含一个little术语的术语。
inner
span匹配来自little且位于big中的术语内的术语。
little
文档

以递归方式指定的一个或多个位置操作符或仅指定术语操作符。 下表显示了span spanToReturn little查询类型。

inner
span匹配来自little且位于big中的术语内的术语。
outer
span匹配big中至少包含一个little术语的术语。
score
文档
没有
要应用于此Atlas Search结果的分数。
spanToReturn
字符串

要执行的查询类型和要返回的匹配结果。 值可以是以下之一:

  • inner - 执行内部查询,匹配位于big术语内部的little术语

  • outer - 执行包含查询,以匹配来自big的术语,而这些术语至少包含一个来自little的术语

以下示例查询使用span.contains查找词语train与词语greatrobbery一起出现的文档,其中greatrobbery 5 title字段。

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" }
]

Atlas Search 返回在词语greatrobbery (使用big指定)内包含词语train (使用little指定)的文档。 如果您将第spanToReturn 行上的6 设置为inner ,Atlas Search 会返回相同的文档,因为词语train (使用little 指定)出现 词语greatrobbery (使用big 指定)中。 )。

first 位置操作符使用指定的数字标识Atlas Search词语的位置。 您可以使用Atlas Search 位置运算符 以递归方式指定 词语,也可以仅使用 词语 运算符。span 匹配Atlas Search词的位置小于或等于指定数字的文档。

first位置操作符采用以下语法:

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

first位置操作符采用以下字段:

选项
类型
必需?
说明
endPositionLte
int
没有
指定Atlas Search词语位置的数字。 如果为多个词语指定Atlas Search ,则最后一个词语应小于或等于此值。 如果省略,则默认为3
operator
文档
包含位置操作符术语操作符选项的文档。
score
文档
没有
要应用于此Atlas Search结果的分数。

以下示例查询使用 span.first 来查找 title 字段中出现指定string的文档。 endPositionLte 参数的值为 2,这意味着使用 term 操作符指定的Atlas Search词语必须是该字段中的第一个或第二个单词。

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' }
]

Atlas Search 返回在title字段的第一个或第二个位置包含搜索词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' }
]

Atlas Search 返回在title字段的第一个或第二个位置包含搜索词manwoman的文档。 Atlas Search 不会在同一title中返回两个搜索词,因为该示例包含or操作符clauses来指定搜索词。

near 位置操作符匹配两个或多个包含彼此相邻的Atlas Search词的子句。 您可以使用Atlas Search 位置操作符 列表以递归方式指定 术语,也可以仅使用 术语 操作符。

near位置操作符采用以下语法:

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

near位置操作符采用以下字段:

字段
类型
必需?
说明
clauses
文档数组
必须彼此靠近的 Span 子句。 子句不能为空。 每个文档包含span位置或仅术语操作符选项。
inOrder
布尔
没有

该标志指定Atlas Search是否必须按指定的顺序搜索子句中的术语并且不能重叠。

值可以是以下值之一:

  • true — Atlas Search按指定顺序搜索子句中的术语,而不会重叠

  • false - Atlas Search以任意顺序搜索子句中的术语

如果省略,则默认值为 false

score
文档
没有
要应用于此Atlas Search结果的分数。
slop
整型
没有
子句中术语之间的允许距离。 较低的值允许词语之间的位置距离较小,较大的值允许词语之间的距离较大,以满足查询要求。 默认值为0 ,这意味着不同子句中的单词必须相邻才能被视为匹配。

以下示例查询使用 span.near Atlas Search字符串 princepauper 彼此接近的文档。 inOrder 参数设置为 false,因此Atlas Search词语可以按任意顺序排列。 slop 参数设置为 4,因此Atlas Search术语最多只能用 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' } ]

Atlas Search在 title 字段中返回包含Atlas Search单词 princepauper 的文档,这些单词之间的间隔少于四个单词。

or位置操作符匹配两个或多个子句中的任何一个。 您可以使用Atlas Search 位置操作符 列表以递归方式指定 术语,也可以仅使用 术语 操作符。

or位置操作符采用以下语法:

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

or位置操作符采用以下字段:

选项
类型
必需?
说明
clauses
文档数组
指定Atlas Search词的 Span 子句。 其中一个子句必须匹配,并且子句不能为空。 每个文档必须包含以递归方式指定的span位置操作符或仅包含术语操作符选项。
score
文档
没有
要应用于此Atlas Search结果的分数。

以下示例查询使用span.or Atlas Searchtitle子句指定 字段包含city 或 的文档的两个 词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' }
]

Atlas Search返回在 title 字段中包含Atlas Search词 citycountry 的文档,但不会在同一 title 中包含这两个词。

subtract位置操作符会删除与其他匹配项重叠的匹配项。 您可以使用Atlas Search 位置操作符 列表以递归方式指定 术语,也可以仅使用 术语 操作符。subtract 子句可用于从Atlas Search结果中排除某些字符串。

subtract位置操作符采用以下语法:

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

subtract位置操作符采用以下字段:

选项
类型
必需?
说明
exclude
文档
指定术语或短语匹配项的文档,以删除与include字段中指定的术语或短语匹配项重叠的部分。 您可以使用任何跨度位置操作符术语操作符来指定术语或短语。
include
文档
用于指定词语匹配的文档,包括使用任何位置操作符或仅使用词语运算符。
score
文档
没有
要应用于此Atlas Search结果的分数。

以下示例查询使用 span.subtract Atlas Search以下文档: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' }
]

Atlas Search 不会返回标题为Like Father Like Son的文档,因为尽管title字段包含单词fatherson ,但它们之间还有like ,位于以下内容的exclude条件中:查询。

后退

正则表达式(Regex)

来年

文本