U
    3d<                     @   sx   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d	d
 ZG dd deZdS )    N)json)OptionStatusIndexFieldStatus)ServicePoliciesStatusRankExpressionStatusDocumentServiceConnectionSearchConnectionc                 C   s   | dkrdS dS )N)TtrueTrueTRUE   TF )valuer   r   ;/tmp/pip-unpacked-wheel-d7dsrkjd/boto/cloudsearch/domain.pyhandle_bool"   s    r   c                   @   s  e Zd ZdZdd Zdd Zedd Zedd	 Zed
d Z	edd Z
edd Zejdd Zedd Zejdd Zedd Zejdd Zedd Zejdd Zedd Zejdd Zedd Zejdd Zed d! Zejd"d! Zed#d$ Zed%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 ZdDd4d5Zd6d7d7d7g fd8d9ZdEd:d;Zd<d= Zd>d? Zd@dA Z dBdC Z!d3S )FDomaina  
    A Cloudsearch domain.

    :ivar name: The name of the domain.

    :ivar id: The internally generated unique identifier for the domain.

    :ivar created: A boolean which is True if the domain is
        created. It can take several minutes to initialize a domain
        when CreateDomain is called. Newly created search domains are
        returned with a False value for Created until domain creation
        is complete

    :ivar deleted: A boolean which is True if the search domain has
        been deleted. The system must clean up resources dedicated to
        the search domain when delete is called. Newly deleted
        search domains are returned from list_domains with a True
        value for deleted for several minutes until resource cleanup
        is complete.

    :ivar processing: True if processing is being done to activate the
        current domain configuration.

    :ivar num_searchable_docs: The number of documents that have been
        submittted to the domain and indexed.

    :ivar requires_index_document: True if index_documents needs to be
        called to activate the current domain configuration.

    :ivar search_instance_count: The number of search instances that are
        available to process search requests.

    :ivar search_instance_type: The instance type that is being used to
        process search requests.

    :ivar search_partition_count: The number of partitions across which
        the search index is spread.
    c                 C   s   || _ | | d S N)layer1update_from_data)selfr   datar   r   r   __init__P   s    zDomain.__init__c                 C   s   |d | _ |d | _|d | _|d | _|d | _|d | _|d | _|d | _|d	d | _	|d
 | _
|d | _|d | _d S )Ncreateddeleted
processingrequires_index_documents	domain_iddomain_namenum_searchable_docssearch_instance_countsearch_instance_typesearch_partition_countZdoc_serviceZsearch_service)r   r   r   r   r    r!   r"   r#   getr$   r%   _doc_service_search_service)r   r   r   r   r   r   T   s    









