jeroen / mongolite

Fast and Simple MongoDB Client for R
https://jeroen.github.io/mongolite/
284 stars 64 forks source link

How to create text index with weights? #201

Open Kvit opened 4 years ago

Kvit commented 4 years ago

MongoDB command for creating an index with weights includes two parts one with field names , another with weights.

db.blog.createIndex(
   {
     content: "text",
     keywords: "text",
     about: "text"
   },
   {
     weights: {
       content: 10,
       keywords: 5
     },
     name: "TextIndex"
   }
 )

How can such index be created via mongolite? The problem is that I cannot find a way to coerce both parts of the command to acceptable valid json object to pass into db$index(add='') method. Any thoughts?

Leojmartin commented 4 years ago

You could solve it?

Kvit commented 4 years ago

I could not find a way

Leojmartin commented 4 years ago

I found a way to make artificial weigths, using the $cond, the problem is that the word need to match identically. If exists someway to use a sensible case it will be better, less restricted. Do you know a way?

cars$aggregate('[ { "$match": { "$text": { "$search": "ford" } }}, { "$project": { "Company": 1, "Model" : 1, "Category" : 1, "_id" : 0, "score": { "$add": [ { "$meta": "textScore" }, { "$cond": [ { "$eq": [ "$Company", "Ford" ] }, 10, 0]}
] } }}, { "$sort": { "score": -1 } } ]')