All files / controllers search.js

23.08% Statements 9/39
0% Branches 0/20
12.5% Functions 1/8
23.08% Lines 9/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 8815x       15x 15x 15x 15x 15x   15x                                           15x                                                                                                         15x    
module.exports = function(crowi, app) {
  'use strict'
 
  // var debug = require('debug')('crowi:routes:search')
  var Page = crowi.model('Page')
  var ApiResponse = require('../util/apiResponse')
  var ApiPaginate = require('../util/apiPaginate')
  var actions = {}
  var api = (actions.api = {})
 
  actions.searchPage = function(req, res) {
    var keyword = req.query.q || null
    var search = crowi.getSearcher()
    if (!search) {
      return res.json(ApiResponse.error('Configuration of ELASTICSEARCH_URI is required.'))
    }
 
    return res.render('search', {
      q: keyword,
    })
  }
 
  /**
   * @api {get} /search search page
   * @apiName Search
   * @apiGroup Search
   *
   * @apiParam {String} q keyword
   * @apiParam {String} path
   * @apiParam {String} offset
   * @apiParam {String} limit
   */
  api.search = function(req, res) {
    const { q: keyword = null, tree = null, type = null } = req.query
    let paginateOpts
 
    try {
      paginateOpts = ApiPaginate.parseOptionsForElasticSearch(req.query)
    } catch (e) {
      res.json(ApiResponse.error(e))
    }
 
    if (keyword === null || keyword === '') {
      return res.json(ApiResponse.error('keyword should not empty.'))
    }
 
    const search = crowi.getSearcher()
    if (!search) {
      return res.json(ApiResponse.error('Configuration of ELASTICSEARCH_URI is required.'))
    }
 
    const searchOpts = { ...paginateOpts, type }
    let doSearch
    if (tree) {
      doSearch = search.searchKeywordUnderPath(keyword, tree, searchOpts)
    } else {
      doSearch = search.searchKeyword(keyword, searchOpts)
    }
    const result = {}
    doSearch
      .then(function(data) {
        result.meta = data.meta
        result.searchResult = data.data
 
        return Page.populatePageListToAnyObjects(data.data)
      })
      .then(function(pages) {
        result.data = pages
          .filter(page => {
            if (Object.keys(page).length < 12) {
              // FIXME: 12 is a number of columns.
              return false
            }
            return true
          })
          .map(page => {
            return { ...page, bookmarkCount: (page._source && page._source.bookmark_count) || 0 }
          })
        return res.json(ApiResponse.success(result))
      })
      .catch(function(err) {
        return res.json(ApiResponse.error(err))
      })
  }
 
  return actions
}