【问题标题】:Calculate relevant result on full text search in mongodb在mongodb中计算全文搜索的相关结果
【发布时间】:2017-09-08 19:29:43
【问题描述】:

我正在尝试从 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 }

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

分数是由谁计算的?

【问题讨论】:

    标签: mongodb full-text-search


    【解决方案1】:

    评分算法是 MongoDB 内部的,并且可能会随着时间的推移而改变,因此精确值无关紧要。如果需要,您可以尝试通过查看sources 来了解发生了什么(尽管我不建议这样做)。

    最终分数取决于您的搜索词(或者更确切地说是词干)的出现次数、匹配之间的距离、匹配质量(完全匹配与部分匹配)、语言设置和权重,您可以 @987654322 @。这些都是非常重要的东西,不容易记录在案。但是,有一篇博文很好地解释了某些方面: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
        }
    })
    

    【讨论】:

      猜你喜欢
      • 2014-02-18
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多