let db = require(`${__dirname}/../schemas.js`);
let Recipe = db.Recipe;
let _ = require('underscore');
let Promise = require('bluebird');

module.exports = {
  // description: Returns a list of recipes by relevancy to a given search string
  // params: {
  //   string: 'search string'
  // }
  search: (req, res) => {
    //build a query for each word in search string
    let searchTerms = req.params.string.split('+');
    console.log('SEARCH TERMS: ', searchTerms);
    let queries = searchTerms.map(term => {
      return Recipe.find({
        'name.value': new RegExp(term, 'i')
      });
    });
    
    //execute all queries
    return Promise.all(queries)
    .spread((...queryResults) => {
      //join results
      let joinedResults = queryResults.length > 0 ? queryResults[0].concat(...queryResults.slice(1)) : [];

      //tally appearances between queries
      let appearances = {};
      joinedResults.forEach((result) => {
        if (!appearances[result._id]) {
          appearances[result._id] = 0;
        }
        appearances[result._id]++;
      });
      console.log('appearances: ', appearances);

      //sort based on # of appearances
      let sortedRecipes = _.pairs(appearances)
      .sort((a, b) => {
        return b[1] - a[1];
      }).map(recipe => {
        return joinedResults.find((queryResult) => {
          return queryResult._id.equals(recipe[0]);
        });
      });
      console.log('sorted recipes: ', sortedRecipes);

      //return sorted by appearances
      res.status(200).send(sortedRecipes);
    }).catch(error => {
      console.log('ERROR: ', error);
      res.status(404).send(error);
    });
  }
};
@@ -49,20 +49,105 @@
 let 
-Recipe = db.
+helpers = require(%60$%7B__dirname%7D/../helpers.js%60);%0Alet Recipe = db.Recipe;%0Alet UserRecipe = db.User
 Reci
@@ -504,56 +504,8 @@
 ');%0A
-    console.log('SEARCH TERMS: ', searchTerms);%0A
     
@@ -727,24 +727,432 @@
 sults) =%3E %7B%0A
+      //group by rootVersionid%0A      let uniqueRoots = (results) =%3E %7B%0A        let unique = %7B%7D;%0A        results.forEach(result =%3E %7B%0A          unique%5Bresult.rootVersion %7C%7C result._id%5D = result;          %0A        %7D);%0A        results = _.values(unique);%0A        return results;%0A      %7D;%0A%0A      let uniqueQueryResults = %5B%5D;%0A      queryResults.forEach(result =%3E %7B uniqueQueryResults.push(uniqueRoots(result)) %7D);%0A%0A
       //join
@@ -1186,17 +1186,23 @@
 sults = 
-q
+uniqueQ
 ueryResu
@@ -1218,17 +1218,23 @@
 h %3E 0 ? 
-q
+uniqueQ
 ueryResu
@@ -1246,25 +1246,31 @@
 %5D.concat(...
-q
+uniqueQ
 ueryResults.
@@ -1415,32 +1415,54 @@
 f (!appearances%5B
+result.rootVersion %7C%7C 
 result._id%5D) %7B%0A 
@@ -1474,32 +1474,54 @@
     appearances%5B
+result.rootVersion %7C%7C 
 result._id%5D = 0;
@@ -1562,72 +1562,45 @@
 ult.
-_id%5D++;%0A      %7D);%0A      console.log('appearances: ', appearances
+rootVersion %7C%7C result._id%5D++;%0A      %7D
 );%0A%0A
@@ -1796,17 +1796,16 @@
 ts.find(
-(
 queryRes
@@ -1803,25 +1803,24 @@
 (queryResult
-)
  =%3E %7B%0A      
@@ -1827,28 +1827,78 @@
     
-return queryResult._
+let id = queryResult.rootVersion %7C%7C queryResult._id;%0A          return 
 id.e
@@ -1941,52 +1941,714 @@
 %7D);%0A
+%0A
       
-console.log('sorted recipes: ', 
+//get full versions%0A      let sortedFullRecipes = %5B%5D;%0A      sortedRecipes.forEach(result =%3E %7B%0A        let fullRecipe = UserRecipe.findOne(%7B%0A          username: result.username%0A        %7D).then(userRecipesCollection =%3E %7B%0A          let recipe = _.find(userRecipesCollection.recipes, recipe =%3E %7B%0A            return recipe.rootRecipeId.equals(result.rootVersion %7C%7C result._id);%0A          %7D);%0A          let version = _.find(recipe.branches, currentBranch =%3E %7B%0A            return currentBranch.name === result.branch;%0A          %7D);%0A%0A          return helpers.retrieveVersion(version.mostRecentVersionId);%0A        %7D);%0A%0A        sortedFullRecipes.push(fullRecipe);%0A      %7D);%0A      return Promise.all(
 sorted
+Full
 Reci
@@ -2653,16 +2653,51 @@
 cipes);%0A
+    %7D).spread((...fullRecipes) =%3E %7B
 %0A      /
@@ -2753,22 +2753,20 @@
 0).send(
-sorted
+full
 Recipes)

Add search for recipes names