I am working on a search feature. I have an object containing data in key value pair where key is an id of an object and value is a score that I have counted with some background process. So my case during search fora data I have to add the score counted before with the new score.
I am using aggregation for my search operation. Below is my code snippet.
Document
{
"_id": ObjectId("57de00eb0b1e50fa66290198"),
"id": "205332",
"Title": "The relationship between bispectral index values and age in children",
"title": "Bispectral index values during general anesthesia in children",
"OutcomesAvailable": "No",
"DateStart": "2011-03-10T00:00:00-06:00",
"DateEnd": {
"type": "actual",
"value": "2012-05-01T00:00:00-05:00"
},
"DateChangeLast": "2014-07-21T11:39:54-05:00",
"gender": "N/A",
"highAge": NumberInt(12),
"lowAge": 0.5,
"recordType": "xxxxxxxx",
"__v": NumberInt(0)
}
Object containing key as document id and value as score
-------------------------------------------------------
var textSearch = 'cancer';
var test_ids = {
'277313',
'278237',
'278356',
'278547'
}
scoresArr = {
'277313': '79.06410256410257',
'278237': '65.27777777777777',
'278356': '66.83928571428572',
'278547': '66.8051948051948'
}
My Query where I want to add the score from the above object in new search score.
var aggregation = TestCollection.aggregate(
[{
$match: {
$and: [
{
"id": {
$in : test_ids
}
},
{
$text: {$search: textSearch}
}
]
}
}, {
$sort: {
score: {
$meta: "textScore"
}
}
}, {
$project: {
id: 1,
scoreText: {
$meta: "textScore"
},
score: {
$add: [{
$cond: ["$scoreText", {
$meta: "textScore"
}, {
$meta: "textScore"
}]
},
{
$cond: [{
$gte: [scoresArr["$id"], 0]
},
scoresArr["$id"],
scoresArr["$id"]
]},
]
}
}
}, {
$sort: {
score: -1
}
}
]
);
aggregation.exec(function(err, data) {
// here you will get output
});
I am getting null as a score. I know I am doing it in a wrong way. Please help me to fix this issue.
Thanks