
    [g=                         S SK Jr  S SKrS SKrS SKJr  S SKJr  S SKJ	r	  S SK
Jr  S SKJr  S SKJr  S S	KJrJr  S S
KJr  S SKJr  S SKJr  \R2                  " \5      r " S S5      rg)    )defaultdictN)BaseLunrException)FieldRef)	MatchData)TokenSet)TokenSetBuilder)Pipeline)QueryQueryPresence)QueryParser)CompleteSet)Vectorc                   T    \ rS rSrSrS rS rS rSS jrSS jr	S	 r
\S
 5       rSrg)Index   a>  An index contains the built index of all documents and provides a query
interface to the index.

Usually instances of lunr.Index will not be created using this
constructor, instead lunr.Builder should be used to construct new
indexes, or lunr.Index.load should be used to load previously built and
serialized indexes.
c                 @    Xl         X l        X0l        X@l        XPl        g N)inverted_indexfield_vectors	token_setfieldspipeline)selfr   r   r   r   r   s         ,/usr/lib/python3/dist-packages/lunr/index.py__init__Index.__init__   s    ,*"     c                 t    U R                   UR                   :H  =(       a    U R                  UR                  :H  $ r   )r   r   )r   others     r   __eq__Index.__eq__$   s/     5#7#77WDKK5<<<W	
r   c                 z    U R                  5       n[        X5      nUR                  5         U R                  U5      $ )aK  Performs a search against the index using lunr query syntax.

Results will be returned sorted by their score, the most relevant
results will be returned first.

For more programmatic querying use `lunr.Index.query`.

Args:
    query_string (str): A string to parse into a Query.

