Atlas搜索
概述
在这份指南中,您可以了解如何使用搜索
构建器使用MongoDB .NET/C#驱动程序构建$search
聚合管道阶段。
要了解更多关于$search
聚合管道阶段的信息,请参阅$search.
注意
仅在MongoDB v4.2及以上版本的Atlas上可用
$search
聚合管道操作符仅适用于托管在MongoDB Atlas集群上的集合,这些集群运行MongoDB v4.2或更高版本,并且受Atlas搜索索引的保护。要了解更多关于所需设置和此操作符的功能,请参阅Atlas Search文档。
本指南中的示例使用名为guitars
的集合中的以下文档。
{ "_id": 1, "make": "Fender", "description": "Classic guitars known for their versatility.", "establishedYear": 1946, "in_stock": true, "rating": 9 } { "_id": 2, "make": "Gibson", "description": "Classic guitars known for their rich, full tones.", "establishedYear": 1902, "in_stock": true, "rating": 8 } { "_id": 3, "make": "PRS", "description": "High-end guitars known for their quality.", "establishedYear": 1985, "in_stock": true, "rating": 9 } { "_id": 4, "make": "Kiesel", "description": "Quality guitars made only for custom orders.", "establishedYear": 2015, "in_stock": false } { "_id": 5, "make": "Ibanez", "description": "Well-crafted guitars used by many professional guitarists.", "establishedYear": 1957, "in_stock": true, "rating": 7 } { "_id": 6, "make": "Strandberg", "description": "Modern guitars known for their headless models.", "establishedYear": 1982, "in_stock": false }
以下Guitar
类表示该集合中的文档。
public class Guitar { public int Id { get; set; } public string Make { get; set; } public List<string> Models { get; set; } public int EstablishedYear { get; set; } [ ] public bool InStock { get; set; } public int? Rating { get; set; } }
注意
guitars
集合中的文档使用驼峰命名约定。本指南中的示例使用ConventionPack
将集合中的字段反序列化为Pascal大小写,并将它们映射到Guitar
类的属性。
要了解更多关于自定义序列化的信息,请参阅自定义序列化.
创建Atlas搜索索引
在您可以对Atlas集合进行搜索之前,您必须首先在该集合上创建一个Atlas搜索索引。Atlas搜索索引是一种将数据分类为可搜索格式的数据结构。
有关创建Atlas搜索索引的说明,请参阅创建Atlas搜索索引 Atlas指南。
Atlas搜索操作符
Search
类包含您可以使用的方法来执行$search
操作。有关可用的$search
操作符的完整列表,请参阅操作符和收集器 Atlas指南。
自动完成
使用Autocomplete()
方法搜索包含来自不完整输入字符串的字符序列的单词或短语。
以下示例使用字符串 "Gib" 在 make
字段上对 guitars
集合进行自动完成搜索。
var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.Autocomplete(g => g.Make, "Gib"), indexName: "guitarmakes") .ToList();
注意
如果您正在搜索的字段由搜索索引索引,您必须将索引名称传递给 Autocomplete
调用。如果不存在搜索索引,则使用默认索引。
搜索返回以下文档
{ "_id" : 2, "make" : "Gibson", "description" : "Classic guitars known for their rich, full tones.", "establishedYear" : 1902, "in_stock" : true, "rating" : 8 }
有关 autocomplete
操作符的更多信息,请参阅 autocomplete Atlas指南。
复合
使用 Compound()
方法将两个或多个操作符组合成单个搜索。
以下示例在 guitars
集合中搜索符合以下所有条件的文档
文档上存在
rating
字段in_stock
字段不是false
establishedYear
字段具有大于 1940 的值
var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.Compound() .Must(Builders<Guitar>.Search.Exists(g => g.Rating)) .MustNot(Builders<Guitar>.Search.Equals(g => g.InStock, false)) .Must(Builders<Guitar>.Search.Range(g => g.EstablishedYear, SearchRangeBuilder.Gt(1940)))) .ToList();
搜索返回以下文档
{ "_id" : 1, "make" : "Fender", "description" : "...", "establishedYear" : 1946, "in_stock" : true, "rating" : 9 } { "_id" : 3, "make" : "PRS", "description" : "...", "establishedYear" : 1985, "in_stock" : true, "rating" : 9 } { "_id" : 5, "make" : "Ibanez", "description" : "...", "establishedYear" : 1957, "in_stock" : true, "rating" : 7 }
有关 compound
操作符的更多信息,请参阅 compound Atlas指南。
内嵌文档
使用 EmbeddedDocument()
方法对字段数组值中的文档执行搜索操作。
注意
要搜索内嵌文档,必须在数组字段上创建 embeddedDocument
索引。
要了解如何定义一个嵌入式文档
索引,请参阅Atlas文档中的定义嵌入式文档类型的索引。
考虑到guitars
集合中的一些文档添加了包含产品详情对象数组的productDetails
字段。
{ "_id": 1, "make": "Fender", "description": "...", "establishedYear": 1946, "in_stock": true, "rating": 9, "productDetails": [{"product_id": 1234, "serial": "YZ5678"}] } { "_id": 2, "make": "Gibson", "description": "...", "establishedYear": 1902, "in_stock": true, "rating": 8 } { "_id": 3, "make": "PRS", "description": "...", "establishedYear": 1985, "in_stock": true, "rating": 9, "productDetails": [{"product_id": 9870, "serial": "AB5555"}] } { "_id": 4, "make": "Kiesel", "description": "...", "establishedYear": 2015, "in_stock": false } { "_id": 5, "make": "Ibanez", "description": "...", "establishedYear": 1957, "in_stock": true, "rating": 7, "productDetails": [{"product_id": 5432, "serial": "ZZ1234"}] } { "_id": 6, "make": "Strandberg", "description": "...", "establishedYear": 1982, "in_stock": false }
在productDetails
字段上创建嵌入式文档索引后,您可以对该字段中的文档执行Atlas搜索操作。以下示例在productDetails
数组字段上执行文本搜索,并返回任何具有serial
字段值为"YZ5678"
的文档。
var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.EmbeddedDocument( g => g.ProductDetails, Builders<ProductDetails>.Search.Text(p => p.Serial, "YZ5678") )).ToList(); return result;
搜索返回以下文档
{ "_id" : 1, "make" : "Fender", "description" : "Classic guitars known for their versatility.", "establishedYear" : 1946, "in_stock" : true, "rating" : 9, "productDetails" : [{ "product_id" : 1234, "serial" : "YZ5678" }] }
要了解有关嵌入式文档
运算符的更多信息,请参阅Atlas指南中的嵌入式文档。
等于
使用Equals()
方法检查字段是否匹配指定值。
以下示例在guitars
集合中搜索任何in_stock
字段值为true
的文档。
var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.Equals(g => g.InStock, true)) .ToList();
搜索返回以下文档
{ "_id" : 1, "make" : "Fender", "description" : "...", "establishedYear" : 1946, "in_stock" : true, "rating" : 9 } { "_id" : 2, "make" : "Gibson", "description" : "...", "establishedYear" : 1902, "in_stock" : true, "rating" : 8 } { "_id" : 3, "make" : "PRS", "description" : "...", "establishedYear" : 1985, "in_stock" : true, "rating" : 9 } { "_id" : 5, "make" : "Ibanez", "description" : "...", "establishedYear" : 1957, "in_stock" : true, "rating" : 7 }
要了解有关equals
运算符的更多信息,请参阅Atlas指南中的equals。
存在
使用Exists()
方法搜索具有指定索引字段名的文档。如果指定的字段存在但未索引,则该文档不包括在结果集中。
以下示例在guitars
集合中搜索任何存在rating
字段的文档。
var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.Exists(g => g.Rating)) .ToList();
搜索返回以下文档
{ "_id" : 1, "make" : "Fender", "description" : "...", "establishedYear" : 1946, "in_stock" : true, "rating" : 9 } { "_id" : 2, "make" : "Gibson", "description" : "...", "establishedYear" : 1902, "in_stock" : true, "rating" : 8 } { "_id" : 3, "make" : "PRS", "description" : "...", "establishedYear" : 1985, "in_stock" : true, "rating" : 9 } { "_id" : 5, "make" : "Ibanez", "description" : "...", "establishedYear" : 1957, "in_stock" : true, "rating" : 7 }
要了解有关exists
运算符的更多信息,请参阅Atlas指南中的exists。
GeoShape
使用 GeoShape()
方法来搜索与给定几何形状相关的文档。在指定搜索坐标时,必须首先指定经度,然后是纬度。经度值可以是 -180
到 180
,纬度值可以是 -90
到 90
。
注意
Atlas Search 不支持以下内容
非默认坐标系(CRS)
平面 XY 坐标系(二维)
坐标对点表示法(pointFieldName: [12, 34])
假设在 guitars
集合中的一些文档添加了 in_stock_location
字段。集合中更改后的文档现在如下所示
{ "_id": 1, "make": "Fender", "description": "...", "establishedYear": 1946, "in_stock": true, "in_stock_location": { "type": "Point", "coordinates": [ -73.93615, 40.69791 ]}, "rating": 9 } { "_id": 2, "make": "Gibson", "description": "...", "establishedYear": 1902, "in_stock": true, "in_stock_location": { "type": "Point", "coordinates": [ 47.6062, 122.321 ]}, "rating": 8 }
以下示例搜索所有在 in_stock_location
字段中的坐标与指定的多边形相交的文档
GeoJsonPolygon<GeoJson2DGeographicCoordinates> searchArea = new(new(new(new GeoJson2DGeographicCoordinates[] { new(-72.93615, 41.69791), new(-72.93615, 40.59791), new(-74.93615, 40.59791), new(-74.93615, 41.69791), new(-72.93615, 41.69791), }))); var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.GeoShape(g => g.InStockLocation, GeoShapeRelation.Intersects, searchArea)) .ToList();
搜索返回以下文档
{ "_id" : 1, "make" : "Fender", "description" : "...", "establishedYear" : 1946, "in_stock" : true, "in_stock_location" : { "type" : "Point", "coordinates" : ["-73.93615", "40.69791"] }, "rating" : 9 }
有关 geoShape
操作符的更多信息,请参阅 geoShape Atlas 指南。
GeoWithin
使用 GeoWithin()
方法来搜索坐标在给定几何形状内的指定 GeoJSON 字段中的文档。您可以搜索在以下范围内的点
圆形
边界框
多边形
在指定搜索坐标时,必须首先指定经度,然后是纬度。经度值可以是 -180
到 180
,纬度值可以是 -90
到 90
。
注意
Atlas Search 不支持以下内容
非默认坐标系(CRS)
平面 XY 坐标系(二维)
坐标对点表示法(pointFieldName: [12, 34])
假设在 guitars
集合中的一些文档添加了 in_stock_location
字段。集合中更改后的文档现在如下所示
{ "_id": 1, "make": "Fender", "description": "...", "establishedYear": 1946, "in_stock": true, "in_stock_location": { "type": "Point", "coordinates": [ -73.93615, 40.69791 ]}, "rating": 9 } { "_id": 2, "make": "Gibson", "description": "...", "establishedYear": 1902, "in_stock": true, "in_stock_location": { "type": "Point", "coordinates": [ 47.6062, 122.321 ]}, "rating": 8 }
以下示例搜索所有在 in_stock_location
字段中的坐标位于指定多边形内的文档
GeoJsonPolygon<GeoJson2DGeographicCoordinates> searchArea = new(new(new(new GeoJson2DGeographicCoordinates[] { new(-74.3994140625, 40.5305017757), new(-74.7290039063, 40.5805846641), new(-74.7729492188, 40.9467136651), new(-74.0698242188, 41.1290213475), new(-73.65234375, 40.9964840144), new(-72.6416015625, 40.9467136651), new(-72.3559570313, 40.7971774152), new(-74.3994140625, 40.5305017757), }))); var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.GeoWithin(g => g.InStockLocation, searchArea)) .ToList();
搜索返回以下文档
{ "_id" : 1, "make" : "Fender", "description" : "Classic guitars known for their versatility.", "establishedYear" : 1946, "in_stock" : true, "in_stock_location" : { "type" : "Point", "coordinates" : ["-73.93615", "40.69791"] }, "rating" : 9 }
有关 geoWithin
操作符的更多信息,请参阅 geoWithin Atlas 指南。
更多类似
使用MoreLikeThis()
方法搜索与输入文档类似的文档。
以下示例搜索guitars
集合中,描述字段值为"高质量"的对象类似的文档。
var searchDocument = new GuitarSearch() { Description = "high quality", }; var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.MoreLikeThis(searchDocument)) .ToList();
搜索返回以下文档
{ "_id" : 3, "make" : "PRS", "description" : "High-end guitars known for their quality.", "establishedYear" : 1985, "in_stock" : true, "rating" : 9 } { "_id" : 4, "make" : "Kiesel", "description" : "Quality guitars made only for custom orders.", "establishedYear" : 2015, "in_stock" : false, "rating" : null }
有关moreLikeThis
运算符的更多信息,请参阅moreLikeThis Atlas指南。
接近
使用Near()
方法搜索指定字段值接近给定值的文档。您可以在以下字段上执行搜索:
数字字段
日期字段
地理点
以下示例搜索guitars
集合中,评分字段值接近9
的文档。文档根据其值接近9
的远近顺序返回。
var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.Near(g => g.Rating, 9, 1)) .ToList();
搜索返回以下文档
{ "_id" : 1, "make" : "Fender", "description" : "...", "establishedYear" : 1946, "in_stock" : true, "rating" : 9 } { "_id" : 3, "make" : "PRS", "description" : "...", "establishedYear" : 1985, "in_stock" : true, "rating" : 9 } { "_id" : 2, "make" : "Gibson", "description" : "...", "establishedYear" : 1902, "in_stock" : true, "rating" : 8 } { "_id" : 5, "make" : "Ibanez", "description" : "...", "establishedYear" : 1957, "in_stock" : true, "rating" : 7 }
有关near
运算符的更多信息,请参阅near Atlas指南。
短语
使用 Phrase()
方法来搜索包含指定字段和输入字符串的文档。
以下示例搜索 guitars
集合中,description
字段包含短语 "classic guitars" 的文档。
var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.Phrase(g => g.Description, "classic guitars")) .ToList();
搜索返回以下文档
{ "_id" : 1, "make" : "Fender", "description" : "Classic guitars known for their versatility.", "establishedYear" : 1946, "in_stock" : true, "rating" : 9 } { "_id" : 2, "make" : "Gibson", "description" : "Classic guitars known for their rich, full tones.", "establishedYear" : 1902, "in_stock" : true, "rating" : 8 }
您还可以按照以下方式搜索匹配多个单独短语的集合中的文档
var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.Phrase(g => g.Description, new List<string>() { "classic guitars", "quality guitars" })) .ToList();
此搜索返回以下文档
{ "_id" : 1, "make" : "Fender", "description" : "Classic guitars known for their versatility.", "establishedYear" : 1946, "in_stock" : true, "rating" : 9 } { "_id" : 4, "make" : "Kiesel", "description" : "Quality guitars made only for custom orders.", "establishedYear" : 2015, "in_stock" : false, "rating" : null } { "_id" : 2, "make" : "Gibson", "description" : "Classic guitars known for their rich, full tones.", "establishedYear" : 1902, "in_stock" : true, "rating" : 8 }
要了解有关 phrase
操作符的更多信息,请参阅phrase Atlas指南。
查询字符串
使用 QueryString()
方法通过以下运算符和分隔符进行字符串搜索
AND
OR
NOT
()
以下示例搜索 guitars
集合中,description
字段的值满足以下每个条件的文档
包含字符串 "classic" 或字符串 "quality" 或
不包含字符串 "custom" 或
var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.QueryString(g => g.Description, "(classic OR quality) AND NOT custom")) .ToList();
搜索返回以下文档
{ "_id" : 1, "make" : "Fender", "description" : "Classic guitars known for their versatility.", "establishedYear" : 1946, "in_stock" : true, "rating" : 9 } { "_id" : 3, "make" : "PRS", "description" : "High-end guitars known for their quality.", "establishedYear" : 1985, "in_stock" : true, "rating" : 9 } { "_id" : 2, "make" : "Gibson", "description" : "Classic guitars known for their rich, full tones.", "establishedYear" : 1902, "in_stock" : true, "rating" : 8 }
要了解有关 queryString
操作符的更多信息,请参阅queryString Atlas指南。
范围
使用 Range()
方法搜索指定字段的值在给定数字或日期范围内的文档。
以下示例搜索 guitars
集合中所有 establishedYear
值大于 1980 且小于 2020 的文档。
var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.Range(g => g.EstablishedYear, SearchRangeBuilder.Gt(1980).Lt(2020))) .ToList();
搜索返回以下结果
{ "_id" : 3, "make" : "PRS", "description" : "High-end guitars known for their quality.", "establishedYear" : 1985, "in_stock" : true, "rating" : 9 } { "_id" : 4, "make" : "Kiesel", "description" : "Quality guitars made only for custom orders.", "establishedYear" : 2015, "in_stock" : false, "rating" : null } { "_id" : 6, "make" : "Strandberg", "description" : "Modern guitars known for their headless models.", "establishedYear" : 1982, "in_stock" : false, "rating" : null }
要了解有关 range
操作符的更多信息,请参阅range Atlas指南。
正则表达式
使用Regex()
方法使用正则表达式搜索文档。
以下示例搜索guitars
集合中,make
字段的值恰好包含六个字母的文档。
var regex = "[A-Za-z]{6}"; var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.Regex(g => g.Make, regex)) .ToList();
搜索返回以下结果
{ "_id" : 1, "make" : "Fender", "description" : "Classic guitars known for their versatility.", "establishedYear" : 1946, "in_stock" : true, "rating" : 9 } { "_id" : 2, "make" : "Gibson", "description" : "Classic guitars known for their rich, full tones.", "establishedYear" : 1902, "in_stock" : true, "rating" : 8 } { "_id" : 4, "make" : "Kiesel", "description" : "Quality guitars made only for custom orders.", "establishedYear" : 2015, "in_stock" : false, "rating" : null } { "_id" : 5, "make" : "Ibanez", "description" : "Well-crafted guitars used by many professional guitarists.", "establishedYear" : 1957, "in_stock" : true, "rating" : 7 }
注意
默认情况下,regex
运算符不能在分析字段上运行。您可以通过将allowAnalyzedField
选项设置为true来允许它在分析字段上运行,如下所示
var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.Regex(g => g.Make, regex, true)) .ToList();
将allowAnalyzedField
选项设置为true可能会导致意外的搜索结果。有关更多信息,请参阅regex行为。
有关regex
运算符的更多信息,请参阅regex Atlas指南。
跨度
使用Span()
方法在字段的区域中搜索文本搜索匹配项。您可以使用此方法查找彼此非常接近的字符串。
注意
span
运算符比其他运算符更复杂,因为查询必须跟踪位置信息。
以下示例搜索guitars
集合中,description
字段的值包含“guitars”和“quality”字符串,这两个字符串彼此之间在一个单词之内。
var searchTerms = new[] { Builders<Guitar>.SearchSpan.Term(g => g.Description, "guitars"), Builders<Guitar>.SearchSpan.Term(g => g.Description, "quality") }; var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.Span(Builders<Guitar>.SearchSpan.Near(searchTerms, 1))) .ToList();
搜索返回以下文档
{ "_id" : 4, "make" : "Kiesel", "description" : "Quality guitars made only for custom orders.", "establishedYear" : 2015, "in_stock" : false, "rating" : null }
尽管文档中包含_id: 3
的字符串“guitars”和“quality”,但它们被分隔在一个单词以上,因此搜索将此文档排除在结果之外。
有关span
运算符的更多信息,请参阅span Atlas指南。
文本
使用Text()
方法在文档中搜索给定的字符串或字符串数组。如果给定字符串中有多个术语,Atlas Search也会分别搜索字符串中的每个术语的匹配项。
以下示例搜索guitars
集合中,其中description
字段值包含字符串"used by professionals"的文档。
var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.Text(g => g.Description, "used by professional")) .ToList();
搜索返回以下文档
{ "_id" : 5, "make" : "Ibanez", "description" : "Well-crafted guitars used by many professional guitarists.", "establishedYear" : 1957, "in_stock" : true, "rating" : 7 }
提示
如果您的搜索字符串包含多个术语,该方法还会分别搜索字符串中的每个术语的匹配项。
有关text
运算符的更多信息,请参阅text Atlas指南。
通配符
使用Wildcard()
方法通过搜索字符串中的特殊字符来搜索文档,这些特殊字符可以匹配任何字符。您可以在搜索中使用以下字符
字符 | 描述 |
---|---|
| 匹配任何单个字符 |
| 匹配0个或多个字符 |
| 转义字符 |
以下示例搜索在make
字段值包含字符串"Strand"后跟任意字符的文档。
var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.Wildcard(g => g.Make, "Strand*")) .ToList();
搜索返回以下文档
{ "_id" : 6, "make" : "Strandberg", "description" : "Modern guitars known for their headless models.", "establishedYear" : 1982, "in_stock" : false, "rating" : null }
注意
默认情况下,wildcard
运算符不能在分析字段上运行。您可以通过将allowAnalyzedField
选项设置为true来允许它在不分析字段上运行,如下所示
var result = guitarsCollection.Aggregate() .Search(Builders<Guitar>.Search.Wildcard(g => g.Make, "Strand*", true)) .ToList();
将allowAnalyzedField
选项设置为true可能会导致意外的搜索结果。有关更多信息,请参阅通配符行为。
有关wildcard
运算符的更多信息,请参阅通配符 Atlas指南。