def search(self, trans, search_term, **kwd): """ Perform a search over the Whoosh index. The index has to be pre-created with build_ts_whoosh_index.sh. TS config option toolshed_search_on has to be turned on and whoosh_index_dir has to be specified. :param search_term: (required)the term to search :type search_term: str :param page: (optional)requested page of the search :type page: int :param jsonp: (optional)flag whether to use jsonp format response, defaults to False :type jsonp: bool :param callback: (optional)name of the function to wrap callback in used only when jsonp is true, defaults to 'callback' :type callback: str :returns dict: object containing list of results and a hostname """ if not self.app.config.toolshed_search_on: raise exceptions.ConfigDoesNotAllowException( 'Searching the TS through the API is turned off for this instance.' ) if not self.app.config.whoosh_index_dir: raise exceptions.ConfigDoesNotAllowException( 'There is no directory for the search index specified. Please ontact the administrator.' ) search_term = search_term.strip() if len(search_term) < 3: raise exceptions.RequestParameterInvalidException( 'The search term has to be at least 3 characters long.') page = kwd.get('page', 1) return_jsonp = util.asbool(kwd.get('jsonp', False)) callback = kwd.get('callback', 'callback') repo_search = RepoSearch() results = repo_search.search(trans, search_term, page) results['hostname'] = url_for('/', qualified=True) if return_jsonp: response = str('%s(%s);' % (callback, json.dumps(results))) else: response = json.dumps(results) return response
def _search(self, trans, q, page=1, page_size=10): """ Perform the search over TS repositories. Note that search works over the Whoosh index which you have to pre-create with scripts/tool_shed/build_ts_whoosh_index.sh manually. Also TS config option toolshed_search_on has to be True and whoosh_index_dir has to be specified. """ conf = self.app.config if not conf.toolshed_search_on: raise ConfigDoesNotAllowException( 'Searching the TS through the API is turned off for this instance.' ) if not conf.whoosh_index_dir: raise ConfigDoesNotAllowException( 'There is no directory for the search index specified. Please contact the administrator.' ) search_term = q.strip() if len(search_term) < 3: raise RequestParameterInvalidException( 'The search term has to be at least 3 characters long.') repo_search = RepoSearch() Boosts = namedtuple('Boosts', [ 'repo_name_boost', 'repo_description_boost', 'repo_long_description_boost', 'repo_homepage_url_boost', 'repo_remote_repository_url_boost', 'repo_owner_username_boost' ]) boosts = Boosts( float(conf.get('repo_name_boost', 0.9)), float(conf.get('repo_description_boost', 0.6)), float(conf.get('repo_long_description_boost', 0.5)), float(conf.get('repo_homepage_url_boost', 0.3)), float(conf.get('repo_remote_repository_url_boost', 0.2)), float(conf.get('repo_owner_username_boost', 0.3))) results = repo_search.search(trans, search_term, page, page_size, boosts) results['hostname'] = web.url_for('/', qualified=True) return results
def search(self, trans, search_term, **kwd): """ Perform a search over the Whoosh index. The index has to be pre-created with build_ts_whoosh_index.sh. TS config option toolshed_search_on has to be turned on and toolshed_whoosh_index_dir has to be specified and existing. :param search_term: :param page: :param jsonp: :param callback: :returns dict: """ if not self.app.config.toolshed_search_on: raise exceptions.ConfigDoesNotAllowException( 'Searching the TS through the API is turned off for this instance.' ) if not self.app.config.toolshed_whoosh_index_dir: raise exceptions.ConfigDoesNotAllowException( 'There is no directory for the search index specified. Please ontact the administrator.' ) search_term = search_term.strip() if len(search_term) < 3: raise exceptions.RequestParameterInvalidException( 'The search term has to be at least 3 characters long.') page = kwd.get('page', 1) return_jsonp = util.asbool(kwd.get('jsonp', False)) callback = kwd.get('callback', 'callback') repo_search = RepoSearch() results = repo_search.search(trans, search_term, page) results['hostname'] = url_for('/', qualified=True) if return_jsonp: response = '%s(%s);' % (callback, json.dumps(results)) else: response = json.dumps(results) return response