
    AHj                     P    d dl mZmZ d dlmZmZ ddlmZ ddlm	Z	  G d d      Z
y)	    )ListOptional)decode_field_valuestr_if_bytes   )	to_string)Documentc                   ~    e Zd ZdZ	 	 	 	 	 ddee   deee      fdZe		 	 	 ddede
dedee   d	d f
d
       Zd	efdZy)Resultz[
    Represents the result of a search query, and has an array of Document
    objects
    Nfield_encodingswarningsc                    |d   | _         || _        g | _        |xs g | _        d}|r|dz   }|r|dz   }|r|dz   }|rdnd}	t	        dt        |      |      D ]
  }
t        ||
         }|rt        ||
|	z            nd}|r|	dz   n|	}|rt        ||
dz            nd}i }|rx||
|z      pt        t        ||
|z      ddd         }||
|z      ddd   }t        ||      D ]7  \  }}|||vrt        |      ||<   ||   }||||<   (t        ||      ||<   9 	 |d= 	 |d   |d<   |d= |rt        |f||d	|nt        |fd
|i|}| j                  j                  |        y# t        $ r Y Uw xY w# t        $ r Y Xw xY w)a8  
        - duration: the execution time of the query
        - has_payload: whether the query has payloads
        - with_scores: whether the query has scores
        - field_encodings: a dictionary of field encodings if any is provided
        - warnings: list of server warnings (from RESP3 responses)
        r   r      N)encodingid$jsonscorepayloadr   )totaldurationdocsr   rangelenr   floatmapzipKeyErrorr	   append)selfres
hascontentr   has_payloadwith_scoresr   r   stepoffsetir   r   fields_offsetr   fieldskeysvalueskeyvaluer   docs                         T/root/tools/cai/cai_env/lib/python3.12/site-packages/redis/commands/search/result.py__init__zResult.__init__   s   $ V
 	 B!8D!8D!8D!qq#c(D) )	"A3q6"B4?iAJ0TG*5FQJ6M)4E#a!e*%$EFc!m"34@9c!m*;&<SqS&ABQ./15"%dF"3 JJC&.#_2L&/&6s .s3H  '&+s&/&IsJ4L!'v3K  D5'DVDb<'<V< 
 IIS!S)	"6    s$   E&E5&	E21E25	F Fr"   r   r%   returnc                    | j                  |       }|i }|j                         D ci c]  \  }}t        |      | }}}|j                  dd      |_        ||_        g |_        |j                  dg       D cg c]  }t        |       c}|_        |j                  dg       D ]  }	|	j                         D ci c]  \  }}t        |      | }	}}t        |	j                  dd            }
d}|rd|	v rt        |	d         }i }|	j                  d	      xs i }|j                         D ]   \  }}t        |      }t        |||      ||<   " 	 |d= 	 |d
   |d<   |d
= |rt        |
f|dd|nt        |
fddi|}|j                  j                  |        |S c c}}w c c}w c c}}w # t        $ r Y fw xY w# t        $ r Y iw xY w)a+  Construct a Result from a RESP3 dict response.

        RESP3 format:
        {
            "total_results": N,
            "results": [
                {"id": "doc1", "score": 1.5, "extra_attributes": {"f1": "v1"}},
                ...
            ],
            "warning": [...]
        }
        Ntotal_resultsr   warningresultsr    r   extra_attributesr   r   r   r   )__new__itemsr   getr   r   r   r   r   r   r   r	   r    )clsr"   r   r%   r   instancekvwresult_itemdoc_idr   r*   extra_attrsr-   r.   r/   s                    r0   
from_resp3zResult.from_resp3[   s   ( ;;s#;C /2iik:da|A!::!4$69ggi6LM\!_M779b1 	&K:E:K:K:MN$!Q<?A-NKN!+//$";<FEw+5k'23F%//*<=CK)//1 N
U"3'0_MsN4L!'v3K  EudEfEf=d=f= 
 MM  %;	&> K ; N O    s5   F;F%=F*F0F?0	F<;F<?	G
Gc                 <    d| j                    d| j                   dS )NzResult{z total, docs: })r   r   )r!   s    r0   __repr__zResult.__repr__   s    $**^DII;bAA    )r   FFNN)r   FN)__name__
__module____qualname____doc__r   dictr   strr1   classmethodr   boolrD   rG    rH   r0   r   r   	   s     *.(,J" "$J" 49%J"X  !*.>> > 	>
 "$> 
> >@B# BrH   r   N)typingr   r   redis.utilsr   r   _utilr   documentr	   r   rQ   rH   r0   <module>rV      s    ! 8  TB TBrH   