2

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

Varun Nayyar
  • 887
  • 4
  • 20
  • 46
  • Try merging the arrays in one step of aggregation – Rahul Patel Nov 23 '16 at 10:55
  • @chridam ,my document has id field and my query is failing only for the above case when I am trying to add score from external object. – Varun Nayyar Nov 23 '16 at 11:22
  • I missed that since you've just [updated your question](http://stackoverflow.com/posts/40762019/revisions). Can you please add the full document structure? Better yet, can you add some sample documents for testing and your expected JSON output from that sample? – chridam Nov 23 '16 at 11:25
  • @chridam I have updated my question and I am using mongoose text search for searching. – Varun Nayyar Nov 23 '16 at 11:38
  • May be you can add the doc that you used for testing which will recreate the problem you are facing. Right now the input values doesnt match with the doc. – s7vr Nov 23 '16 at 13:13

0 Answers0