def send_documents(self, docs): '''Open a connection to the Solr endpoint, and send the documents for indexing. Multiple requests are sent if a large number of documents are being sent (see chunk_xml()) Raises SearchHTTPError if the endpoint indicates a failure ''' core = getattr(g, 'solr_core', 'collection1') responses = [] connection = httplib.HTTPConnection(self.solr_host, self.solr_port) chunker = chunk_xml(docs) headers = {} headers['Content-Type'] = 'application/xml' try: for data in chunker: # HTTPLib calculates Content-Length header automatically if getattr(g, 'solr_version', '1').startswith('4'): connection.request('POST', "/solr/%s/update/" % core, data, headers) else: connection.request('POST', "/solr/update/", data, headers) response = connection.getresponse() if 200 <= response.status < 300: responses.append(response.read()) else: raise SearchHTTPError(response.status, response.reason, response.read()) finally: connection.close() return responses
def send_documents(self, docs): '''Open a connection to the cloudsearch endpoint, and send the documents for indexing. Multiple requests are sent if a large number of documents are being sent (see chunk_xml()) Raises SearchHTTPError if the endpoint indicates a failure ''' responses = [] connection = httplib.HTTPConnection(self.doc_api, port=80, timeout=_TIMEOUT) chunker = chunk_xml(docs) try: for data in chunker: headers = {} headers['Content-Type'] = 'application/xml' # HTTPLib calculates Content-Length header automatically connection.request('POST', "/2011-02-01/documents/batch", data, headers) response = connection.getresponse() if 200 <= response.status < 300: responses.append(response.read()) else: raise SearchHTTPError(response.status, response.reason, response.read()) finally: connection.close() return responses
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)
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)