zDomain.update_from_datac                 C   s
   | j d S NZarnr'   r   r   r   r   doc_service_arnb   s    zDomain.doc_service_arnc                 C   s
   | j d S NZendpointr*   r+   r   r   r   doc_service_endpointf   s    zDomain.doc_service_endpointc                 C   s
   | j d S r)   r(   r+   r   r   r   search_service_arnj   s    zDomain.search_service_arnc                 C   s
   | j d S r-   r/   r+   r   r   r   search_service_endpointn   s    zDomain.search_service_endpointc                 C   s   | j S r   )_createdr+   r   r   r   r   r   s    zDomain.createdc                 C   s   t || _d S r   )r   r2   r   r   r   r   r   r   v   s    c                 C   s   | j S r   )_deletedr+   r   r   r   r   z   s    zDomain.deletedc                 C   s   t || _d S r   )r   r4   r3   r   r   r   r   ~   s    c                 C   s   | j S r   )_processingr+   r   r   r   r      s    zDomain.processingc                 C   s   t || _d S r   )r   r5   r3   r   r   r   r      s    c                 C   s   | j S r   )_requires_index_documentsr+   r   r   r   r      s    zDomain.requires_index_documentsc                 C   s   t || _d S r   )r   r6   r3   r   r   r   r      s    c                 C   s   | j S r   )_search_partition_countr+   r   r   r   r%      s    zDomain.search_partition_countc                 C   s   t || _d S r   )intr7   r3   r   r   r   r%      s    c                 C   s   | j S r   )_search_instance_countr+   r   r   r   r#      s    zDomain.search_instance_countc                 C   s   t || _d S r   )r8   r9   r3   r   r   r   r#      s    c                 C   s   | j S r   )_num_searchable_docsr+   r   r   r   r"      s    zDomain.num_searchable_docsc                 C   s   t || _d S r   )r8   r:   r3   r   r   r   r"      s    c                 C   s   | j S r   r!   r+   r   r   r   name   s    zDomain.namec                 C   s   | j S r   )r    r+   r   r   r   id   s    z	Domain.idc                 C   s   | j | jS )zK
        Delete this domain and all index data associated with it.
        )r   Zdelete_domainr<   r+   r   r   r   delete   s    zDomain.deletec                 C   s   t | d| jj| jjS )z
        Return a :class:`boto.cloudsearch.option.OptionStatus` object
        representing the currently defined stemming options for
        the domain.
        N)r   r   Zdescribe_stemming_optionsZupdate_stemming_optionsr+   r   r   r   get_stemming   s    zDomain.get_stemmingc                 C   s   t | d| jj| jjS )z
        Return a :class:`boto.cloudsearch.option.OptionStatus` object
        representing the currently defined stopword options for
        the domain.
        N)r   r   Zdescribe_stopword_optionsZupdate_stopword_optionsr+   r   r   r   get_stopwords   s    zDomain.get_stopwordsc                 C   s   t | d| jj| jjS )z
        Return a :class:`boto.cloudsearch.option.OptionStatus` object
        representing the currently defined synonym options for
        the domain.
        N)r   r   Zdescribe_synonym_optionsZupdate_synonym_optionsr+   r   r   r   get_synonyms   s    zDomain.get_synonymsc                 C   s   t | d| jj| jjS )z
        Return a :class:`boto.cloudsearch.option.OptionStatus` object
        representing the currently defined access policies for
        the domain.
        N)r   r   Z describe_service_access_policiesZupdate_service_access_policiesr+   r   r   r   get_access_policies   s    zDomain.get_access_policiesc                 C   s   | j | j dS )a)  
        Tells the search domain to start indexing its documents using
        the latest text processing options and IndexFields. This
        operation must be invoked to make options whose OptionStatus
        has OptioState of RequiresIndexDocuments visible in search
        results.
        N)r   index_documentsr<   r+   r   r   r   rC      s    zDomain.index_documentsNc                    s"    j  j|} fdd|D S )zH
        Return a list of index fields defined for this domain.
        c                    s   g | ]}t  |qS r   r   .0dr+   r   r   
<listcomp>   s     z+Domain.get_index_fields.<locals>.<listcomp>)r   describe_index_fieldsr<   )r   field_namesr   r   r+   r   get_index_fields   s    zDomain.get_index_fields Fc           	   
   C   s.   | j j| j|||||||d}t| || j jS )a  
        Defines an ``IndexField``, either replacing an existing
        definition or creating a new one.

        :type field_name: string
        :param field_name: The name of a field in the search index.

        :type field_type: string
        :param field_type: The type of field.  Valid values are
            uint | literal | text

        :type default: string or int
        :param default: The default value for the field.  If the
            field is of type ``uint`` this should be an integer value.
            Otherwise, it's a string.

        :type facet: bool
        :param facet: A boolean to indicate whether facets
            are enabled for this field or not.  Does not apply to
            fields of type ``uint``.

        :type results: bool
        :param results: A boolean to indicate whether values
            of this field can be returned in search results or
            used in ranking.  Does not apply to fields of type ``uint``.

        :type searchable: bool
        :param searchable: A boolean to indicate whether search
            is enabled for this field or not.  Applies only to fields
            of type ``literal``.

        :type source_attributes: list of dicts
        :param source_attributes: An optional list of dicts that
            provide information about attributes for this index field.
            A maximum of 20 source attributes can be configured for
            each index field.

            Each item in the list is a dict with the following keys:

            * data_copy - The value is a dict with the following keys:
                * default - Optional default value if the source attribute
                    is not specified in a document.
                * name - The name of the document source field to add
                    to this ``IndexField``.
            * data_function - Identifies the transformation to apply
                when copying data from a source attribute.
            * data_map - The value is a dict with the following keys:
                * cases - A dict that translates source field values
                    to custom values.
                * default - An optional default value to use if the
                    source attribute is not specified in a document.
                * name - the name of the document source field to add
                    to this ``IndexField``
            * data_trim_title - Trims common title words from a source
                document attribute when populating an ``IndexField``.
                This can be used to create an ``IndexField`` you can
                use for sorting.  The value is a dict with the following
                fields:
                * default - An optional default value.
                * language - an IETF RFC 4646 language code.
                * separator - The separator that follows the text to trim.
                * name - The name of the document source field to add.

        :raises: BaseException, InternalException, LimitExceededException,
            InvalidTypeException, ResourceNotFoundException
        )defaultfacetresult
