search.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. const { serializeUserSecurely } = require('../models/serializers/user-serializer');
  2. /**
  3. * @swagger
  4. *
  5. * components:
  6. * schemas:
  7. * ElasticsearchResult:
  8. * description: Elasticsearch result v1
  9. * type: object
  10. * properties:
  11. * meta:
  12. * type: object
  13. * properties:
  14. * took:
  15. * type: number
  16. * description: Time Elasticsearch took to execute a search(milliseconds)
  17. * example: 34
  18. * total:
  19. * type: number
  20. * description: Number of documents matching search criteria
  21. * example: 2
  22. * results:
  23. * type: number
  24. * description: Actual array length of search results
  25. * example: 2
  26. *
  27. */
  28. module.exports = function(crowi, app) {
  29. // var debug = require('debug')('growi:routes:search')
  30. const Page = crowi.model('Page');
  31. const User = crowi.model('User');
  32. const ApiResponse = require('../util/apiResponse');
  33. const ApiPaginate = require('../util/apiPaginate');
  34. const actions = {};
  35. const api = {};
  36. actions.searchPage = function(req, res) {
  37. const keyword = req.query.q || null;
  38. return res.render('search', {
  39. q: keyword,
  40. });
  41. };
  42. /**
  43. * @swagger
  44. *
  45. * /search:
  46. * get:
  47. * tags: [Search, CrowiCompatibles]
  48. * operationId: searchPages
  49. * summary: /search
  50. * description: Search pages
  51. * parameters:
  52. * - in: query
  53. * name: q
  54. * schema:
  55. * type: string
  56. * description: keyword
  57. * example: daily report
  58. * required: true
  59. * - in: query
  60. * name: path
  61. * schema:
  62. * $ref: '#/components/schemas/Page/properties/path'
  63. * - in: query
  64. * name: offset
  65. * schema:
  66. * $ref: '#/components/schemas/V1PaginateResult/properties/meta/properties/offset'
  67. * - in: query
  68. * name: limit
  69. * schema:
  70. * $ref: '#/components/schemas/V1PaginateResult/properties/meta/properties/limit'
  71. * responses:
  72. * 200:
  73. * description: Succeeded to get list of pages.
  74. * content:
  75. * application/json:
  76. * schema:
  77. * properties:
  78. * ok:
  79. * $ref: '#/components/schemas/V1Response/properties/ok'
  80. * meta:
  81. * $ref: '#/components/schemas/ElasticsearchResult/properties/meta'
  82. * totalCount:
  83. * type: integer
  84. * description: total count of pages
  85. * example: 35
  86. * data:
  87. * type: array
  88. * items:
  89. * $ref: '#/components/schemas/Page'
  90. * description: page list
  91. * 403:
  92. * $ref: '#/components/responses/403'
  93. * 500:
  94. * $ref: '#/components/responses/500'
  95. */
  96. /**
  97. * @api {get} /search search page
  98. * @apiName Search
  99. * @apiGroup Search
  100. *
  101. * @apiParam {String} q keyword
  102. * @apiParam {String} path
  103. * @apiParam {String} offset
  104. * @apiParam {String} limit
  105. */
  106. api.search = async function(req, res) {
  107. const user = req.user;
  108. const { q: keyword = null, type = null } = req.query;
  109. let paginateOpts;
  110. try {
  111. paginateOpts = ApiPaginate.parseOptionsForElasticSearch(req.query);
  112. }
  113. catch (e) {
  114. res.json(ApiResponse.error(e));
  115. }
  116. if (keyword === null || keyword === '') {
  117. return res.json(ApiResponse.error('keyword should not empty.'));
  118. }
  119. const { searchService } = crowi;
  120. if (!searchService.isReachable) {
  121. return res.json(ApiResponse.error('SearchService is not reachable.'));
  122. }
  123. let userGroups = [];
  124. if (user != null) {
  125. const UserGroupRelation = crowi.model('UserGroupRelation');
  126. userGroups = await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user);
  127. }
  128. const searchOpts = { ...paginateOpts, type };
  129. const result = {};
  130. try {
  131. const esResult = await searchService.searchKeyword(keyword, user, userGroups, searchOpts);
  132. // create score map for sorting
  133. // key: id , value: score
  134. const scoreMap = {};
  135. for (const esPage of esResult.data) {
  136. scoreMap[esPage._id] = esPage._score;
  137. }
  138. const ids = esResult.data.map((page) => { return page._id });
  139. const findResult = await Page.findListByPageIds(ids);
  140. // add tag data to result pages
  141. findResult.pages.map((page) => {
  142. const data = esResult.data.find((data) => { return page.id === data._id });
  143. page._doc.tags = data._source.tag_names;
  144. return page;
  145. });
  146. result.meta = esResult.meta;
  147. result.totalCount = findResult.totalCount;
  148. result.data = findResult.pages
  149. .map((page) => {
  150. if (page.lastUpdateUser != null && page.lastUpdateUser instanceof User) {
  151. page.lastUpdateUser = serializeUserSecurely(page.lastUpdateUser);
  152. }
  153. page.bookmarkCount = (page._source && page._source.bookmark_count) || 0;
  154. return page;
  155. })
  156. .sort((page1, page2) => {
  157. // note: this do not consider NaN
  158. return scoreMap[page2._id] - scoreMap[page1._id];
  159. });
  160. }
  161. catch (err) {
  162. return res.json(ApiResponse.error(err));
  163. }
  164. return res.json(ApiResponse.success(result));
  165. };
  166. actions.api = api;
  167. return actions;
  168. };