<commit_before>###

@author <NAME>
@copyright 2015 Microbrew.it
###

RouteHandler = require '../../core/RouteHandler'

class Handler extends RouteHandler

	getRoute: () ->
		super [
			{ path: '/search/{query}', method: 'GET' }
			{ path: '/search/ingredients/{query}', method: 'GET' }
			{ path: '/search', method: 'GET' }
			{ path: '/search/ingredients', method: 'GET' }
		]

	render: (results, user, query, hits, pagination) ->
		@renderer.page
			title: "Search"
			navigationState: 'search'
			user: user
			html: @renderer.render
				template: "public/templates/search/index.jade"
				data: 
					user: user
					type:'search' 
					#headline: @renderer.headline "Search"
					results: results
					query: query
					hits: hits
					nextPage: pagination?.next
					prevPage: pagination?.prev

	_parseHits: (body) ->
		results = []
		for item in body.hits.hits
			resultItem = item._source
			resultItem.score = item._score
			results.push resultItem
		return results

	onRequest: (req, reply) ->
		@logger.log req.params
		if req.raw.query?.query
			@api.search.get(req.params, (err, res, body) =>
				@logger.log JSON.stringify body, false, '\t'
				hits = body.hits.total
				query = req.params.query
				results = @_parseHits(body)
				pagination = @makePrevNextLink(req.raw.url.query, req.raw.url.pathname, results.length)

				reply @render results, req.user, query, hits, pagination

			, req.token)
		else
			reply @render()

<commit_msg>Fixed rendering search<commit_after>###

@author <NAME>
@copyright 2015 Microbrew.it
###

RouteHandler = require '../../core/RouteHandler'

class Handler extends RouteHandler

	getRoute: () ->
		super [
			{ path: '/search/{query}', method: 'GET' }
			{ path: '/search/ingredients/{query}', method: 'GET' }
			{ path: '/search', method: 'GET' }
			{ path: '/search/ingredients', method: 'GET' }
		]

	render: (results, user, query, hits, pagination) ->
		@renderer.page
			title: "Search"
			navigationState: 'search'
			user: user
			html: @renderer.render
				template: "public/templates/search/index.jade"
				data: 
					user: user
					type:'search' 
					#headline: @renderer.headline "Search"
					results: results
					query: query
					hits: hits
					nextPage: pagination?.next
					prevPage: pagination?.prev

	_parseHits: (body) ->
		return body
		# results = []
		# for item in body.hits.hits
		# 	resultItem = item._source
		# 	resultItem.score = item._score
		# 	results.push resultItem
		# return results

	onRequest: (req, reply) ->
		@logger.log req.params
		if req.raw.query?.query
			@api.search.get(req.params, (err, res, body) =>
				@logger.log JSON.stringify body, false, '\t'
				hits = body.length
				query = req.params.query
				results = @_parseHits(body)
				pagination = @makePrevNextLink(req.raw.url.query, req.raw.url.pathname, results.length)

				reply @render results, req.user, query, hits, pagination

			, req.token)
		else
			reply @render()


{'fraction_non_alphanumeric': 0.1454855195911414, 'fraction_numerical': 0.00272572402044293, 'mean_word_length': 3.112044817927171, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6706853', 'n_tokens_mistral': 1186, 'n_tokens_neox': 986, 'n_words': 238}