o ckF[^?@sddlmZddlmZmZmZddlZddlmZdZ dZ dZ dZ Gd d d e ZGd d d eZGd ddeZGdddeZdS))ceil)jsonmapsixN)CloudSearchDomainConnectionsimple structuredlucenedismaxc@s eZdZdS)SearchServiceExceptionN)__name__ __module__ __qualname__rr:/usr/lib/python3/dist-packages/boto/cloudsearch2/search.pyr "sr c@s,eZdZddZddZddZddZd S) SearchResultsc Ks|dd|_|dd|_|dd|_|dd|_|dd|_|d|_|d |_i|_d |vrX|d D]\}}d |vrWt d d t dd| d gD|j|<q:t |j|jj |_dS)Nstatusridztime-mshitsfoundhitstartquerysearch_servicefacetsbucketscss|] \}}||fVqdSNr).0kvrrr 4sz)SearchResults.__init__..cSs|d|dfS)Nvaluecountr)xrrr4sz(SearchResults.__init__..)rtime_msrdocsrrrritemsdictrgetr real_sizenum_pages_needed)selfattrsfacetvaluesrrr__init__'s  *zSearchResults.__init__cC t|jSr)lenr&r,rrr__len__8 zSearchResults.__len__cCr1r)iterr&r3rrr__iter__;r5zSearchResults.__iter__cCsB|jj|jkr|jj|jj7_|jjd7_||jSt)zCall Cloudsearch to get the next page of search results :rtype: :class:`boto.cloudsearch2.search.SearchResults` :return: the following page of search results )rpager+rr*r StopIterationr3rrr next_page>s  zSearchResults.next_pageN)r r rr0r4r7r;rrrrr&s  rc@s8eZdZdZ   d ddZddZd d Zd d ZdS)QueryiN rc Csj||_||_||_|p i|_|pg|_|pg|_||_| pi|_| p#i|_| |_ | |_ d|_ | |dSNr) qparserfqexprsort return_fieldsrr. highlightpartialoptionsr9 update_size) r,r?r@rArBrDsizerrCr.rErFrGrrrr0Ps     zQuery.__init__cCs4||_|jtjks|jdkrtj|_dS|j|_dSr>)rIr<RESULTS_PER_PAGEr*)r,new_sizerrrrHbs  zQuery.update_sizecCs |j|jd}|jr|j|d<|jr|j|d<|jr|j|d<|jr3t|jD] \}}||d|<q(|jrRt|jD]\}}t |tj sKt |}||d|<q<|j rft|j D] \}}||d|<q[|jrn|j|d<|jryd |j|d <|jd ur|j|d <|jrd |j|d <|S)zTransform search parameters from instance properties to a dictionary :rtype: dict :return: search parameters rrIr?zq.parserrAexpr.%sfacet.%s highlight.%sz q.options,returnNrFrCrr*r?r@rArBr iteritemsr. isinstance string_typesrdumpsrErGrDjoinrFrC)r,paramsrrrrr to_paramsgs6        zQuery.to_paramscCsD|j|jd}|jr|j|d<|jr|j|d<|jr|j|d<|jr9i}t|jD] \}}||d|<q*||d<|jr^i}t|jD]\}}t |tj sSt |}||d|<qD||d<|j rxi}t|j D] \}}||d |<qi||d <|jr|j|d <|jrd |j|d <|jdur|j|d<|jrd |j|d<|S)z Transform search parameters from instance properties to a dictionary that CloudSearchDomainConnection can accept :rtype: dict :return: search parameters rLr? query_parser filter_queryrMrBrNr.rOrE query_optionsrPretNrFrCrR)r,rXrBrrr.rErrrto_domain_connection_paramssB        z!Query.to_domain_connection_params NNNNNr=rNNNNN)r r rrJr0rHrYr^rrrrr<Ls  -r<c@sfeZdZdddZ   dddZ   ddd Zd d Zd d ZddZddZ ddZ ddZ dS)SearchConnectionNcCs||_||_t|_|s|j|_d|_|jrK|jjrM|jjjr*|jj |jj d<t |jjdd|_|jrO|jj}t |j|j |j|j|jd|_dSdSdSdS)NFhttp sign_request)hostaws_access_key_idaws_secret_access_keyregionprovider)domainendpointrequestsSessionsessionsearch_service_endpointrblayer1 use_proxyget_proxy_url_with_authproxiesgetattrrrdrerfrgdomain_connection)r,rhrirnrrrr0s*   zSearchConnection.__init__r=rc Cs t||||||||| | | | d S)N) r?r@rArBrDrIrr.rErCrFrG)r<) r,r?r@rArankrDrIrr.rErCrFrGrrr 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 parser: string :param parser: The parser to use. 'simple', 'structured', 'lucene', 'dismax' :type fq: string :param fq: The filter query to use. :type sort: List of strings :param sort: A list of fields or rank expressions used to order the search results. Order is handled by adding 'desc' or 'asc' after the field name. ``['year desc', 'author asc']`` :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: dict :param facet: Dictionary of fields for which facets should be returned The facet value is string of JSON options ``{'year': '{sort:"bucket", size:3}', 'genres': '{buckets:["Action","Adventure","Sci-Fi"]}'}`` :type highlight: dict :param highlight: Dictionary of fields for which highlights should be returned The facet value is string of JSON options ``{'genres': '{format:'text',max_phrases:2,pre_tag:'',post_tag:''}'}`` :type partial: bool :param partial: Should partial results from a partioned service be returned if one or more index partitions are unreachable. :type options: str :param options: Options for the query parser specified in *parser*. Specified as a string in JSON format. ``{fields: ['title^5', 'description']}`` :rtype: :class:`boto.cloudsearch2.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(q="'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(q="(and 'Tim' (field author 'John Smith'))", parser='structured') Facets allow you to show classification information about the search results. For example, you can retrieve the authors who have written about Tim with a max of 3 >>> search(q='Tim', facet={'Author': '{sort:"bucket", size:3}'}) ) r?r@rArtrDrIrr.rErCrFrG)ru)r,r?r@rArtrDrIrr.rErCrFrGrrrrsearchs ZzSearchConnection.searchcCs|jj|ddfi|S)Nr?)rsrvpop)r,rXrrr_search_with_authRsz"SearchConnection._search_with_authcCs2d|j|f}|jj||d}|jd|jdS)Nzhttp://%s/%s/search)rXzutf-8)body status_code)rirlr)contentdecoder{)r,rX api_versionurlresprrr_search_without_authUsz%SearchConnection._search_without_authc CsRd}|jr|jjr|jjj}|jr||}nR|||}|d}|d}zt |}Wn:t yj|dkrcd}ddl }| d|} z d | d}Wn ty\Ynwtd |td ||wd |vrd |vr|d D]} | ddkrtdt| df|qwnd |vrtdt||||d<||d<tdi|S)zMake a call to CloudSearch :type query: :class:`boto.cloudsearch2.search.Query` :param query: A group of search criteria :rtype: :class:`boto.cloudsearch2.search.SearchResults` :return: search results z 2013-01-01rzr{irwrNz*

