2017-09-08 5 views
0

でのフルテキスト検索に関連した結果は、私はこのコレクションに計算MongoDBの

{ "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 } 

最初の2つはすべて私が検索したテキストを持っていることをどのように知っていますか?

誰がスコアを計算したのですか?

答えて

1

スコアリングアルゴリズムはMongoDBの内部であり、正確な値が重要でないように、おそらく時間とともに変化すると予想されるはずです。必要に応じて、sourcesを見ることで、何が起こっているのかを理解しようとすることができます(私はそれをお勧めしませんが)。

最終的なスコアは、検索語句(語幹)の出現回数、試合間の距離、試合の品質(完全一致vs.部分)、言語設定、重みなどによって異なります。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 
    } 
}) 
関連する問題