0

I am having trouble with my elasticsearch/node/express application. A result returned by elasticsearch takes following format.

[{
    _index:"football",
    _type:"palyer",
    _source:{
        name:"Paul Pogba",
        jerseyNumber:14,
        shortName:"Norwich"
    }
    }]
[{
    _index:"football",
    _type:"team",
    _source:{
        name:"Norwich",
        shortName:"Norwich"
    }
    }]

My primary concern is to provide combined search results. Say when I search a Club/Team (Ex:Manchester) it should return all clubs with search-term (that is done). Additionally I am planning to find the team members by searching with shortName of the team. I can do this when I have only one result returned in team search. When two or more results are there calling the player search for all of them is not working due to asynchronous nature of JS. I am researching on promise and express-promise-router and couldn't come up with a possible solution. I have two separate functions searchTeam and searchPlayer which accept searchTerm and return results via a callback function.

search player function

module.exports.searchPlayers = function(searchData, callback) {
  client.search({
    //elasticsearch query
  }).then(function (resp) {
    callback(resp.hits.hits);
  }, function (err) {
      callback(err.message)
      console.log(err.message);
  });
}

calling search in ejs

<form action='/search/search-players' method='post'>
    <input type="text" name="searchTerm" placeholder="your search term here">
    <button type="submit"> SEARCH </button>
</form>

route

router.post('/search/search-players', function(req, res) {
  searchModulePlayers.searchPlayers(req.body, function(data) {
    res.render('search', { title: 'Players', resultsP: data });
  });
});

Since my search functions are accepting only one term how do I get all search results to an array so that I can render them in one page with ejs. I have posted similar question q1 to this before. Since I couldn't get a proper answer I am asking again in a different angle.

Community
  • 1
  • 1
akalanka
  • 553
  • 7
  • 21
  • See my updated answer to your previous question: http://stackoverflow.com/a/40297822/4604579 ;-) – Val Nov 04 '16 at 05:06
  • @Val: Thanks for the answer. It was slightly modified to pass the parameter to searchTeam. – akalanka Nov 04 '16 at 18:37

0 Answers0