o ckF[4&@sddlZddlmZddlZddlZGdddeZGdddeZGdddeZGd d d eZ Gd d d e Z Gd dde Z dS)N)jsonc@ eZdZdS)SearchServiceExceptionN__name__ __module__ __qualname__r r ;/usr/lib/python3/dist-packages/boto/cloudsearch/document.pyrrc@r)CommitMismatchErrorNrr r r r r "r r c@eZdZdZdS) EncodingErrorz Content sent for Cloud Search indexing was incorrectly encoded. This usually happens when a document is marked as unicode but non-unicode characters are present. Nrrr__doc__r r r r r%src@r )ContentTooLongErrorz Content sent for Cloud Search indexing was too long This will usually happen when documents queued for indexing add up to more than the limit allowed per upload batch (5MB) Nrr r r r r.src@sLeZdZdZdddZdddZdd Zd d Zd d ZddZ ddZ dS)DocumentServiceConnectiona+ A CloudSearch document service. The DocumentServiceConection is used to add, remove and update documents in CloudSearch. Commands are uploaded to CloudSearch in SDF (Search Document Format). To generate an appropriate SDF, use :func:`add` to add or update documents, as well as :func:`delete` to remove documents. Once the set of documents is ready to be index, use :func:`commit` to send the commands to CloudSearch. If there are a lot of documents to index, it may be preferable to split the generation of SDF data and the actual uploading into CloudSearch. Retrieve the current SDF with :func:`get_sdf`. If this file is the uploaded into S3, it can be retrieved back afterwards for upload into CloudSearch using :func:`add_sdf_from_s3`. The SDF is not cleared after a :func:`commit`. If you wish to continue using the DocumentServiceConnection for another batch upload of commands, you will need to :func:`clear_sdf` first to stop the previous batch of commands from being uploaded again. NcCs*||_||_|js |j|_g|_d|_dS)N)domainendpointdoc_service_endpointdocuments_batch_sdf)selfrrr r r __init__Rs  z"DocumentServiceConnection.__init__encCs d||||d}|j|dS)a Add a document to be processed by the DocumentService The document will not actually be added until :func:`commit` is called :type _id: string :param _id: A unique ID used to refer to this document. :type version: int :param version: Version of the document being indexed. If a file is being reindexed, the version should be higher than the existing one in CloudSearch. :type fields: dict :param fields: A dictionary of key-value pairs to be uploaded . :type lang: string :param lang: The language code the data is in. Only 'en' is currently supported add)typeidversionlangfieldsNrappend)r_idrr rdr r r rZszDocumentServiceConnection.addcCsd||d}|j|dS)a Schedule a document to be removed from the CloudSearch service The document will not actually be scheduled for removal until :func:`commit` is called :type _id: string :param _id: The unique ID of this document. :type version: int :param version: Version of the document to remove. The delete will only occur if this version number is higher than the version currently in the index. delete)rrrNr!)rr#rr$r r r r%ts z DocumentServiceConnection.deletecCs|jr|jSt|jS)z Generate the working set of documents in Search Data Format (SDF) :rtype: string :returns: JSON-formatted string of the documents in SDF )rrdumpsrrr r r get_sdfsz!DocumentServiceConnection.get_sdfcCsd|_g|_dS)z Clear the working documents from this DocumentServiceConnection This should be used after :func:`commit` if the connection will be reused for another set of documents. N)rrr'r r r clear_sdfs z#DocumentServiceConnection.clear_sdfcCs||_dS)a Load an SDF from S3 Using this method will result in documents added through :func:`add` and :func:`delete` being ignored. :type key_obj: :class:`boto.s3.key.Key` :param key_obj: An S3 key which contains an SDF N)get_contents_as_stringr)rkey_objr r r add_sdf_from_s3s z)DocumentServiceConnection.add_sdf_from_s3cCs|}d|vr!tjd|d}tj||d|dd|j}t}tjj dddd}| d || d ||j ||d d id }t |||S)aD Actually send an SDF to CloudSearch for processing If an SDF file has been explicitly loaded it will be used. Otherwise, documents added through :func:`add` and :func:`delete` will be used. :rtype: :class:`CommitResponse` :returns: A summary of documents added and deleted z: nullz@null value in sdf detected. This will probably raise 500 error.dz$http://%s/2011-02-01/documents/batch2)pool_connections pool_maxsize max_retrieszhttp://zhttps://z Content-Typezapplication/json)dataheaders) r(botologerrorindexrrequestsSessionadapters HTTPAdaptermountpostCommitResponse)rsdfr9urlsessionadapterrr r r commits        z DocumentServiceConnection.commit)NN)r) rrrrrrr%r(r)r,rFr r r r r8s    rc@s eZdZdZddZddZdS)r@ajWrapper for response to Cloudsearch document batch commit. :type response: :class:`requests.models.Response` :param response: Response from Cloudsearch /documents/batch API :type doc_service: :class:`boto.cloudsearch.document.DocumentServiceConnection` :param doc_service: Object containing the documents posted and methods to retry :raises: :class:`boto.exception.BotoServerError` :raises: :class:`boto.cloudsearch.document.SearchServiceException` :raises: :class:`boto.cloudsearch.document.EncodingError` :raises: :class:`boto.cloudsearch.document.ContentTooLongError` c Cs(||_||_||_|jd}zt||_Wntj d ||jtj j |jj d|d|jd|_|jdkrudd|jd gD|_|jD]}d |vrVtd |d kr^td qLd|jvsid|jvrttd|jddng|_|jd|_|jd|_|d|j|d|jdS)Nzutf-8z9Error indexing documents. Response Content: {0} SDF: {1})bodystatusr8cSsg|]}|dqS)message)get).0er r r sz+CommitResponse.__init__..errorszIllegal Unicode characterz%Illegal Unicode character in documentzThe Content-Length is too longzContent was too longaddsdeleteszError indexing documents => %srJrr%)response doc_servicerAcontentdecoderloadsr6r7r8format exceptionBotoServerError status_coderIrKrOrrrrPrQ_check_num_ops)rrRrSrA_bodyrMr r r rsD        zCommitResponse.__init__cs8tfdd|jjD}||krtd||dS)aXRaise exception if number of ops in response doesn't match commit :type type_: str :param type_: Type of commit operation: 'add' or 'delete' :type response_num: int :param response_num: Number of adds or deletes in the response. :raises: :class:`boto.cloudsearch.document.CommitMismatchError` csg|] }|dkr|qS)rr )rLr$type_r r rN s z1CommitResponse._check_num_ops..zs