
    Zi%                         d dl mZmZ d dlmZmZmZmZmZm	Z	 d dl
mZmZmZmZmZmZmZmZmZmZ e G d d                      Ze G d d                      Ze G d d	                      Z G d
 d          ZdS )    )	dataclassfield)ListDictAnyUnionSetOptional)
KNNFilterGroupByLimit
ProjectionScanRankSelectWhereKeyc                       e Zd ZU eed<   dS )	CountPlanscanN)__name__
__module____qualname__r   __annotations__     _/root/projects/butler/venv/lib64/python3.11/site-packages/chromadb/execution/expression/plan.pyr   r      s         
JJJJJr   r   c                       e Zd ZU eed<    ee          Zeed<    ee          Z	eed<    ee
          Ze
ed<   dS )GetPlanr   default_factoryfilterlimit
projectionN)r   r   r   r   r   r   r   r#   r   r$   r   r%   r   r   r   r    r       sr         
JJJU6222FF2225///E5///"U:>>>J
>>>>>r   r    c                   h    e Zd ZU eed<   eed<    ee          Zeed<    ee	          Z
e	ed<   dS )KNNPlanr   knnr!   r#   r%   N)r   r   r   r   r   r   r   r   r#   r   r%   r   r   r   r'   r'      s^         
JJJ	HHHU6222FF222"U:>>>J
>>>>>r   r'   c                   B   e Zd ZdZ	 	 	 	 	 ddeeeeee	f         f                  deee
eee	f         f                  deeeeee	f         f                  deeeeee	f         ef                  deeeeee	f         ee         ee         f                  f
dZd	eee	f         fd
ZddZdeeef         d	d fdZdeeeeee	f         f                  d	d fdZdeee
eee	f         f                  d	d fdZdeeeeee	f         f                  d	d fdZddeded	d fdZdS )Searcha\  Payload for hybrid search operations.

    Can be constructed by directly providing the parameters, or by using the builder pattern.

    Examples:
        Direct construction with expressions:
            Search(
                where=Key("status") == "active",
                rank=Knn(query=[0.1, 0.2]),
                limit=Limit(limit=10),
                select=Select(keys={Key.DOCUMENT}),
            )

        Direct construction with dicts:
            Search(
                where={"status": "active"},
                rank={"$knn": {"query": [0.1, 0.2]}},
                limit=10,
                select=["#document", "#score"],
            )

        Builder pattern:
            (Search()
             .where(Key("status") == "active")
             .rank(Knn(query=[0.1, 0.2]))
             .limit(10)
             .select(Key.DOCUMENT))
    Nwhererankgroup_byr$   selectc                    |d| _         npt          |t                    r|| _         nSt          |t                    rt          j        |          | _         n$t          dt          |          j                   |d| _        npt          |t                    r|| _        nSt          |t                    rt          j        |          | _        n$t          dt          |          j                   |t                      | _        npt          |t                    r|| _        nSt          |t                    rt          j        |          | _        n$t          dt          |          j                   |t                      | _        nt          |t                    r|| _        nt          |t                    rt          j        |dd          | _        nSt          |t                    rt          j        |          | _        n$t          dt          |          j                   |t                      | _        dS t          |t                    r	|| _        dS t          |t                    rt          j        |          | _        dS t          |t"          t$          f          r*t          j        dt#          |          i          | _        dS t          d	t          |          j                   )
aW  Initialize a Search payload.

        Args:
            where: Where expression or dict for filtering results (defaults to None - no filtering)
                   Dict will be converted using Where.from_dict()
            rank: Rank expression or dict for scoring (defaults to None - no ranking)
                  Dict will be converted using Rank.from_dict()
                  Note: Primitive numbers are not accepted - use {"$val": number} for constant ranks
            group_by: GroupBy configuration for grouping and aggregating results (defaults to None)
                      Dict will be converted using GroupBy.from_dict()
            limit: Limit configuration for pagination (defaults to no limit)
                   Can be a Limit object, a dict for Limit.from_dict(), or an int
                   When passing an int, it creates Limit(limit=value, offset=0)
            select: Select configuration for keys (defaults to empty selection)
                    Can be a Select object, a dict for Select.from_dict(),
                    or a list/set of strings (e.g., ["#document", "#score"])
        Nz1where must be a Where object, dict, or None, got z/rank must be a Rank object, dict, or None, got z6group_by must be a GroupBy object, dict, or None, got r   )r$   offsetz6limit must be a Limit object, dict, int, or None, got keysz>select must be a Select object, dict, list, set, or None, got )_where
