o ckF[5@s|ddlmZddlmZmZmZddlZGdddeZGdddeZ Gdd d e Z Gd d d e Z Gd d d e Z dS))ceil)jsonmapsixNc@ eZdZdS)SearchServiceExceptionN__name__ __module__ __qualname__r r 9/usr/lib/python3/dist-packages/boto/cloudsearch/search.pyrrc@r)CommitMismatchErrorNrr r r r r!rrc@s,eZdZddZddZddZddZd S) SearchResultscKs|dd|_|dd|_|dd|_|dd|_|dd|_|dd|_|d |_|d |_|d |_|d |_ i|_ d |vrg|d  D]\}}d|vrft ddt dd|dD|j |<qKt|j|jj|_dS)Ninforidz cpu-time-msztime-mshitsfoundhitstartrankz match-exprquerysearch_servicefacets constraintscss|] \}}||fVqdSNr ).0kvr r r 7sz)SearchResults.__init__..cSs|d|dfS)Nvaluecountr )xr r r 7sz(SearchResults.__init__..)r cpu_time_mstime_msrdocsrrmatch_expressionrrritemsdictrr real_sizenum_pages_needed)selfattrsfacetvaluesr r r __init__&s"    &zSearchResults.__init__cC t|jSr)lenr'r-r r r __len__; zSearchResults.__len__cCr2r)iterr'r4r r r __iter__>r6zSearchResults.__iter__cCsB|jj|jkr|jj|jj7_|jjd7_||jSt)zCall Cloudsearch to get the next page of search results :rtype: :class:`boto.cloudsearch.search.SearchResults` :return: the following page of search results )rpager,rr+r StopIterationr4r r r next_pageAs  zSearchResults.next_pageN)r r r r1r5r8r<r r r r r%s  rc@s2eZdZdZ    d ddZddZd d ZdS) QueryiN rc Csl||_||_|p g|_|pg|_||_|pg|_|pi|_| p i|_| p%i|_| p*i|_ d|_ | |dSNr) qbqr return_fieldsrr/facet_constraints facet_sort facet_top_ntr: update_size r-r@rArrBsizerr/rCrDrErFr r r r1Ss       zQuery.__init__cCs4||_|jtjks|jdkrtj|_dS|j|_dSr?)rIr=RESULTS_PER_PAGEr+)r-new_sizer r r rGes  zQuery.update_sizecCs|j|jd}|jr|j|d<|jr|j|d<|jr"d|j|d<|jr-d|j|d<|jr8d|j|d<|jrLt |jD] \}}||d|<qA|j r`t |j D] \}}||d |<qU|j rtt |j D] \}}||d |<qi|j rt |j D] \}}||d |<q}|S) zTransform search parameters from instance properties to a dictionary :rtype: dict :return: search parameters )rrIr@rA,rz return-fieldsr/zfacet-%s-constraintsz facet-%s-sortzfacet-%s-top-nzt-%s)rr+r@rArjoinrBr/rCr iteritemsrDrErF)r-paramsrrr r r to_paramsjs0  zQuery.to_params NNNNr>rNNNNN)r r r rJr1rGrPr r r r r=Os  r=c@sVeZdZdddZ   dddZ   ddd Zd d Zd d ZddZddZ dS)SearchConnectionNcCs ||_||_|s|j|_dSdSr)domainendpointsearch_service_endpoint)r-rSrTr r r r1s  zSearchConnection.__init__r>rc Cst||||||||| | | d S)N r@rArrBrIrr/rCrDrErF)r=rHr r r build_querys zSearchConnection.build_queryc Cs(|j||||||||| | | d } || S)a Send a query to CloudSearch Each search query should use at least the q or bq argument to specify the search parameter. The other options are used to specify the criteria of the search. :type q: string :param q: A string to search the default search fields for. :type bq: string :param bq: A string to perform a Boolean search. This can be used to create advanced searches. :type rank: List of strings :param rank: A list of fields or rank expressions used to order the search results. A field can be reversed by using the - operator. ``['-year', 'author']`` :type return_fields: List of strings :param return_fields: A list of fields which should be returned by the search. If this field is not specified, only IDs will be returned. ``['headline']`` :type size: int :param size: Number of search results to specify :type start: int :param start: Offset of the first search result to return (can be used for paging) :type facet: list :param facet: List of fields for which facets should be returned ``['colour', 'size']`` :type facet_constraints: dict :param facet_constraints: Use to limit facets to specific values specified as comma-delimited strings in a Dictionary of facets ``{'colour': "'blue','white','red'", 'size': "big"}`` :type facet_sort: dict :param facet_sort: Rules used to specify the order in which facet values should be returned. Allowed values are *alpha*, *count*, *max*, *sum*. Use *alpha* to sort alphabetical, and *count* to sort the facet by number of available result. ``{'color': 'alpha', 'size': 'count'}`` :type facet_top_n: dict :param facet_top_n: Dictionary of facets and number of facets to return. ``{'colour': 2}`` :type t: dict :param t: Specify ranges for specific fields ``{'year': '2000..2005'}`` :rtype: :class:`boto.cloudsearch.search.SearchResults` :return: Returns the results of this search The following examples all assume we have indexed a set of documents with fields: *author*, *date*, *headline* A simple search will look for documents whose default text search fields will contain the search word exactly: >>> search(q='Tim') # Return documents with the word Tim in them (but not Timothy) A simple search with more keywords will return documents whose default text search fields contain the search strings together or separately. >>> search(q='Tim apple') # Will match "tim" and "apple" More complex searches require the boolean search operator. Wildcard searches can be used to search for any words that start with the search string. >>> search(bq="'Tim*'") # Return documents with words like Tim or Timothy) Search terms can also be combined. Allowed operators are "and", "or", "not", "field", "optional", "token", "phrase", or "filter" >>> search(bq="(and 'Tim' (field author 'John Smith'))") Facets allow you to show classification information about the search results. For example, you can retrieve the authors who have written about Tim: >>> search(q='Tim', facet=['Author']) With facet_constraints, facet_top_n and facet_sort more complicated constraints can be specified such as returning the top author out of John Smith and Mark Smith who have a document with the word Tim in it. >>> search(q='Tim', ... facet=['Author'], ... facet_constraints={'author': "'John Smith','Mark Smith'"}, ... facet=['author'], ... facet_top_n={'author': 1}, ... facet_sort={'author': 'count'}) rV)rW) r-r@rArrBrIrr/rCrDrErFrr r r searchs izSearchConnection.searchc Cs8d|j}|}tj||d}|jd}zt|}Wn@ty]}z4|j dkrRd}ddl } | d|} z d | d }Wn tyKYnwtd |td ||d}~wwd |vrd |vr|d D]} | ddkr}td|| df|qjnd |vrtdt||||d<||d<tdi|S)zMake a call to CloudSearch :type query: :class:`boto.cloudsearch.search.Query` :param query: A group of search criteria :rtype: :class:`boto.cloudsearch.search.SearchResults` :return: search results zhttp://%s/2011-02-01/search)rOzutf-8irNz*

403 Forbidden

([^<]+) %smessagez"Unknown error processing search %srrr )rTrPrequestsgetcontentdecoderloads ValueError status_codererXgroupsstripAttributeErrorrdumpsr) r-rurlrOrbodydataemsgrfgmr r r __call__sN         zSearchConnection.__call__ccsX||d}d}||kr*||}|j}|V|j|j7_|d7}||ksdSdS)aGet a generator to iterate over all pages of search results :type query: :class:`boto.cloudsearch.search.Query` :param query: A group of search criteria :type per_page: int :param per_page: Number of docs in each :class:`boto.cloudsearch.search.SearchResults` object. :rtype: generator :return: Generator containing :class:`boto.cloudsearch.search.SearchResults` rr9N)rGr,rr+)r-rper_pager:r,resultsr r r get_all_paged>s zSearchConnection.get_all_pagedccsXd}d}||kr*||}|j}|D]}|Vq|j|j7_|d7}||ks dSdS)aGet a generator to iterate over all search results Transparently handles the results paging from Cloudsearch search results so even if you have many thousands of results you can iterate over all results in a reasonably efficient manner. :type query: :class:`boto.cloudsearch.search.Query` :param query: A group of search criteria :rtype: generator :return: All docs matching query rr9N)r,rr+)r-rr:r,rudocr r r get_all_hitsTszSearchConnection.get_all_hitscCs|d||jS)zReturn the total number of hits for query :type query: :class:`boto.cloudsearch.search.Query` :param query: a group of search criteria :rtype: int :return: Total number of hits for query r9)rGr)r-rr r r get_num_hitsls zSearchConnection.get_num_hits)NNrQ) r r r r1rWrXrsrvrxryr r r r rRs   q* rR)mathr boto.compatrrrr_ Exceptionrrobjectrr=rRr r r r s *D