def validate(self): for key, value in self.items(): if key in self.BOOLEAN_OPTIONS: try: value = asbool(value) except ValueError: raise SearchQueryError('Value for search option %r must be True or False (1 or 0) but received %r' % (key, value)) elif key in self.INTEGER_OPTIONS: try: value = int(value) except ValueError: raise SearchQueryError('Value for search option %r must be an integer but received %r' % (key, value)) elif key in self.UNSUPPORTED_OPTIONS: raise SearchQueryError('Search option %r is not supported' % key) self[key] = value
def convert_legacy_parameters_to_solr(legacy_params): '''API v1 and v2 allowed search params that the SOLR syntax does not support, so use this function to convert those to SOLR syntax. See tests for examples. raises SearchQueryError on invalid params. ''' options = QueryOptions(**legacy_params) options.validate() solr_params = legacy_params.copy() solr_q_list = [] if solr_params.get('q'): solr_q_list.append(solr_params['q'].replace('+', ' ')) non_solr_params = set(legacy_params.keys()) - VALID_SOLR_PARAMETERS for search_key in non_solr_params: value_obj = legacy_params[search_key] value = value_obj.replace('+', ' ') if isinstance( value_obj, basestring) else value_obj if search_key == 'all_fields': if value: solr_params['fl'] = '*' elif search_key == 'offset': solr_params['start'] = value elif search_key == 'limit': solr_params['rows'] = value elif search_key == 'order_by': solr_params['sort'] = '%s asc' % value elif search_key == 'tags': if isinstance(value_obj, list): tag_list = value_obj elif isinstance(value_obj, basestring): tag_list = [value_obj] else: raise SearchQueryError( 'Was expecting either a string or JSON list for the tags parameter: %r' % value) solr_q_list.extend([ 'tags:"%s"' % escape_legacy_argument(tag) for tag in tag_list ]) else: if len(value.strip()): value = escape_legacy_argument(value) if ' ' in value: value = '"%s"' % value solr_q_list.append('%s:%s' % (search_key, value)) del solr_params[search_key] solr_params['q'] = ' '.join(solr_q_list) if non_solr_params: log.debug('Converted legacy search params from %r to %r', legacy_params, solr_params) return solr_params
def run(self, query): ''' Performs a dataset search using the given query. @param query - dictionary with keys like: q, fq, sort, rows, facet @return - dictionary with keys results and count May raise SearchQueryError or SearchError. ''' from solr import SolrException assert isinstance(query, (dict, MultiDict)) # check that query keys are valid if not set(query.keys()) <= VALID_SOLR_PARAMETERS: invalid_params = [ s for s in set(query.keys()) - VALID_SOLR_PARAMETERS ] raise SearchQueryError("Invalid search parameters: %s" % invalid_params) # default query is to return all documents q = query.get('q') if not q or q == '""' or q == "''": query['q'] = "*:*" # number of results query['rows'] = min(1000, int(query.get('rows', 10))) # order by score if no 'sort' term given order_by = query.get('sort') if order_by == 'rank' or order_by is None: query['sort'] = 'score desc, name asc' # show only results from this CKAN instance fq = query.get('fq', '') if not '+site_id:' in fq: fq += ' +site_id:"%s"' % config.get('ckan.site_id') # filter for package status if not '+state:' in fq: fq += " +state:active" query['fq'] = fq # faceting query['facet'] = query.get('facet', 'true') query['facet.limit'] = query.get( 'facet.limit', config.get('search.facets.limit', '50')) query['facet.mincount'] = query.get('facet.mincount', 1) # return the package ID and search scores query['fl'] = query.get('fl', 'name') # return results as json encoded string query['wt'] = query.get('wt', 'json') # query field weighting: disabled for now as solr 3.* is required for # the 'edismax' query parser, our current Ubuntu version only has # packages for 1.4 # # query['defType'] = 'edismax' # query['tie'] = '0.5' # query['qf'] = query.get('qf', QUERY_FIELDS) conn = make_connection() log.debug('Package query: %r' % query) try: solr_response = conn.raw_query(**query) except SolrException, e: raise SearchError( 'SOLR returned an error running query: %r Error: %r' % (query, e.reason))
def run(self, query): ''' Performs a dataset search using the given query. @param query - dictionary with keys like: q, fq, sort, rows, facet @return - dictionary with keys results and count May raise SearchQueryError or SearchError. ''' from solr import SolrException assert isinstance(query, (dict, MultiDict)) # check that query keys are valid if not set(query.keys()) <= VALID_SOLR_PARAMETERS: invalid_params = [ s for s in set(query.keys()) - VALID_SOLR_PARAMETERS ] raise SearchQueryError("Invalid search parameters: %s" % invalid_params) # default query is to return all documents q = query.get('q') if not q or q == '""' or q == "''": query['q'] = "*:*" # number of results rows_to_return = min(1000, int(query.get('rows', 10))) if rows_to_return > 0: # #1683 Work around problem of last result being out of order # in SOLR 1.4 rows_to_query = rows_to_return + 1 else: rows_to_query = rows_to_return query['rows'] = rows_to_query # order by score if no 'sort' term given order_by = query.get('sort') if order_by == 'rank' or order_by is None: query['sort'] = 'score desc, name asc' # show only results from this CKAN instance fq = query.get('fq', '') if not '+site_id:' in fq: fq += ' +site_id:"%s"' % config.get('ckan.site_id') # filter for package status if not '+state:' in fq: fq += " +state:active" query['fq'] = fq # faceting query['facet'] = query.get('facet', 'true') query['facet.limit'] = query.get( 'facet.limit', config.get('search.facets.limit', '50')) query['facet.mincount'] = query.get('facet.mincount', 1) # return the package ID and search scores query['fl'] = query.get('fl', 'name') # return results as json encoded string query['wt'] = query.get('wt', 'json') # If the query has a colon in it then consider it a fielded search and do use dismax. if ':' not in query['q']: query['defType'] = 'dismax' query['tie'] = '0.1' query['mm'] = '1' query['qf'] = query.get('qf', QUERY_FIELDS) conn = make_connection() log.debug('Package query: %r' % query) try: solr_response = conn.raw_query(**query) except SolrException, e: raise SearchError( 'SOLR returned an error running query: %r Error: %r' % (query, e.reason))