All files / lib/controllers search.ts

18.92% Statements 7/37
0% Branches 0/20
20% Functions 1/5
18.92% Lines 7/37

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 88 89          16x     16x 16x 16x   16x                                           16x                                                                                                       16x    
import Crowi from 'server/crowi'
import ApiResponse from '../util/apiResponse'
import ApiPaginate from '../util/apiPaginate'
import Debug from 'debug'
 
const debug = Debug('crowi:routes:search')
 
export default (crowi: Crowi) => {
  const Page = crowi.model('Page')
  const actions = {} as any
  const api = (actions.api = {} as any)
 
  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 = async function(req, res) {
    const { user } = req
    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, user, searchOpts)
    } else {
      doSearch = search.searchKeyword(keyword, user, searchOpts)
    }
 
    try {
      const { meta, data: searchResult } = await doSearch
 
      const pages = await Page.populatePageListToAnyObjects(searchResult)
 
      const 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({ meta, searchResult, data }))
    } catch (err) {
      debug('Error on searching:', err)
      return res.json(ApiResponse.error(err))
    }
  }
 
  return actions
}