构造查询路径
Overview
Atlas Search 操作符使用 path
参数来指定要搜索的一个或多个字段。它可能包含:
字符串
字符串数组
包含字符串和 multi 分析器规范组合的数组
注意
并非所有操作符都可以使用所有不同类型的路径。有关每个操作符支持的路径类型的详细信息,请参阅每个操作符的文档。
使用
若要仅搜索一个索引字段,请在 path
参数中使用带引号的字符串。以下示例搜索名为 description
的字段。
"path": "description"
要搜索多个索引字段,请在 path
参数中使用带引号字符串数组。与任何指定字段匹配的文档都包含在结果集中。 以下示例搜索 description
和 type
字段。
"path": [ "description", "type" ]
注意
multi
路径选项仅适用于字符串类型的字段。
如果索引定义包含一个具有多个分析器的字段,您可以指定使用哪个分析器。path
参数可以接受一个具有以下字段的对象:
字段 | 说明 |
---|---|
| 要搜索的字段名称。 |
| 在索引定义的 |
| 包含通配符
只有以下操作符接受通配符路径: 通配符路径也可用于突出显示。 |
以下索引定义中,名为 names
和 notes
的字段使用标准分析器。名为 comments
的字段使用 standard
作为默认分析器,同时还指定了名为 mySecondaryAnalyzer
的 multi
使用 lucene.whitespace 分析器。
{ "mappings": { "dynamic": false, "fields": { "names": { "type": "string", "analyzer": "lucene.standard" }, "notes": { "type": "string", "analyzer": "lucene.standard" }, "comments": { "type": "string", "analyzer": "lucene.standard", "multi": { "mySecondaryAnalyzer": { "analyzer": "lucene.whitespace", "type": "string" } } } } } }
以下 path
示例使用索引定义中名为 mySecondaryAnalyzer
的 multi
搜索 comments
字段。
"path": { "value": "comments", "multi": "mySecondaryAnalyzer" }
要搜索索引字段和具有多个分析器的字段的组合,请使用数组。以下示例使用默认分析器搜索 names
和 notes
字段,并使用索引定义中名为 mySecondaryAnalyzer
的 multi
搜索 comments
字段。
"path": [ "names", "notes", { "value": "comments", "multi": "mySecondaryAnalyzer" } ]
以下 path
示例搜索包含字母 n
后跟任何字符的所有字段,以及在索引定义中使用名为 mySecondaryAnalyzer
的 multi
的 comments
的字段。
"path": [{ "wildcard": "n*" }, { "value": "comments", "multi": "mySecondaryAnalyzer" }]
示例
以下示例使用了一个名为 cars
的集合,其中包含以下文档:
{ "_id" : 1, "type" : "sedan", "make" : "Toyota", "description" : "Blue four-door sedan, lots of trunk space. Three to four passengers." } { "_id" : 2, "type" : "coupe", "make" : "BMW", "description" : "Red two-door convertible, driver's-side airbag." } { "_id" : 3, "type" : "SUV", "make" : "Ford", "description" : "Black four-door SUV, three rows of seats." }
静态字段映射允许您指定如何索引和搜索集合中的各个字段。
cars
集合的索引定义如下:
{ "mappings": { "dynamic": false, "fields": { "make": { "type": "string", "analyzer": "lucene.standard" }, "description": { "type": "string", "analyzer": "lucene.standard", "multi": { "simpleAnalyzer": { "analyzer": "lucene.simple", "type": "string" } } } } } }
前面的索引定义指定 make
字段使用标准分析器进行索引。默认情况下,description
字段使用 standard
分析器,但也可以通过指定 simpleAnalyzer
和 multi
参数来使用简单分析器。
单字段搜索
以下示例在 make
字段中搜索字符串 Ford
:
db.cars.aggregate([ { $search: { "text": { "query": "Ford", "path": "make" } } } ])
上述示例返回具有 _id: 3
的文档。
多字段搜索
以下示例使用 path
参数中的一组字段在 make
或 description
字段中搜索字符串 blue
。
db.cars.aggregate([ { $search: { "text": { "query": "blue", "path": [ "make", "description" ] } } } ])
上述查询返回以下结果:
{ "_id" : 1, "type" : "sedan", "make" : "Toyota", "description" : "Blue four-door sedan, lots of trunk space. Three to four passengers." }
备用分析器搜索
简单分析器示例
下面的示例在索引定义中使用名为 simpleAnalyzer
的 multi
,它使用简单分析器。
查询在 description
字段中搜索字符串 driver
。
db.cars.aggregate([ { $search: { "text": { "query": "driver", "path": { "value": "description", "multi": "simpleAnalyzer" } } } } ])
上述查询返回以下结果:
{ "_id" : 2, "type" : "coupe", "make" : "BMW", "description" : "Red two-door convertible, driver's-side airbag." }
简单分析器将 driver's
side airbag
索引为 [driver s side airbag
],因此它与 driver
匹配。
相比之下,默认的标准分析器会将 driver's side airbag
索引为 [driver's side airbag
],因此会匹配 driver's
或 side
,但不会匹配 driver
。
空格分析器示例
假设 cars
集合的索引定义中的 multi
对象如下:
"multi": { "simpleAnalyzer": { "analyzer": "lucene.whitespace", "type": "string" } }
下面的示例在索引定义中使用名为 simpleAnalyzer
的 multi
,该索引定义使用空格分析器。
db.cars.aggregate([ { $search: { "text": { "query": "Three", "path": { "value": "description", "multi": "simpleAnalyzer" } } } } ])
上述查询返回以下结果:
{ "_id" : 1, "type" : "sedan", "make" : "Toyota", "description" : "Blue four-door sedan, lots of trunk space. Three to four passengers." }
对于以上针对词语 Three
的查询,Atlas Search 仅返回与词语 Three
匹配的文档,而不返回与 three
匹配的文档,因为空格分析器区分大小写。相比之下,默认的标准分析器不区分大小写,并按集合中列出的顺序返回与查询中的词语匹配的所有文档。
现在,考虑以下查询:
db.cars.aggregate([ { $search: { "compound": { "should": [ { "text": { "path": "description", "query": "Three" } }, { "text": { "query": "Three", "path": { "value" : "description", "multi" : "simpleAnalyzer" }, score: { boost: { value: 2 }} } } ] } } }, { $project: { "_id": 0, "type": 1, "description": 1, "score": { "$meta": "searchScore" } } } ])
上述查询返回以下结果:
{ "type" : "sedan", "description" : "Blue four-door sedan, lots of trunk space. Three to four passengers seats.", "score" : 1.1092689037322998 } { "type" : "SUV", "description" : "Black four-door SUV, three rows of seats.", "score" : 0.17812025547027588 }
对于上述查询,Atlas Search 返回同时包含Three
和three
的文档。但是,带有 Three
的结果分数更高,因为带有 three
的文档使用默认的标准分析器进行匹配,而带有 Three
的文档由指定的 simpleAnalyzer
和默认的标准分析器进行匹配。
以下示例使用一个名为 posts
的集合,其中包含以下文档:
{ "_id": 1, "username": "pinto", "post": { "date": "12-03-2018", "forum": "Tofu Recipes", "body": "Spicy Garlic Tofu cooks up crispy in 10 minutes or less. Serve with broccoli and rice for a delicious vegetarian meal." } } { "_id": 2, "username": "paloma", "post": { "date": "12-08-2018", "forum": "Tofu Recipes", "body": "Crispy Tofu in Shiitake Broth has flavors of citrus and umami. Great as an appetizer or entree." } }
动态字段映射允许您根据需要为集合中的所有字段建立索引。
posts
集合的索引定义如下:
{ "mappings": { "dynamic": true } }
嵌套字段搜索
以下复合查询在字段 post.body
中搜索字符串 broccoli
,并指定该字段不得包含字符串 cauliflower
。
db.posts.aggregate([ { $search: { "compound": { "must": { "text": { "query": "broccoli", "path": "post.body" } }, "mustNot": { "text": { "query": "cauliflower", "path": "post.body" } } } } } ])
前面的查询返回带有 _id: 1
的文档,其中 posts.body
字段包含字符串 broccoli
。
通配符字段搜索
以下示例使用一个名为 cars
的集合,其中具有以下文档:
{ "_id" : 1, "type" : "sedan", "make" : "Toyota", "description" : "Four-door sedan, lots of trunk space. Three to four passengers.", "warehouse" : [ { "inventory" : 3, "color" : "red" } ] } { "_id" : 2, "type" : "coupe", "make" : "BMW", "description" : "Two-door convertible, driver's-side airbag.", "warehouse" : [ { "inventory" : 5, "color" : "black" } ] } { "_id" : 3, "type" : "SUV", "make" : "Ford", "description" : "Four-door SUV, three rows of seats.", "warehouse" : [ { "inventory" : 7, "color" : "white" }, { "inventory" : 3, "color" : "red" } ] }
cars
集合的索引定义如下:
{ "mappings": { "dynamic": true } }
以下查询在使用通配符 *
指定的字段中搜索 red
字符串。
所有字段搜索示例
下面的查询会在所有字段中搜索字符串 red
。
db.cars.aggregate([ { "$search": { "phrase": { "path": { "wildcard": "*" }, "query": "red" } } } ])
查询返回以下结果:
{ "_id" : 1, "type" : "sedan", "make" : "Toyota", "description" : "Four-door sedan, lots of trunk space. Three to four passengers.", "warehouse" : [ { "inventory" : 3, "color" : "red" } ] } { "_id" : 3, "type" : "SUV", "make" : "Ford", "description" : "Four-door SUV, three rows of seats.", "warehouse" : [ { "inventory" : 7, "color" : "white" }, { "inventory" : 3, "color" : "red" } ] }
嵌套字段搜索示例
以下查询在 warehouse
字段的所有嵌套字段中搜索字符串 red
。
db.cars.aggregate([ { "$search": { "text": { "path": { "wildcard": "warehouse.*" }, "query": "red" } } } ])
查询返回以下结果:
{ "_id" : 1, "type" : "sedan", "make" : "Toyota", "description" : "Four-door sedan, lots of trunk space. Three to four passengers.", "warehouse" : [ { "inventory" : 3, "color" : "red" } ] } { "_id" : 3, "type" : "SUV", "make" : "Ford", "description" : "Four-door SUV, three rows of seats.", "warehouse" : [ { "inventory" : 7, "color" : "white" }, { "inventory" : 3, "color" : "red" } ] }