o
    `f                     @   s,  d d Z d5ddZdd Zdd Zd	d
 Zdd Zdd Zd6ddZG dd dZG dd deZ	G dd deZ
G dd deZG dd deZG dd dZG dd deZG d d! d!eZG d"d# d#eZG d$d% d%eZG d&d' d'eZG d(d) d)eZd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4S )7c                  G   s   | st dt|  S )zx
    Indicate that the values should be matched to a tag field

    ### Parameters

    - **t**: Tags to search for
    z"At least one tag must be specified)
ValueErrorTagValue)t r   Z/home/ubuntu/webapp/venv/lib/python3.10/site-packages/redis/commands/search/querystring.pytags   s   r   Tc                 C   s   t | |||dS )z0
    Indicate that value is a numeric range
    )inclusive_mininclusive_max)
RangeValue)abr   r   r   r   r   between   s   r   c                 C   s
   t | | S )z
    Match a numeric value
    r   nr   r   r   equal   s   
r   c                 C      t d| ddS )z%
    Match any value less than n
    NFr   r   r   r   r   r   lt      r   c                 C   r   )z,
    Match any value less or equal to n
    NTr   r   r   r   r   r   le#   r   r   c                 C      t | dddS )z(
    Match any value greater than n
    NFr   r   r   r   r   r   gt*   r   r   c                 C   r   )z/
    Match any value greater or equal to n
    NTr   r   r   r   r   r   ge1   r   r   kmc                 C   s   t | |||S )z-
    Indicate that value is a geo region
    )GeoValue)latlonradiusunitr   r   r   geo8   r   r    c                   @   s4   e Zd Zedd Zedd Zdd Zdd Zd	S )
Valuec                 C   s   dS )z
        Whether this type of value may be combined with other values
        for the same field. This makes the filter potentially more efficient
        Fr   selfr   r   r   
combinable@   s   zValue.combinablec                 C   s   t | tr| S t| S )zL
        Convert an object to a value, if it is not a value already
        )
isinstancer!   ScalarValuevr   r   r   
make_valueH   s   
zValue.make_valuec                 C      t  NNotImplementedErrorr"   r   r   r   	to_stringQ      zValue.to_stringc                 C      |   S r+   r.   r"   r   r   r   __str__T      zValue.__str__N)	__name__
__module____qualname__propertyr$   staticmethodr)   r.   r2   r   r   r   r   r!   ?   s    

r!   c                   @   s"   e Zd ZdZdddZdd ZdS )r	   Fc                 C   s:   |d u rd}|d u rd}t |t |g| _|| _|| _d S )Nz-infinf)strranger   r   )r#   r
   r   r   r   r   r   r   __init__[   s   
zRangeValue.__init__c                 C   s&   d | j| js	dnd| jsdS dS )Nz[{1}{0[0]} {2}{0[1]}]( )formatr;   r   r   r"   r   r   r   r.   d   s   zRangeValue.to_stringN)FFr4   r5   r6   r$   r<   r.   r   r   r   r   r	   X   s    
	r	   c                   @       e Zd ZdZdd Zdd ZdS )r&   Tc                 C   s   t || _d S r+   )r:   r(   )r#   r(   r   r   r   r<   o      zScalarValue.__init__c                 C      | j S r+   r'   r"   r   r   r   r.   r   r/   zScalarValue.to_stringNr@   r   r   r   r   r&   l       r&   c                   @   rA   )r   Fc                 G   s
   || _ d S r+   )r   )r#   r   r   r   r   r<   y   s   
zTagValue.__init__c                 C   s   dd dd | jD  d S )N{z | c                 s   s    | ]}t |V  qd S r+   )r:   ).0r   r   r   r   	<genexpr>}       z%TagValue.to_string.<locals>.<genexpr>})joinr   r"   r   r   r   r.   |   s   zTagValue.to_stringNr@   r   r   r   r   r   v   rD   r   c                   @   s   e Zd ZdddZdd ZdS )r   r   c                 C   s   || _ || _|| _|| _d S r+   r   r   r   r   )r#   r   r   r   r   r   r   r   r<      s   
zGeoValue.__init__c              	   C   s&   d| j  d| j d| j d| j d	S )N[ ]rK   r"   r   r   r   r.      s   &zGeoValue.to_stringNr   )r4   r5   r6   r<   r.   r   r   r   r   r      s    
r   c                   @   sN   e Zd Zdd Zdd Zedd Zedd Zdd
dZ	dd Z
dd Zd	S )Nodec                 O   s   g | _ i }| D ]0\}}||g }t|tttfr$|t	| q	t|tr/|| q	|
dd |D  q	|  j dd |D 7  _ | D ]\}}| j 
| || qJdS )a  
        Create a node

        ### Parameters

        - **children**: One or more sub-conditions. These can be additional
            `intersect`, `disjunct`, `union`, `optional`, or any other `Node`
            type.

            The semantics of multiple conditions are dependent on the type of
            query. For an `intersection` node, this amounts to a logical AND,
            for a `union` node, this amounts to a logical `OR`.

        - **kwparams**: key-value parameters. Each key is the name of a field,
            and the value should be a field value. This can be one of the
            following:

            - Simple string (for text field matches)
            - value returned by one of the helper functions
            - list of either a string or a value


        ### Examples

        Field `num` should be between 1 and 10
        ```
        intersect(num=between(1, 10)
        ```

        Name can either be `bob` or `john`

        ```
        union(name=("bob", "john"))
        ```

        Don't select countries in Israel, Japan, or US

        ```
        disjunct_union(country=("il", "jp", "us"))
        ```
        c                 s   s    | ]}t |V  qd S r+   )r!   r)   )rF   subvr   r   r   rG      s    z Node.__init__.<locals>.<genexpr>c                 S   s   g | ]}t |qS r   )rP   to_node)rF   pr   r   r   
<listcomp>   s    z!Node.__init__.<locals>.<listcomp>N)paramsitems
setdefaultr%   r:   intfloatappendr!   r)   extendjoin_fields)r#   childrenkwparamskvparamskr(   curvalsr   r   r   r<      s   +
zNode.__init__c                    st   t |dkrtd  d|d   gS |d js# fdd|D S td  d| jdd	 |D  d
}|gS )N   @:    c                    s$   g | ]}t d   d|  qS )rc   rd   )BaseNoder.   rF   r(   keyr   r   rT      s   $ z$Node.join_fields.<locals>.<listcomp>z:(c                 s       | ]}|  V  qd S r+   r1   rg   r   r   r   rG      rH   z#Node.join_fields.<locals>.<genexpr>))lenrf   r.   r$   JOINSTRrJ   )r#   ri   valssr   rh   r   r\      s   
(zNode.join_fieldsc                 C   s   t |tr|S t|S r+   )r%   rP   rf   )clsobjr   r   r   rR      s   
zNode.to_nodec                 C   r*   r+   r,   r"   r   r   r   rm      s   zNode.JOINSTRNc                 C   s>   |  |}|r	dnd\}}| | jdd | jD  | S )N)r=   rk   )r>   r>   c                 s   rj   r+   r1   )rF   r   r   r   r   rG      rH   z!Node.to_string.<locals>.<genexpr>)_should_use_parenrm   rJ   rU   )r#   with_parensprepostr   r   r   r.      s   
$zNode.to_stringc                 C   s   |d ur|S t | jdkS )Nrb   )rl   rU   )r#   optvalr   r   r   rr      s   zNode._should_use_parenc                 C   r0   r+   r1   r"   r   r   r   r2      r3   zNode.__str__r+   )r4   r5   r6   r<   r\   classmethodrR   r7   rm   r.   rr   r2   r   r   r   r   rP      s    <


rP   c                       s&   e Zd Z fddZdddZ  ZS )rf   c                    s   t    t|| _d S r+   )superr<   r:   ro   )r#   ro   	__class__r   r   r<      s   
zBaseNode.__init__Nc                 C   rC   r+   )ro   )r#   rs   r   r   r   r.      r/   zBaseNode.to_stringr+   )r4   r5   r6   r<   r.   __classcell__r   r   ry   r   rf      s    rf   c                   @      e Zd ZdZdZdS )IntersectNodezw
    Create an intersection node. All children need to be satisfied in order for
    this node to evaluate as true
    rM   Nr4   r5   r6   __doc__rm   r   r   r   r   r}          r}   c                   @   r|   )	UnionNodezv
    Create a union node. Any of the children need to be satisfied in order for
    this node to evaluate as true
    |Nr~   r   r   r   r   r      r   r   c                       "   e Zd ZdZd fdd	Z  ZS )DisjunctNodezs
    Create a disjunct node. In order for this node to be true, all of its
    children must evaluate to false
    Nc                    0   |  |}t jdd}|rd| d S d| S )NFrs   z(-rk   -rr   rx   r.   r#   rs   retry   r   r   r.   	  
   
zDisjunctNode.to_stringr+   r4   r5   r6   r   r.   r{   r   r   ry   r   r         r   c                   @   r|   )DistjunctUnionz~
    This node is true if *all* of its children are false. This is equivalent to
    ```
    disjunct(union(...))
    ```
    r   Nr~   r   r   r   r   r     s    r   c                       r   )OptionalNodez}
    Create an optional node. If this nodes evaluates to true, then the document
    will be rated higher in score/rank.
    Nc                    r   )NFr   z(~rk   ~r   r   ry   r   r   r.   #  r   zOptionalNode.to_stringr+   r   r   r   ry   r   r     r   r   c                  O      t | i |S r+   )r}   argskwargsr   r   r   	intersect,  rB   r   c                  O   r   r+   )r   r   r   r   r   union0  rB   r   c                  O   r   r+   )r   r   r   r   r   disjunct4  rB   r   c                  O   r   r+   )r   r   r   r   r   disjunct_union8  rB   r   c                  O   s   t | i | S r+   )r   r.   r   r   r   r   querystring<  s   r   N)TTrO   )r   r   r   r   r   r   r   r    r!   r	   r&   r   r   rP   rf   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s2    



]			