isinstancer   dict	from_dict	TypeErrortyper   _rankr   r   	_group_byr   _limitintr   _selectlistset)selfr+   r,   r-   r$   r.   s         r   __init__zSearch.__init__E   s   4 =DKKu%% 	DKKt$$ 	/%00DKKZDKKDXZZ  
 <DJJd## 	DJJd## 	--DJJW$t**BUWW  
 $YYDNN'** 	%DNN$'' 	$.x88DNNbhI`bb  
 =''DKKu%% 		DKKs## 	/EQ*G*GHHDKKt$$ 	/%00DKK_eI]__  
 >!88DLLL'' 
	!DLLL%% 	!+F33DLLLs,, 	!+VT&\\,BCCDLLLhQUV\Q]Q]Qfhh  r   returnc                    | j         | j                                         nd| j        | j                                        nd| j                                        | j                                        | j                                        dS )z5Return a JSON-serializable dictionary representation.N)r#   r,   r-   r$   r.   )r2   to_dictr8   r9   r:   r<   )r?   s    r   rC   zSearch.to_dict   s     04{/Fdk))+++D,0J,BDJ&&(((..00[((**l**,,
 
 	
r   c                     t          t          j        t          j        t          j        t          j        h          }t          | j        | j        | j	        | j
        |          S )zBSelect all predefined keys (document, embedding, metadata, score).r1   r+   r,   r-   r$   r.   )r   r   DOCUMENT	EMBEDDINGMETADATASCOREr*   r2   r8   r9   r:   )r?   
new_selects     r   
select_allzSearch.select_all   sQ    #,s|SY!WXXX
+^+
 
 
 	
r   r1   c                     t          t          |                    }t          | j        | j        | j        | j        |          S )zSelect specific keys to return.

        Args:
            *keys: Key objects or string key names.

        Returns:
            Search: A new Search with updated selection.
        rE   rF   )r   r>   r*   r2   r8   r9   r:   )r?   r1   rK   s      r   r.   zSearch.select   sH     T+++
+^+
 
 
 	
r   c                 R    t          || j        | j        | j        | j                  S )aQ  Set the where clause for filtering.

        Args:
            where: Where expression, dict, or None.

        Example:
            search.where((Key("status") == "active") & (Key("score") > 0.5))
            search.where({"status": "active"})
            search.where({"$and": [{"status": "active"}, {"score": {"$gt": 0.5}}]})
        rF   )r*   r8   r9   r:   r<   )r?   r+   s     r   r+   zSearch.where   s2     ^+<
 
 
 	
r   	rank_exprc                 R    t          | j        || j        | j        | j                  S )a  Set the ranking expression.

        Args:
            rank_expr: A Rank expression, dict, or None for scoring
                       Dicts will be converted using Rank.from_dict()
                       Note: Primitive numbers are not accepted - use {"$val": number} for constant ranks

        Example:
            search.rank(Knn(query=[0.1, 0.2]) * 0.8 + Val(0.5) * 0.2)
            search.rank({"$knn": {"query": [0.1, 0.2]}})
            search.rank({"$sum": [{"$knn": {"query": [0.1, 0.2]}}, {"$val": 0.5}]})
        rF   )r*   r2   r9   r:   r<   )r?   rO   s     r   r,   zSearch.rank   s2     +^+<
 
 
 	
r   c                 R    t          | j        | j        || j        | j                  S )a#  Set the group_by configuration for grouping and aggregating results

        Args:
            group_by: A GroupBy object, dict, or None for grouping
                      Dicts will be converted using GroupBy.from_dict()

        Example:
            search.group_by(GroupBy(
                keys=[Key("category")],
                aggregate=MinK(keys=[Key.SCORE], k=3)
            ))
            search.group_by({
                "keys": ["category"],
                "aggregate": {"$min_k": {"keys": ["#score"], "k": 3}}
            })
        rF   )r*   r2   r8   r:   r<   )r?   r-   s     r   r-   zSearch.group_by   s2    " ++<
 
 
 	
r   r   r0   c                 t    t          ||          }t          | j        | j        | j        || j                  S )zSet the limit and offset for pagination

        Args:
            limit: Maximum number of results to return
            offset: Number of results to skip (default: 0)

        Example:
            search.limit(20, offset=10)
        )r0   r$   rF   )r   r*   r2   r8   r9   r<   )r?   r$   r0   	new_limits       r   r$   zSearch.limit  sD     u555	+^<
 
 
 	
r   )NNNNN)rA   r*   )r   )r   r   r   __doc__r
   r   r   r   strr   r   r   r   r;   r   r   r	   r@   rC   rL   r   r.   r+   r,   r-   r$   r   r   r   r*   r*   '   s>        > 9=6:=A=AOSX XeT#s(^345X uT4S>123X 5$sCx.!89:	X
 eT#s(^S89:X vtCH~tCy#c(JKLX X X Xt
c3h 
 
 
 
	
 	
 	
 	

E#s(O 
 
 
 
 
$
8E%c3h*?$@A 
h 
 
 
 
&
huT4S>-A'BC 
 
 
 
 
*
%c3h0G*H!I 
h 
 
 
 
2
 
3 
 
H 
 
 
 
 
 
r   r*   N)dataclassesr   r   typingr   r   r   r   r	   r
   &chromadb.execution.expression.operatorr   r   r   r   r   r   r   r   r   r   r   r    r'   r*   r   r   r   <module>rY      s   ( ( ( ( ( ( ( ( 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8                                ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?r
 r
 r
 r
 r
 r
 r
 r
 r
 r
r   