MongoDB Atlas full-text Search : assign score for one field based on value in another field

I am using MongoDB atlas search. How can I assign a score for one field based on the value in another field? In the below example I have an array of objects called tags with two properties - name and tag_category. The data structure is like this in MongoDB.
{
“_id”:ObjectId(“6094e1dd0ac22244fd1cd5c3”),

        "tags":[ 
        { 
        "name":"India", 
        "tag_id":"6094e1bb72bff620f43b0f77", 
        "tag_category":"region" 
        },
         { 
        "name":"something else", 
        "tagid":"aaaae1bb72bff620f43b0faa", 
        "tag_category":"general" 
        }
            ]
    }

I want to have a score of 15 for tags.name when tags.tag_category is “region” and score of 5 when tags.tag_category is “general” when query matches search result.
How can I achieve this? Any help is appreciated

Hey @Thomas_George1 . Welcome to the MongoDB community. Your question is a good one and what you are looking for is constant scores.

The query would look something like this:

db.movies.aggregate([
  {
    $search: {
      "text": {
        "query": "region",
        "path": "tag_category",
        "score": { "constant": { "value": 5 } }
      }
    }
  },
  {
    $project: {
      "_id": 0,
      "title": 1,
      "score": { "$meta": "searchScore" }
    }
  }
])

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.