.Net连接MongoDb:文本字段索引
阿里云服务器,每月低至7.8元,项目演示即建站必备,比腾讯云更便宜,并且不需学生认证,从此链接购买有效:去购买
介绍在上一篇文章中,我们看了解函数生成的查询计划的各个方面。 有三种查询计划模式对应于越来越多的细节:查询计划模式,执行统计模式和所有计划执行模式。 使用查询计划模式,我们可以快速验证MongoDb将使用哪个策略来执行查询。 执行统计数据还会告知在每个步骤中扫描和生成的文档和索引键的数量。 最后,所有计划执行模式显示MongoDb如何通过生成任何被拒绝的计划的细节来选择某个执行路径。 在这篇文章中,我们将介绍如何索引文本字段。 文本搜索有时候,MongoDb文档可以有一个文本字段,如下例所示: db.texts.insert({"news" : "Decisively surrounded all admiration and not you."}) db.texts.insert({"news" : "Out particular sympathize not favourable introduced insipidity but ham."}) db.texts.insert({"news" : "Evil true high lady roof men had open."}) db.texts.insert({"news" : "surrounded all admiration"}) db.texts.insert({"news" : "sympathize not favourable introduced insipidity admiration and not you lady roof men had"}) db.texts.insert({"news" : "Decisively surrounded all not favourable introduced."}) db.texts.insert({"news" : "Knew as miss my high hope quit true high lady roof men had"}) db.texts.insert({"news" : "particular sympathize not favourable admiration and not you lady roof all not favourable"}) db.texts.insert({"news" : "insipidity but ham admiration surrounded all not favourable"}) db.texts.insert({"news" : "sympathize not favourable admiration and not sympathize not favourable introduced insipidity"}) db.texts.insert({"news" : "Decisively surrounded all not favourable but ham admiration surrounded all not"}) db.texts.insert({"news" : "as miss my high hope quit true high surrounded all not favourable"}) 这是本网站生成的一些随机的英文文本:随机文本生成器。 我们来看看我们是否可以使用简单的搜索找到一些文本: db.texts.find({"news" : "high hope"}) 不,没有找到任何东西,因为这是一个平等的行动,我们没有任何文字等于“高希望”。 我们可以使用正则表达式搜索,但我们将看另一个选项,即文本索引。 文本索引将为文本中的单词创建一个索引,类似于数组索引的工作原理。 通常当我们创建索引时,我们必须提供索引属性的名称,然后提供1或-1,无论是升序还是降序。 使用文本索引,它稍有不同,因为我们不提供整数参数,而是提供一个特殊的“text”关键字: db.texts.createIndex({"news" : "text"}) 文本搜索语法也将不同。 这是我们如何使用$ text和$ search运算符重写上述find命令: db.texts.find({$text : {$search : "high hope"}}) 它返回3个文件: { "_id" : ObjectId("574a71613935a48996923e45"), "news" : "as miss my high hope quit true high surrounded all not favourable" } { "_id" : ObjectId("574a71603935a48996923e40"), "news" : "Knew as miss my high hope quit true high lady roof men had" } { "_id" : ObjectId("574a71603935a48996923e3c"), "news" : "Evil true high lady roof men had open." } 按照文本符合搜索字词的顺序,返回文档。 看到第三个文件“高”,但没有“希望”,所以最后。 搜索是非常灵活的。 例如。 搜索词“希望高”也会产生上述文件,所以不仅要按照一定的顺序搜索单词。 请注意,套管没有任何区别。 搜索…: db.texts.find({$text : {$search : "HOPE HIGH"}}) ...返回相同的文档集。 另一个有趣的功能是,我们可以要求MongoDb产生文档与搜索词匹配的程度。 $ meta运算符生成一个包含字段“textScore”的元文档。 文字分数为双倍,分数越高越好比赛: db.texts.find({$text : {$search : "insipidity roof introduced"}}, {"degree" : {$meta : "textScore"}}).sort({"degree" : {$meta : "textScore"}}) 上述命令生成以下文档: { "_id" : ObjectId("574a71603935a48996923e3e"), "news" : "sympathize not favourable introduced insipidity admiration and not you lady roof men had", "degree" : 1.6875 } { "_id" : ObjectId("574a71603935a48996923e3b"), "news" : "Out particular sympathize not favourable introduced insipidity but ham.", "degree" : 1.1666666666666667 } { "_id" : ObjectId("574a71603935a48996923e43"), "news" : "sympathize not favourable admiration and not sympathize not favourable introduced insipidity", "degree" : 1.1428571428571428 } { "_id" : ObjectId("574a71603935a48996923e3f"), "news" : "Decisively surrounded all not favourable introduced.", "degree" : 0.625 } { "_id" : ObjectId("574a71603935a48996923e42"), "news" : "insipidity but ham admiration surrounded all not favourable", "degree" : 0.6 } { "_id" : ObjectId("574a71603935a48996923e3c"), "news" : "Evil true high lady roof men had open.", "degree" : 0.5714285714285714 } { "_id" : ObjectId("574a71603935a48996923e41"), "news" : "particular sympathize not favourable admiration and not you lady roof all not favourable", "degree" : 0.5714285714285714 } { "_id" : ObjectId("574a71603935a48996923e40"), "news" : "Knew as miss my high hope quit true high lady roof men had", "degree" : 0.55 } 此功能可帮助您构建一个搜索框,帮助用户在打字时完成查询。 阿里云服务器,每月低至7.8元,项目演示即建站必备,比腾讯云更便宜,并且不需学生认证,从此链接购买有效: 去购买
版权声明:本站所有教程均为本站原创或翻译,转载请注明出处,请尊重他人劳动果实。请记住本站地址:www.yuanjiaocheng.net (猿教程) 作者:卿文刚
本文标题: C#环境 本文地址:http://www.yuanjiaocheng.net/CsharpMongo/37.html |