Ejemplo n.º 1
0
def basic_query(query=None,
                bq=None,
                faceting=None,
                size=1000,
                start=0,
                rank=None,
                rank_expressions=None,
                return_fields=None,
                record_stats=False,
                search_api=None):
    if search_api is None:
        search_api = g.CLOUDSEARCH_SEARCH_API
    if faceting is None:
        faceting = DEFAULT_FACETS
    path = _encode_query(query, bq, faceting, size, start, rank,
                         rank_expressions, return_fields)
    timer = None
    if record_stats:
        timer = g.stats.get_timer("providers.cloudsearch")
        timer.start()
    connection = httplib.HTTPConnection(search_api, port=80, timeout=_TIMEOUT)
    try:
        connection.request('GET', path)
        resp = connection.getresponse()
        response = resp.read()
        if record_stats:
            g.stats.action_count("event.search_query", resp.status)
        if resp.status >= 300:
            try:
                reasons = json.loads(response)
            except ValueError:
                pass
            else:
                messages = reasons.get("messages", [])
                for message in messages:
                    if message['code'] in INVALID_QUERY_CODES:
                        raise InvalidQuery(resp.status, resp.reason, message,
                                           search_api, path, reasons)
            raise SearchHTTPError(resp.status, resp.reason, search_api, path,
                                  response)
    except socket.timeout as e:
        g.stats.simple_event('cloudsearch.error.timeout')
        raise SearchError(e, search_api, path)
    except socket.error as e:
        g.stats.simple_event('cloudsearch.error.socket')
        raise SearchError(e, search_api, path)
    finally:
        connection.close()
        if timer is not None:
            timer.stop()

    return json.loads(response)
Ejemplo n.º 2
0
def basic_query(query=None,
                bq=None,
                faceting=None,
                size=1000,
                start=0,
                rank="",
                return_fields=None,
                record_stats=False,
                search_api=None):
    if search_api is None:
        search_api = g.solr_search_host
    if faceting is None:
        faceting = DEFAULT_FACETS
    path = _encode_query(query, faceting, size, start, rank, return_fields)
    timer = None
    if record_stats:
        timer = g.stats.get_timer("solrsearch_timer")
        timer.start()
    connection = httplib.HTTPConnection(search_api, g.solr_port)
    try:
        connection.request('GET', path)
        resp = connection.getresponse()
        response = resp.read()
        if record_stats:
            g.stats.action_count("event.search_query", resp.status)
        if resp.status >= 300:
            try:
                response_json = json.loads(response)
            except ValueError:
                pass
            else:
                if 'error' in response_json:
                    message = response_json['error'].get(
                        'msg', 'Unknown error')
                    raise InvalidQuery(resp.status, resp.reason, message,
                                       search_api, path, response_json)
            raise SearchHTTPError(resp.status, resp.reason, search_api, path,
                                  response)
    except socket.error as e:
        raise SearchError(e, search_api, path)
    finally:
        connection.close()
        if timer is not None:
            timer.stop()

    return json.loads(response)