Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 获取文档特定字段的分数_Mongodb_Search - Fatal编程技术网

Mongodb 获取文档特定字段的分数

Mongodb 获取文档特定字段的分数,mongodb,search,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 i

我在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.TITLE
POST.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
}

它为每个文档提供
分数
。但我想得到这个
分数
字段。我的意思是,当我搜索任何东西(例如rat)时,我希望分别获得每个标题和正文的分数,而不是整个文档的分数。尝试了很多方法,但都没有成功。

我对文本搜索功能的理解是,分数在文档级别。由于您是针对索引的两个字段进行搜索,因此该分数实际上是搜索这两个字段的综合分数。我认为没有办法在每个领域都得分。我再次查看了mongo文档,以防有什么变化,但它也表示“分数”是每个文档的分数


在查看模型时,您似乎有两个博客文档,每个博客文档中都有多个博客帖子。如果你想在你的搜索中找到特定的博客文章,你可以考虑在你有一个博客文章集合的情况下对它进行不同的建模,你可以搜索它来给你每个博客帖子的分数。

我也在文档中看到了。它正式声明分数为dcument。所以,他们根本没有办法让这一切正常运转。不管它是否复杂。我不能尝试使用
foreach()
循环
$meta
来获取每个字段的分数。不,每个文档的分数在文档级别。。。不是现场级别。根据我的回答,您需要以不同的方式对此进行建模,以获得每个帖子/标题组合的分数。