代码之家  ›  专栏  ›  技术社区  ›  stalin

在mongodb中计算全文搜索的相关结果

  •  2
  • stalin  · 技术社区  · 8 年前

    我正试图从mongo那里得到更相关的结果,比如说我有这个收藏

    { "text" : "mitsubishi lancer 2011"}
    { "text" : "mitsubishi lancer 2011"}
    { "text" : "mitsubishi lancer 2011 in good conditions"}
    { "text" : "lancer 2011"}
    { "text" : "mitsubishi lancer 2014"}
    { "text" : "lancer 2016"}
    

    db.post.find({$text: {$search: "mitsubishi lancer 2011"}}, {score: {$meta: "textScore"}}).sort({score:{$meta:"textScore"}})
    

    我得到这个结果

    { "text" : "mitsubishi lancer 2011", "score" : 2 }
    { "text" : "mitsubishi lancer 2011", "score" : 2 }
    { "text" : "mitsubishi lancer 2011 in good conditions", "score" : 1.7999999999999998 }
    { "text" : "lancer 2011", "score" : 1.5 }
    { "text" : "mitsubishi lancer 2014", "score" : 1.3333333333333333 }
    { "text" : "lancer 2016", "score" : 0.75 }
    

    我怎么知道前两个有我搜索的所有文本?

    1 回复  |  直到 8 年前
        1
  •  2
  •   dnickless    8 年前

    评分算法是MongoDB内部的,可能会随着时间的推移而变化,因此精确的值应该无关紧要。您可以通过查看 sources

    最终分数取决于搜索词(或词干)的出现次数、匹配之间的距离、匹配质量(完全匹配与部分匹配)、语言设置和权重 configure . 这些都是很重要的东西,很难记录下来。然而,有一篇博客文章很好地解释了一些方面: https://blog.codecentric.de/en/2013/01/text-search-mongodb-stemming/

    最后,如果你想知道是否有一个完美的匹配,我能想到的唯一方法是这样做:

    db.getCollection('test').aggregate(
    {
        // do the normal filtering query
        $match: {
            $text: {
                $search: "mitsubishi lancer 2011"
            }
        }
    }, {
        // select what's relevant in the output and add an indicator "perfectmatch"
        $project: {
            "text": 1,
            "score": {
                $meta: "textScore"
            },
            "perfectmatch": {
                $cond: [
                    { $eq: [ "$text", "mitsubishi lancer 2011" ] }, // this would check for a perfect match using the exact full string, for individual token matching you would need to do tokenize your query and do a series of other checks here.
                    true,
                    false
                ]
            }
        }
    }, {
        // if you want to have the results sorted by "best match first"
        $sort: {
            "score": -1
        }
    })