search.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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.filter(function(page) {
  44. if (Object.keys(page).length < 12) { // FIXME: 12 is a number of columns.
  45. return false;
  46. }
  47. return true;
  48. });
  49. return res.json(ApiResponse.success(result));
  50. })
  51. .catch(function(err) {
  52. return res.json(ApiResponse.error(err));
  53. });
  54. };
  55. return actions;
  56. };