403 Forbidden

([^<]+) %smessagez"Unknown error processing search %srrr)rhrn APIVersionrbryr^rrYrloads ValueErrorrervgroupsstripAttributeErrorr rXrVr) r,rr~datar_body _status_codemsgrgmrrr__call__[sT         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.cloudsearch2.search.Query` :param query: A group of search criteria :type per_page: int :param per_page: Number of docs in each :class:`boto.cloudsearch2.search.SearchResults` object. :rtype: generator :return: Generator containing :class:`boto.cloudsearch2.search.SearchResults` rr8N)rHr+rr*)r,rper_pager9r+resultsrrr get_all_pageds 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.cloudsearch2.search.Query` :param query: A group of search criteria :rtype: generator :return: All docs matching query rr8N)r+rr*)r,rr9r+rdocrrr get_all_hitsszSearchConnection.get_all_hitscCs|d||jS)zReturn the total number of hits for query :type query: :class:`boto.cloudsearch2.search.Query` :param query: a group of search criteria :rtype: int :return: Total number of hits for query r8)rHr)r,rrrr get_num_hitss zSearchConnection.get_num_hits)NNr_) r r rr0rurvryrrrrrrrrrr`s    a1 r`)mathr boto.compatrrrrjboto.cloudsearchdomain.layer1rSIMPLE STRUCTUREDLUCENEDISMAX Exceptionr objectrr<r`rrrrs  &