searchablesource_attributes)r   Zdefine_index_fieldr<   r   rH   )	r   
field_nameZ
field_typerL   rM   rN   rO   rP   r   r   r   r   create_index_field   s    E  zDomain.create_index_fieldc                    s(   j j  j|} fdd|D S )zL
        Return a list of rank expressions defined for this domain.
        c                    s   g | ]}t | qS r   r   rD   fnr   r   r   rG   D  s     z/Domain.get_rank_expressions.<locals>.<listcomp>)r   describe_rank_expressionsr<   )r   Z
rank_namesr   r   rS   r   get_rank_expressions>  s    zDomain.get_rank_expressionsc                 C   s"   | j | j||}t| || j jS )a
  
        Create a new rank expression.
        
        :type rank_name: string
        :param rank_name: The name of an expression computed for ranking
            while processing a search request.

        :type rank_expression: string
        :param rank_expression: The expression to evaluate for ranking
            or thresholding while processing a search request. The
            RankExpression syntax is based on JavaScript expressions
            and supports:

            * Integer, floating point, hex and octal literals
            * Shortcut evaluation of logical operators such that an
                expression a || b evaluates to the value a if a is
                true without evaluting b at all
            * JavaScript order of precedence for operators
            * Arithmetic operators: + - * / %
            * Boolean operators (including the ternary operator)
            * Bitwise operators
            * Comparison operators
            * Common mathematic functions: abs ceil erf exp floor
                lgamma ln log2 log10 max min sqrt pow
            * Trigonometric library functions: acosh acos asinh asin
                atanh atan cosh cos sinh sin tanh tan
            * Random generation of a number between 0 and 1: rand
            * Current time in epoch: time
            * The min max functions that operate on a variable argument list

            Intermediate results are calculated as double precision
            floating point values. The final return value of a
            RankExpression is automatically converted from floating
            point to a 32-bit unsigned integer by rounding to the
            nearest integer, with a natural floor of 0 and a ceiling
            of max(uint32_t), 4294967295. Mathematical errors such as
            dividing by 0 will fail during evaluation and return a
            value of 0.

            The source data for a RankExpression can be the name of an
            IndexField of type uint, another RankExpression or the
            reserved name text_relevance. The text_relevance source is
            defined to return an integer from 0 to 1000 (inclusive) to
            indicate how relevant a document is to the search request,
            taking into account repetition of search terms in the
            document and proximity of search terms to each other in
            each matching IndexField in the document.

            For more information about using rank expressions to
            customize ranking, see the Amazon CloudSearch Developer
            Guide.

        :raises: BaseException, InternalException, LimitExceededException,
            InvalidTypeException, ResourceNotFoundException
        )r   Zdefine_rank_expressionr<   r   rU   )r   r<   Z
expressionr   r   r   r   create_rank_expressionF  s    8zDomain.create_rank_expressionc                 C   s
   t | dS N)domainr	   r+   r   r   r   get_document_service  s    zDomain.get_document_servicec                 C   s
   t | dS rX   r   r+   r   r   r   get_search_service  s    zDomain.get_search_servicec                 C   s
   d| j  S )Nz<Domain: %s>r;   r+   r   r   r   __repr__  s    zDomain.__repr__)N)N)"__name__
__module____qualname____doc__r   r   propertyr,   r.   r0   r1   r   setterr   r   r   r%   r#   r"   r<   r=   r>   r?   r@   rA   rB   rC   rJ   rR   rV   rW   rZ   r[   r\   r   r   r   r   r   (   sz   '

























   
M
<r   )ZbotoZboto.compatr   Zboto.cloudsearch.optionstatusr   r   r   r   Zboto.cloudsearch.documentr
   Zboto.cloudsearch.searchr   r   objectr   r   r   r   r   <module>   s   