Mongo全文搜索,通过Ruby驱动程序进行评分

尼古拉斯

Mongo文档中,您可以执行以下操作:

db.articles.find(
   { $text: { $search: "cake" } },
   { score: { $meta: "textScore" } }
)

当我从mongo控制台运行它时,效果很好,但是我不知道如何通过Ruby驱动程序来做到这一点。

当我这样做时:

articles.find('$text': { '$search': 'cake' }, score: { '$meta': 'textScore' })

我懂了

Mongo::Error::OperationFailure: unknown operator: $meta (2)

当我做

articles.find({ '$text': { '$search': 'cake' } }, score: { '$meta': 'textScore' })

我得到结果,但是它不包含分数,并且日志消息也没有显示它正在使用score: { $meta': 'textScore' }

{"find"=>"articles", "filter"=>{"$text"=>{"$search"=>"cake"}}}

我想我只是不理解Ruby驱动程序和Mongo CLI如何将它们转换为Mongo查询。

我正在使用MongoDB版本v3.2.7和mongo gem版本2.2.5。

法卡特·米哈科(Farkhat Mikhalko)

让我们看一下mongo命令的结构

db.collection.find(
   <query>,
   { score: { $meta: "textScore" } }
)

我们看到该命令包含两部分(在本例中为find

  1. <query>
  2. options hash

mongo-driver中的命令结构与mongo非常相似。但是有些事情并不简单。

在mongo-driver中Mongo::Collection::View,检查(链接到source):

articles.find({ '$text': { '$search': 'cake' } }.class # => Mongo::Collection::View

因此,在分析代码之后,我发现您可以使用projectionoption,但这很棘手:

articles.find(
 { "$text" => { "$search" => "cake" } }, 
 projection: { 
   "score" => { "$meta" => "textScore" }, 
   some_field: 1
 })

我的测试是:

posts.find(
  { "$text" => { "$search" => "house" } }, 
  projection: { "score" => { "$meta" => "textScore" }, rooms: 1 }
)

结果是:

{"_id"=>BSON::ObjectId('53f5c8b54465764a82fb0000'), 
 "rooms"=>2.0, 
 "score"=>0.5004448398576512}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

MongoDB全文搜索C#驱动程序

来自分类Dev

指定字段值上的Mongo Ruby驱动程序#find()

来自分类Dev

指定字段值上的Mongo Ruby驱动程序#find()

来自分类Dev

无法通过mongo的官方node.js驱动程序进行身份验证,只能通过mongo shell进行身份验证。(MongoDB Cloud Manager副本部署)

来自分类Dev

MySQL全文搜索和评分

来自分类Dev

如何使用mongo ruby驱动程序查找与特定值数组匹配的条目

来自分类Dev

Mongo db c# 驱动程序 - 如何通过 id 加入集合?

来自分类Dev

我该如何搜索db.collection.find({foo:/ ^ bar $ / i}); 在mongo上,但使用Mongo Java驱动程序

来自分类Dev

我该如何搜索db.collection.find({foo:/ ^ bar $ / i}); 在mongo上,但使用Mongo Java驱动程序

来自分类Dev

如何通过OAuth通过CData ODBC驱动程序对Salesforce进行身份验证?

来自分类Dev

MongoDB Ruby驱动程序和mongoS

来自分类Dev

Selenium / Ruby未知驱动程序,Firefox

来自分类Dev

通过驱动程序ChromeOptions和DesiredCapabilities?

来自分类Dev

通过pnputil安装驱动程序

来自分类Dev

Mongo C#驱动程序2.0-在搜索中使用对象类型时结果为空

来自分类Dev

如何通过在 mongo C# 驱动程序中仅使用连接字符串来`ListCollections`?

来自分类Dev

通过姓,名和名字进行全文搜索

来自分类Dev

为什么我无法使用Ruby驱动程序获得Mongodb 2.6文本搜索分数?

来自分类Dev

如何使用Java驱动程序使用Mongo Bulk Update?

来自分类Dev

Mongo DB C#驱动程序2.0同步计数

来自分类Dev

Mongo在Java驱动程序中插入$ currentDate

来自分类Dev

使用Scala Mongo驱动程序序列化为对象?

来自分类Dev

使用Java驱动程序获取Mongo Instance的版本

来自分类Dev

无法构建32位Mongo DB C ++驱动程序

来自分类Dev

Mongo Java驱动程序不遵循限制方法

来自分类Dev

在AWS Elastic Beanstalk上安装php mongo驱动程序

来自分类Dev

使用Mongo驱动程序2升级IBsonSerializer

来自分类Dev

无法使用Scala驱动程序将数据插入mongo

来自分类Dev

mongo c驱动程序更新嵌套数组

Related 相关文章

  1. 1

    MongoDB全文搜索C#驱动程序

  2. 2

    指定字段值上的Mongo Ruby驱动程序#find()

  3. 3

    指定字段值上的Mongo Ruby驱动程序#find()

  4. 4

    无法通过mongo的官方node.js驱动程序进行身份验证,只能通过mongo shell进行身份验证。(MongoDB Cloud Manager副本部署)

  5. 5

    MySQL全文搜索和评分

  6. 6

    如何使用mongo ruby驱动程序查找与特定值数组匹配的条目

  7. 7

    Mongo db c# 驱动程序 - 如何通过 id 加入集合?

  8. 8

    我该如何搜索db.collection.find({foo:/ ^ bar $ / i}); 在mongo上,但使用Mongo Java驱动程序

  9. 9

    我该如何搜索db.collection.find({foo:/ ^ bar $ / i}); 在mongo上,但使用Mongo Java驱动程序

  10. 10

    如何通过OAuth通过CData ODBC驱动程序对Salesforce进行身份验证?

  11. 11

    MongoDB Ruby驱动程序和mongoS

  12. 12

    Selenium / Ruby未知驱动程序,Firefox

  13. 13

    通过驱动程序ChromeOptions和DesiredCapabilities?

  14. 14

    通过pnputil安装驱动程序

  15. 15

    Mongo C#驱动程序2.0-在搜索中使用对象类型时结果为空

  16. 16

    如何通过在 mongo C# 驱动程序中仅使用连接字符串来`ListCollections`?

  17. 17

    通过姓,名和名字进行全文搜索

  18. 18

    为什么我无法使用Ruby驱动程序获得Mongodb 2.6文本搜索分数?

  19. 19

    如何使用Java驱动程序使用Mongo Bulk Update?

  20. 20

    Mongo DB C#驱动程序2.0同步计数

  21. 21

    Mongo在Java驱动程序中插入$ currentDate

  22. 22

    使用Scala Mongo驱动程序序列化为对象?

  23. 23

    使用Java驱动程序获取Mongo Instance的版本

  24. 24

    无法构建32位Mongo DB C ++驱动程序

  25. 25

    Mongo Java驱动程序不遵循限制方法

  26. 26

    在AWS Elastic Beanstalk上安装php mongo驱动程序

  27. 27

    使用Mongo驱动程序2升级IBsonSerializer

  28. 28

    无法使用Scala驱动程序将数据插入mongo

  29. 29

    mongo c驱动程序更新嵌套数组

热门标签

归档