o
    Lj                     @   sJ   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 dZ
dS )	    )ListOptional)decode_field_valuestr_if_bytes   )	to_string)Documentc                   @   sv   e Zd ZdZ					ddee deee  fddZe				dd	ed
e
dedee dd f
ddZdefddZdS )Resultz[
    Represents the result of a search query, and has an array of Document
    objects
    r   FNfield_encodingswarningsc              	   C   s  |d | _ || _g | _|pg | _d}|r|d }|r|d }|r$|d }|r(dnd}	tdt||D ]}
t||
 }|rDt||
|	  nd}|rL|	d n|	}|rXt||
d  nd}i }|r||
|  durtt||
|  ddd }||
|  ddd }t	||D ](\}}|du s||vrt|||< q|| }|du r|||< qt||d||< qz|d= W n	 t
y   Y nw z|d |d< |d= W n	 t
y   Y nw |rt|f||d	|n	t|fd
|i|}| j| q2dS )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   doc r-   Z/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/redis/commands/search/result.py__init__   s\   




zResult.__init__r   r   r"   returnc              	   C   sl  |  | }|du ri }dd | D }|dd|_||_g |_dd |dg D |_|d	g D ]}d
d | D }t|dd}d}|rTd|v rTt|d }i }	|dp\i }
|
 D ]\}}t|}t	||||	|< qaz|	d= W n	 t
y   Y nw z|	d |	d< |	d= W n	 t
y   Y nw |rt|f|dd|	n	t|fddi|	}|j| q3|S )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": [...]
        }
        Nc                 S      i | ]	\}}t ||qS r-   r   .0kvr-   r-   r.   
<dictcomp>u       z%Result.from_resp3.<locals>.<dictcomp>total_resultsr   c                 S   s   g | ]}t |qS r-   r2   )r4   wr-   r-   r.   
<listcomp>y   s    z%Result.from_resp3.<locals>.<listcomp>warningresultsc                 S   r1   r-   r2   r3   r-   r-   r.   r7   |   r8   r    r   extra_attributesr   r   r   r   )__new__itemsgetr   r   r   r   r   r   r   r   r   r   )clsr   r   r"   r
   instanceresult_itemdoc_idr   r'   extra_attrsr*   r+   r,   r-   r-   r.   
from_resp3[   sH   


zResult.from_resp3c                 C   s   d| j  d| j dS )NzResult{z total, docs: })r   r   )r   r-   r-   r.   __repr__   s   zResult.__repr__)r   FFNN)r   FN)__name__
__module____qualname____doc__r   dictr   strr/   classmethodr   boolrH   rJ   r-   r-   r-   r.   r	   	   s8    	

L@r	   N)typingr   r   redis.utilsr   r   _utilr   documentr   r	   r-   r-   r-   r.   <module>   s
    