Returns:
    dict: Results of executing the query.
)create_queryr   parsequery)r   query_stringr%   parsers       r   searchIndex.search*   s4     !!#\1zz%  r   Nc                     Uc  [        U R                  5      $ [        U5      [        U R                  5      -
  nU(       a  [        SU5      e[        U5      $ )a  Convenience method to create a Query with the Index's fields.

Args:
    fields (iterable, optional): The fields to include in the Query,
        defaults to the Index's `all_fields`.

Returns:
    Query: With the specified fields or all the fields in the Index.
z#Fields {} are not part of the index)r
   r   setr   )r   r   non_contained_fieldss      r   r#   Index.create_query>   sR     >%%"6{S-==#57K  V}r   c           
      	   Uc  U R                  5       nUb  U" U5        [        UR                  5      S:X  a  [        R	                  S5        / $ 0 nU R
                   Vs0 s H  oD[        5       _M     nn0 n0 n[        [        5      nUR                   GH  n	U	R                  (       a3  U R                  R                  U	R                  SU	R
                  05      n
OU	R                  /n
[        5       nU
 GH  nXl        [        R                  " U	5      nU R                  R!                  U5      R#                  5       n[        U5      S:X  a@  U	R$                  [&        R(                  :X  a"  U	R
                   H  n[+        5       Xt'   M       GOnU GHb  nU R,                  U   nUS   nU	R
                   GH9  nUU   nUR/                  5       nUS-   U-   n[        U5      nU	R$                  [&        R(                  :X  a#  UR1                  U5      nXG;  a  [+        5       Xt'   O5U	R$                  [&        R2                  :X  a  X   R1                  U5      X'   M  XT   R5                  UU	R6                  S 5        UU;   a  M  U Hd  n[9        UU5      nU[;        U5         n[;        U5      U;  a  [=        XU5      U[;        U5      '   MF  U[;        U5         R?                  XU5        Mf     SUU'   GM<     GMe     GM     U	R$                  [&        R(                  :X  d  GM  U	R
                   H  nXt   RA                  U5      Xt'   M     GM     [+        5       n[        5       nU R
                   H5  nXG;   a  URA                  Xt   5      nXH;   d  M"  UR1                  X   5      nM7     UR/                  5       n/ n0 nURC                  5       (       aO  [E        U RF                  R/                  5       5      nU H&  n[8        RH                  " U5      n[=        5       UU'   M(     U H  n[8        RH                  " U5      nURJ                  nUU;  d  UU;   a  M3  U RF                  U   n UURL                     RO                  U 5      n! UU   n"U"S==   U!-  ss'   U"S	   RQ                  UU   5        M     [W        US SS9$ s  snf ! [R         a#    UU!UU   S
.n#U#UU'   URU                  U#5         M  f = f)a"  Performs a query against the index using the passed lunr.Query
object.

If performing programmatic queries against the index, this method is
preferred over `lunr.Index.search` so as to avoid the additional query
parsing overhead.

Args:
    query (lunr.Query): A configured Query to perform the search
        against, use `create_query` to get a preconfigured object
        or use `callback` for convenience.
    callback (callable): An optional function taking a single Query
        object result of `create_query` for further configuration.
r   zAttempting a query with no clauses. Please add clauses by either using the `callback` argument or using `create_query` to create a preconfigured Query, manually adding clauses and passing it as the `query` argument.r   _index/c                 
    X-   $ r    )abs     r   <lambda>Index.query.<locals>.<lambda>   s    15r   Tscore
match_data)refr7   r8   c                     U S   $ )Nr7   r2   )r3   s    r   r5   r6   9  s    QwZr   )keyreverse),r#   lenclausesloggerwarningr   r   r   r+   use_pipeliner   
run_stringtermr   from_clauser   	intersectto_listpresencer   REQUIREDr   r   keysunion
PROHIBITEDupsertboostr   strr   addintersection
is_negatedlistr   from_stringdoc_ref
field_name
similaritycombineKeyErrorappendsorted)$r   r%   callbackmatching_fieldsfieldquery_vectorsterm_field_cacherequired_matchesprohibited_matchesclausetermsclause_matchesrC   term_token_setexpanded_termsexpanded_termposting
term_indexfield_postingmatching_document_refs
term_fieldmatching_documents_setmatching_document_refmatching_field_refmetadataall_required_matchesall_prohibited_matchesmatching_field_refsresultsmatches	field_refrT   field_vectorr7   	doc_matchmatchs$                                       r   r%   Index.queryS   s    =%%'EUOu}}"NN6 I 6:kkBkUkB(-mmF ""00x>WX UN
 #
 "*!5!5f!=!%!9!9.!I!Q!Q!S '1,=+A+AA!'2=-(/ "/%3M"11-@G!(!2J!' )01>1C1C1E.%2S%85%@
145K1L.
 "??m.D.DD-;-A-A 6.N  %<:E- 0 7 $__0H0HH8J8Q8W8W 69.5 % &,33&6H &)99$5K1
 2::OQV1W.'4S9N5O'PH"#56oMKT$1(L"4F0G H !04F0G H L L$1(!" 6L" 8<(4Q "/	 &43 V -"8"88#]]E.>.E.R.R&/$+ +u $D  +}!$[[E(';'H'H$+($ *)?)E)E&-*& ! .224 "&t'9'9'>'>'@"A&9"$001CD	6?k 23 ': #6 !,,-?@I''G22gAW6W--.@AL!)"6"67BB<PE&#G,	'"e+",'//@R0ST' #6: g#7FFA Cn  &"""12D"E
 $) u%&s   R6=)R;;)S('S(c                 X   SSK Jn  [        U R                  5       Vs/ s H  o"U R                  U   /PM     nnU R                  R                  5        VVs/ s H  u  pEXER                  5       /PM     nnnUU R                  UUU R                  R                  5       S.$ s  snf s  snnf )z;Returns a serialized index as a dict following lunr-schema.r   __TARGET_JS_VERSION__)versionr   fieldVectorsinvertedIndexr   )	lunrr}   rZ   r   r   items	serializer   r   )r   r}   rC   r   r9   vectorr   s          r   r   Index.serialize;  s    . ;AATAT:U
:U$4&&t,-:U 	 
 :>9K9K9Q9Q9S
9S+#S""$%9S 	 
 -kk)+//1
 	


s   B!B&c           
         SSK Jn  [        U[        5      (       a  [        R
                  " U5      nUS   U:w  a'  [        R                  SR                  X!S   5      5        US    VVs0 s H  u  p4U[        U5      _M     nnn[        5       n0 nUS    H  u  pUR                  U5        XU'   M     UR                  5         [        US   UUUR                  [        R                   " US   5      S	9$ s  snnf )
zLoad a serialized indexr   r|   r~   zuVersion mismatch when loading serialized index. Current version of lunr {} does not match that of serialized index {}r   r   r   r   )r   r   r   r   r   )r   r}   
isinstancerN   jsonloadsr?   r@   formatr   r   insertfinishr   rootr	   load)
clsserialized_indexr}   r9   elementsr   tokenset_builderr   rC   rh   s
             r   r   
Index.loadO  s    	/&,,#zz*:;I&*??NN!6"7)9TU 8H7W
7WmcC!!7W 	 
 +,-o>MD##D)#*4  ? 	!#H-')&++]]#3J#?@
 	

s   *C5)r   r   r   r   r   r   )NN)__name__
__module____qualname____firstlineno____doc__r   r    r(   r#   r%   r   classmethodr   __static_attributes__r2   r   r   r   r      s<    !
!(*fGP
(  
  
r   r   )collectionsr   r   logginglunr.exceptionsr   lunr.field_refr   lunr.match_datar   lunr.token_setr   lunr.token_set_builderr   lunr.pipeliner	   
lunr.queryr
   r   lunr.query_parserr   
lunr.utilsr   lunr.vectorr   	getLoggerr   r?   r   r2   r   r   <module>r      sH    #   - # % # 2 " + ) " 			8	$]
 ]
r   