获取特定文档字段的分数

时间:2016-02-09 18:59:24

标签: mongodb search

我在mongoDB中有这个集合: -

{
"_id" : ObjectId("56ba281e60bf38bc0e53d592"),
"name" : "abc",
"POST" : [
    {
        "TITLE" : "Dog eats cat",
        "BODY" : "Dog is cat's enemy"
    },
    {
        "TITLE" : "Dog is close to humans",
        "BODY" : "Dog is the best friend man can have"
    }
]
}
{
"_id" : ObjectId("56ba28b960bf38bc0e53d593"),
"name" : "abc",
"POST" : [
    {
        "TITLE" : "Men are from mars",
        "BODY" : "NASA found rats on mars and venus."
    },
    {
        "TITLE" : "Big cat found",
        "BODY" : "Cat can be friendly if it is small."
    }
]
}
{
"_id" : ObjectId("56ba2a0c60bf38bc0e53d594"),
"name" : "abc",
"POST" : [
    {
        "TITLE" : "Cat eats rats",
        "BODY" : "Rat found dead on mars."
    },
    {
        "TITLE" : "Humans and rats are genetically close",
        "BODY" : "Humans test their new medicines on rats"
    }
]
}

我使用了POST.TITLEPOST.BODY创建了一个索引 db.a.createIndex({"POST.TITLE":"text","POST.BODY":"text"})

当我使用查询搜索集合a时: -

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

给了我这个: -

{
"_id" : ObjectId("56ba28b960bf38bc0e53d593"),
"name" : "abc",
"POST" : [
    {
        "TITLE" : "Men are from mars",
        "BODY" : "NASA found rats on mars and venus."
    },
    {
        "TITLE" : "Big cat found",
        "BODY" : "Cat can be friendly if it is small."
    }
],
"score" : 0.6
}
{
"_id" : ObjectId("56ba2a0c60bf38bc0e53d594"),
"name" : "abc",
"POST" : [
    {
        "TITLE" : "Cat eats rats",
        "BODY" : "Rat found dead on mars."
    },
    {
        "TITLE" : "Humans and rats are genetically close",
        "BODY" : "Humans test their new medicines on rats"
    }
],
"score" : 2.5166666666666666
}

它为每个文档提供score。但是我想在场上明白这个score。我的意思是,当我搜索任何东西(例如老鼠)时,我想分别得到每个TITLE和BODY的分数而不是整个文档的分数。试过很多方法,但无法得到它。

1 个答案:

答案 0 :(得分:0)

我对文本搜索功能的理解是得分在文档级别。由于您要搜索两个被索引的字段,因此该分数实际上是搜索两个字段的综合得分。我认为没有办法获得每场的得分。我再次查看了mongo文档,以防有些内容发生了变化,但它也说“得分”是每个文档。

在查看您的模型时,您看起来有两个博客文档,并且在每个博客文档中都有多个博客文章。如果您正在尝试查找在搜索中得分较高的特定博客帖子,您可以考虑使用不同的方式进行建模,如果您有可以搜索的博客文章集合,则可以为每个博客帖子提供分数。