search.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. module.exports = function(crowi, app) {
  2. 'use strict';
  3. var debug = require('debug')('crowi:routes:search')
  4. , Page = crowi.model('Page')
  5. , User = crowi.model('User')
  6. , ApiResponse = require('../util/apiResponse')
  7. , sprintf = require('sprintf')
  8. , actions = {};
  9. var api = actions.api = {};
  10. actions.searchPage = function(req, res) {
  11. var keyword = req.query.q || null;
  12. var search = crowi.getSearcher();
  13. if (!search) {
  14. return res.json(ApiResponse.error('Configuration of ELASTICSEARCH_URI is required.'));
  15. }
  16. return res.render('search', {
  17. q: keyword,
  18. });
  19. };
  20. /**
  21. * @api {get} /search search page
  22. * @apiName Search
  23. * @apiGroup Search
  24. *
  25. * @apiParam {String} q keyword
  26. * @apiParam {String} path
  27. */
  28. api.search = function(req, res){
  29. var keyword = req.query.q || null;
  30. if (keyword === null || keyword === '') {
  31. return res.json(ApiResponse.error('keyword should not empty.'));
  32. }
  33. var search = crowi.getSearcher();
  34. if (!search) {
  35. return res.json(ApiResponse.error('Configuration of ELASTICSEARCH_URI is required.'));
  36. }
  37. var result = {};
  38. search.searchKeyword(keyword, {})
  39. .then(function(data) {
  40. result.meta = data.meta;
  41. return Page.populatePageListToAnyObjects(data.data);
  42. }).then(function(pages) {
  43. result.data = pages;
  44. return res.json(ApiResponse.success(result));
  45. })
  46. .catch(function(err) {
  47. return res.json(ApiResponse.error(err));
  48. });
  49. };
  50. return actions;
  51. };