
    +h,-                         d dl mZmZmZ  G d d      Z G d d      Z G d de      Z G d d	e      Z G d
 d      Zy)    )ListOptionalUnionc                      e Zd ZdZdeddfdZdefdZd/dZd/dZd0d	ed
e	e   dd fdZ
dee   defdZ	 	 	 	 d1de	e   de	e   de	e   de	e   dd f
dZ	 d2de	ee      de	ee      ddfdZdedd fdZdedd fdZdedd fdZd/dZdedd fdZdee   fdZdee   fdZded edd fd!Zd/d"Zd/d#Zd/d$Zd/d%Zd/d&Zdee   dd fd'Zd3d(Zd4d	ed)e dd fd*Z!d+edd fd,Z"d-edd fd.Z#y)5QueryaB  
    Query is used to build complex queries that have more parameters than just
    the query string. The query string is set in the constructor, and other
    options have setter functions.

    The setter functions return the query object, so they can be chained,
    i.e. `Query("foo").verbatim().filter(...)` etc.
    query_stringreturnNc                 H   || _         d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _	        t               | _        d| _        d| _        d| _        d| _        d| _        g | _        g | _        g | _        d| _        d| _        d| _        y)z
        Create a new query object.
        The query string is set in the constructor, and other options have
        setter functions.
        r   
   FN)_query_string_offset_num_no_content_no_stopwords_fields	_verbatim_with_payloads_with_scores_scorerlist_filters_ids_slop_timeout	_in_order_sortby_return_fields_summarize_fields_highlight_fields	_language	_expander_dialect)selfr   s     N/var/www/html/venv/lib/python3.12/site-packages/redis/commands/search/query.py__init__zQuery.__init__   s     #/	!&#(,0$$)"'&*"f)-	
)-$.2$&')')(,(,'+    c                     | j                   S )z+Return the query string of this query only.)r   r$   s    r%   r   zQuery.query_string,   s    !!!r'   c                     || _         | S )zTLimit the results to a specific set of pre-known document
        ids of any length.)r   )r$   idss     r%   	limit_idszQuery.limit_ids0   s     	r'   c                 0    | xj                   |z  c_         | S )zAdd fields to return fields.)r   r$   fieldss     r%   return_fieldszQuery.return_fields6   s    v%r'   fieldas_fieldc                 n    | j                   j                  |       || xj                   d|fz  c_         | S )zJAdd field to return fields (Optional: add 'AS' name
        to the field).AS)r   append)r$   r1   r2   s      r%   return_fieldzQuery.return_field;   s9     	""5)D(#33r'   r/   c                 F    |sg S t        |t              r|gS t        |      S N)
isinstancestrr   r.   s     r%   _mk_field_listzQuery._mk_field_listC   s$    I%fc2xDVDr'   context_len	num_fragssepc                     dg}| j                  |      }|r|dt        t        |            g|z   z  }||dt        |      gz  }||dt        |      gz  }||d|gz  }|| _        | S )a  
        Return an abridged format of the field, containing only the segments of
        the field which contain the matching term(s).

        If `fields` is specified, then only the mentioned fields are
        summarized; otherwise all results are summarized.

        Server side defaults are used for each option (except `fields`)
        if not specified

        - **fields** List of fields to summarize. All fields are summarized
        if not specified
        - **context_len** Amount of context to include with each fragment
        - **num_frags** Number of fragments per document
        - **sep** Separator string to separate fragments
        	SUMMARIZEFIELDSLENFRAGS	SEPARATOR)r;   r:   lenr   )r$   r/   r<   r=   r>   argss         r%   	summarizezQuery.summarizeH   s    . }$$V,Xs3v;/0699D"UC,--D Wc)n--D?[#&&D!%r'   tagsc                     dg}| j                  |      }|r|dt        t        |            g|z   z  }|r|dgt        |      z   z  }|| _        | S )a  
        Apply specified markup to matched term(s) within the returned field(s).

        - **fields** If specified then only those mentioned fields are
        highlighted, otherwise all fields are highlighted
        - **tags** A list of two strings to surround the match.
        	HIGHLIGHTrA   TAGS)r;   r:   rE   r   r    )r$   r/   rH   rF   s       r%   	highlightzQuery.highlightn   s`     }$$V,Xs3v;/0699DVHtDz))D!%r'   languagec                     || _         | S )z
        Analyze the query as being in the specified language.

        :param language: The language (e.g. `chinese` or `english`)
        )r!   )r$   rM   s     r%   rM   zQuery.language        "r'   slopc                     || _         | S )zpAllow a maximum of N intervening non matched terms between
        phrase terms (0 means exact phrase).
        )r   )r$   rP   s     r%   rP   z
Query.slop   s     
r'   timeoutc                     || _         | S )z-overrides the timeout parameter of the module)r   )r$   rR   s     r%   rR   zQuery.timeout   s    r'   c                     d| _         | S )z
        Match only documents where the query terms appear in
        the same order in the document.
        i.e. for the query "hello world", we do not match "world hello"
        T)r   r)   s    r%   in_orderzQuery.in_order   s     r'   scorerc                     || _         | S )z
        Use a different scoring function to evaluate document relevance.
        Default is `TFIDF`.

        :param scorer: The scoring function to use
                       (e.g. `TFIDF.DOCNORM` or `BM25`)
        )r   )r$   rV   s     r%   rV   zQuery.scorer   s     r'   c                     | j                   g}|| j                         z  }|| j                  | j                  z   z  }|d| j                  | j
                  gz  }|S )z:Format the redis arguments for this query and return them.LIMIT)r   _get_args_tagsr   r    r   r   )r$   rF   s     r%   get_argszQuery.get_args   s\    ""###%%&&)?)???$,,		22r'   c                    g }| j                   r|j                  d       | j                  rD|j                  d       |j                  t        | j                               || j                  z  }| j                  r|j                  d       | j
                  r|j                  d       | j                  r;| j                  D ],  }t        |t              st        d      ||j                  z  }. | j                  r|j                  d       | j                  r|d| j                  gz  }| j                  r|j                  d       | j                  rD|j                  d	       |j                  t        | j                               || j                  z  }| j                  d
k\  r|d| j                  gz  }| j                   |d| j                   gz  }| j"                  r|j                  d       | j$                  rD|j                  d       |j                  t        | j$                               || j$                  z  }| j&                  rOt        | j&                  t(              st        d      |j                  d       || j&                  j                  z  }| j*                  r|d| j*                  gz  }| j,                  r|d| j,                  gz  }| j.                  r|d| j.                  gz  }|S )N	NOCONTENTINFIELDSVERBATIMNOSTOPWORDSz Did not receive a Filter object.WITHPAYLOADSSCORER
WITHSCORESINKEYSr   SLOPTIMEOUTINORDERRETURNzDid not receive a SortByField.SORTBYLANGUAGEEXPANDERDIALECT)r   r5   r   rE   r   r   r   r9   FilterAttributeErrorrF   r   r   r   r   r   r   r   r   r   SortbyFieldr!   r"   r#   )r$   rF   flts      r%   rZ   zQuery._get_args_tags   sQ   KK$<<KK
#KKDLL)*DLL D>>KK
#KK&==}} !!#v.()KLL ! KK'<<Xt||,,DKK%99KK!KKDII'DIID::?VTZZ((D==$Y..D>>KK	"KK!KKD//01D'''D<<dllK8$%EFFKK!DLL%%%D>>Z00D>>Z00D==Y..Dr'   offsetnumc                 "    || _         || _        | S )z
        Set the paging for the query (defaults to 0..10).

        - **offset**: Paging offset for the results. Defaults to 0
        - **num**: How many results do we want
        )r   r   )r$   rq   rr   s      r%   pagingzQuery.paging   s     	r'   c                     d| _         | S )zWSet the query to be verbatim, i.e. use no query expansion
        or stemming.
        T)r   r)   s    r%   verbatimzQuery.verbatim   s     r'   c                     d| _         | S )z>Set the query to only return ids and not the document content.T)r   r)   s    r%   
no_contentzQuery.no_content   s    r'   c                     d| _         | S )z
        Prevent the query from being filtered for stopwords.
        Only useful in very big queries that you are certain contain
        no stopwords.
        T)r   r)   s    r%   no_stopwordszQuery.no_stopwords   s     "r'   c                     d| _         | S )z+Ask the engine to return document payloads.T)r   r)   s    r%   with_payloadszQuery.with_payloads  s    "r'   c                     d| _         | S )z0Ask the engine to return document search scores.T)r   r)   s    r%   with_scoreszQuery.with_scores  s     r'   c                     || _         | S )z
        Limit the search to specific TEXT fields only.

        - **fields**: A list of strings, case sensitive field names
        from the defined schema.
        )r   r.   s     r%   limit_fieldszQuery.limit_fields  s     r'   c                 <    | j                   j                  |       | S )z
        Add a numeric or geo filter to the query.
        **Currently only one of each filter is supported by the engine**

        - **flt**: A NumericFilter or GeoFilter object, used on a
        corresponding field
        )r   r5   )r$   rp   s     r%   
add_filterzQuery.add_filter  s     	S!r'   ascc                 (    t        ||      | _        | S )z
        Add a sortby field to the query.

        - **field** - the name of the field to sort by
        - **asc** - when `True`, sorting will be done in asceding order
        )ro   r   r$   r1   r   s      r%   sort_byzQuery.sort_by&  s     #5#.r'   expanderc                     || _         | S )zg
        Add a expander field to the query.

        - **expander** - the name of the expander
        )r"   )r$   r   s     r%   r   zQuery.expander0  rO   r'   dialectc                     || _         | S )zw
        Add a dialect field to the query.

        - **dialect** - dialect version to execute the query under
        )r#   )r$   r   s     r%   r   zQuery.dialect9  s      r'   )r	   r   r8   )NNNN)NN)rp   rm   r	   r   T)$__name__
__module____qualname____doc__r:   r&   r   r,   r0   r   r6   r   r;   intrG   rL   rM   rP   floatrR   rU   rV   r[   rZ   rt   rv   rx   rz   r|   r~   r   r   boolr   r   r    r'   r%   r   r      s   ,S ,T ,<"c "
# # ' ET#Y E4 E "&%)#'!$$ c]$ C=	$
 c]$ 
$N OStCy)8@c8K	(    u  
	S 	W 	$s) 1S	 1f	S 	s 	w 	


DI ' 
S t w   s w r'   r   c                   *    e Zd Zdededee   ddfdZy)rm   keywordr1   rF   r	   Nc                 .    ||gt        |      z   | _        y r8   )r   rF   )r$   r   r1   rF   s       r%   r&   zFilter.__init__D  s    e$tDz1	r'   )r   r   r   r:   r   r&   r   r'   r%   rm   rm   C  s'    2 2C 2S	 2d 2r'   rm   c                   N    e Zd ZdZdZ	 	 ddedeeef   deeef   dededd	fd
Z	y	)NumericFilterz+infz-infr1   minvalmaxvalminExclusivemaxExclusiver	   Nc                 X    |s|nd| |s|nd| g}t        j                  | d|g|  y )N(FILTERrm   r&   )r$   r1   r   r   r   r   rF   s          r%   r&   zNumericFilter.__init__L  s<     'FaxL&FaxL

 	h55r'   )FF)
r   r   r   INFNEG_INFr:   r   r   r   r&   r   r'   r%   r   r   H  sc    
CG #"66 c3h6 c3h	6
 6 6 
6r'   r   c                   @    e Zd ZdZdZdZdZefdedededed	ed
dfdZ	y)	GeoFiltermkmftmir1   lonlatradiusunitr	   Nc           	      :    t         j                  | d|||||       y )N	GEOFILTERr   )r$   r1   r   r   r   r   s         r%   r&   zGeoFilter.__init__b  s     	k5#sFDIr'   )
r   r   r   METERS
KILOMETERSFEETMILESr:   r   r&   r   r'   r%   r   r   \  sY    FJDE NXJJ$J+0J:?JGJJ	Jr'   r   c                       e Zd ZddeddfdZy)ro   r1   r	   Nc                 ,    ||r	dg| _         y dg| _         y )NASCDESC)rF   r   s      r%   r&   zSortbyField.__init__i  s    SE5	f5	r'   r   )r   r   r   r:   r&   r   r'   r%   ro   ro   h  s    6c 6 6r'   ro   N)	typingr   r   r   r   rm   r   r   ro   r   r'   r%   <module>r      sF    ( (| |~	2 2
6F 6(	J 	J6 6r'   