
    =wgţ                     N   d Z ddlmZ ddlmZ ddlmZmZmZm	Z	 ddl
mZ ddlmZ ddlmZmZmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ  G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z  G d de      Z! G d de      Z"d Z# G d de      Z$y)z?This module contains classes that allow reading from an index.
    )log)bisect_right)heapifyheapreplaceheappopnlargest)columns)abstractmethod)xrangezip_next	iteritems)OverlayStorage)MultiMatcher)distance)
emptybytesc                       e Zd ZdZdZy)ReaderClosedzException raised when you try to do some operation on a closed searcher
    (or a Results object derived from a searcher that has since been closed).
    zOperation on a closed readerN)__name__
__module____qualname____doc__message     E/var/www/horilla/myenv/lib/python3.12/site-packages/whoosh/reading.pyr   r   .   s     -Gr   r   c                       e Zd Zy)TermNotFoundN)r   r   r   r   r   r   r   r   6   s    r   r   c                   N    e Zd ZdZ	 	 ddZddZd Zd Zd Zd Z	d	 Z
d
 Zd Zy)TermInfozRepresents a set of statistics about a term. This object is returned by
    :meth:`IndexReader.term_info`. These statistics may be useful for
    optimizations and scoring algorithms.
    Nc                 f    || _         || _        || _        || _        || _        || _        || _        y N)_weight_df
_minlength
_maxlength
_maxweight_minid_maxid)selfweightdf	minlength	maxlength	maxweightminidmaxids           r   __init__zTermInfo.__init__B   s3    ###r   c                 \   | j                   || _         || _        | xj                  |z  c_        | xj                  dz  c_        t	        | j
                  |      | _        |K| j                  || _        nt        | j                  |      | _        t	        | j                  |      | _        y y )N   )	r(   r)   r#   r$   maxr'   r%   minr&   )r*   docnumr+   lengths       r   add_postingzTermInfo.add_postingL   s    ;; DKAdoov6&"("%doov">!$//6:DO r   c                     | j                   S )zFReturns the total frequency of the term across all documents.
        )r#   r*   s    r   r+   zTermInfo.weight[   s     ||r   c                     | j                   S )z=Returns the number of documents the term appears in.
        )r$   r;   s    r   doc_frequencyzTermInfo.doc_frequencya   s     xxr   c                     | j                   S )zTReturns the length of the shortest field value the term appears
        in.
        )r%   r;   s    r   
min_lengthzTermInfo.min_lengthg       
 r   c                     | j                   S )zSReturns the length of the longest field value the term appears
        in.
        )r&   r;   s    r   
max_lengthzTermInfo.max_lengthn   r@   r   c                     | j                   S )zkReturns the number of times the term appears in the document in
        which it appears the most.
        )r'   r;   s    r   
max_weightzTermInfo.max_weightu   r@   r   c                     | j                   S )z=Returns the lowest document ID this term appears in.
        )r(   r;   s    r   min_idzTermInfo.min_id|        {{r   c                     | j                   S )z>Returns the highest document ID this term appears in.
        )r)   r;   s    r   max_idzTermInfo.max_id   rG   r   )r   r   Nr   r   Nr   r"   )r   r   r   r   r2   r9   r+   r=   r?   rB   rD   rF   rI   r   r   r   r    r    <   s<    
 26=>;r   r    c                      e Zd ZdZd Zd Zed        Zd Zd Z	d Z
d Zd	 Zd
 Zd Zed        Zed        Zd Zed        Zd Zd Zd Zd Zd Zd4dZd Zed        Zd Zd Zed        Zed        Zd Zed        Z ed        Z!ed        Z"ed         Z#ed!        Z$ed"        Z%ed#        Z&ed5d$       Z'd% Z(d& Z)ed'        Z*ed(        Z+ed6d*       Z,d+ Z-d, Z.d5d-Z/d7d.Z0d7d/Z1d0 Z2d1 Z3d2 Z4	 	 d8d3Z5y))9IndexReaderIDo not instantiate this object directly. Instead use Index.reader().
    c                     | S r"   r   r;   s    r   	__enter__zIndexReader.__enter__   s    r   c                 $    | j                          y r"   )close)r*   argss     r   __exit__zIndexReader.__exit__   s    

r   c                     t         )zZReturns True if the given term tuple (fieldname, text) is
        in this reader.
        NotImplementedErrorr*   terms     r   __contains__zIndexReader.__contains__   
    
 "!r   c                      y)zReturns the :class:`whoosh.codec.base.Codec` object used to read
        this reader's segment. If this reader is not atomic
        (``reader.is_atomic() == True``), returns None.
        Nr   r;   s    r   codeczIndexReader.codec        r   c                      y)zReturns the :class:`whoosh.index.Segment` object used by this reader.
        If this reader is not atomic (``reader.is_atomic() == True``), returns
        None.
        Nr   r;   s    r   segmentzIndexReader.segment   r\   r   c                      y)zReturns the :class:`whoosh.filedb.filestore.Storage` object used by
        this reader to read its files. If the reader is not atomic,
        (``reader.is_atomic() == True``), returns None.
        Nr   r;   s    r   storagezIndexReader.storage   r\   r   c                      yNTr   r;   s    r   	is_atomiczIndexReader.is_atomic   s    r   c                 t    || j                   vrt        ||f      | j                   |   j                  |      S r"   )schemar   to_bytesr*   	fieldnametexts      r   _text_to_byteszIndexReader._text_to_bytes   s8    DKK'	4011{{9%..t44r   c                      y)z;Closes the open files associated with this reader.
        Nr   r;   s    r   rP   zIndexReader.close   s     	r   c                      y)zgReturns the generation of the index being read, or -1 if the backend
        is not versioned.
        Nr   r;   s    r   
generationzIndexReader.generation   s    
 r   c                     t         )zReturns an iterable of strings representing the names of the indexed
        fields. This may include additional names not explicitly listed in the
        Schema if you use "glob" fields.
        rT   r;   s    r   indexed_field_nameszIndexReader.indexed_field_names   s
     "!r   c                     t         )zEYields (fieldname, text) tuples for every term in the index.
        rT   r;   s    r   	all_termszIndexReader.all_terms   rY   r   c              #   ^   K   | j                         D ]  \  }}||k  s||k  r||f  yw)zjYields (fieldname, text) tuples for every term in the index starting
        at the given prefix.
        N)rq   )r*   rh   prefixfnameri   s        r   
terms_fromzIndexReader.terms_from   s=       >>+ 	 KE4y D6M$-	    +-c                     t         )zoReturns a :class:`TermInfo` object allowing access to various
        statistics about the given term.
        rT   rg   s      r   	term_infozIndexReader.term_info   
     "!r   c              #      K   | j                  ||      }| j                  ||      D ]!  \  }}||k7  s|j                  |      s y| # yw)zJYields terms in the given field that start with the given prefix.
        N)rj   ru   
startswith)r*   rh   rs   fnri   s        r   expand_prefixzIndexReader.expand_prefix   sR      $$Y7	6: 	HBYdoof&=J	s   A
Ac              #   ^   K   | j                  |t              D ]  \  }}||k7  r y|  yw)z3Yields all bytestrings in the given field.
        N)ru   r   )r*   rh   r|   btexts       r   lexiconzIndexReader.lexicon   s5      J? 	IBYK	rv   c              #   ~   K   | j                   |   j                  }| j                  |      D ]  } ||        yw)zZYields all term values (converted from on-disk bytes) in the given
        field.
        N)re   
from_bytesr   )r*   rh   r   r   s       r   field_termszIndexReader.field_terms   s?     
 [[+66
\\), 	$EU##	$s   ;=c              #   `   K   | j                   }| j                         D ]  }| || f  yw)zkYields ((fieldname, text), terminfo) tuples for each term in the
        reader, in lexical order.
        N)rx   rq   )r*   rx   rW   s      r   __iter__zIndexReader.__iter__  s7     
 NN	NN$ 	+DD)**	+   ,.c              #      K   | j                   }| j                  ||      }| j                  ||      D ]  }| || f  yw)zuYields ((fieldname, text), terminfo) tuples for all terms in the
        reader, starting at the given term.
        N)rx   rj   ru   )r*   rh   ri   rx   rW   s        r   	iter_fromzIndexReader.iter_from  sM     
 NN	""9d3OOIt4 	+DD)**	+s   A Ac              #      K   | j                  ||      }| j                  ||      D ]  \  \  }}}||k7  r y||f  yw)zIYields (text, terminfo) tuples for all terms in the given field.
        N)rj   r   )r*   rh   rs   r|   ri   terminfos         r   
iter_fieldzIndexReader.iter_field  sQ      $$Y7$(NN9f$E 	! JRY. 	!s   >A c              #      K   | j                  ||      }| j                  ||      D ]  \  }}|j                  |      s y||f   yw)zgYields (text, terminfo) tuples for all terms in the given field with
        a certain prefix.
        N)rj   r   r{   )r*   rh   rs   ri   r   s        r   iter_prefixzIndexReader.iter_prefix%  sR     
 $$Y7"ooi@ 	#ND(??6*""	#s   AA	c                     t         )zTReturns True if the underlying index/segment has deleted
        documents.
        rT   r;   s    r   has_deletionszIndexReader.has_deletions0  ry   r   c                 `    | j                   fdt        | j                               D        S )zKReturns an iterator of all (undeleted) document IDs in the reader.
        c              3   2   K   | ]  } |      s|  y wr"   r   ).0r7   
is_deleteds     r   	<genexpr>z*IndexReader.all_doc_ids.<locals>.<genexpr>=  s       +6!&)  +s   )r   r   doc_count_all)r*   r   s    @r   all_doc_idszIndexReader.all_doc_ids8  s-     __
+VD,>,>,@%A + 	+r   c              #   `   K   | j                         D ]  }|| j                  |      f  yw)zvYields a series of ``(docnum, stored_fields_dict)``
        tuples for the undeleted documents in the reader.
        N)r   stored_fieldsr*   r7   s     r   	iter_docszIndexReader.iter_docs@  s6     
 &&( 	5F$,,V444	5r   c                     t         )zEReturns True if the given document number is marked deleted.
        rT   r   s     r   r   zIndexReader.is_deletedH  rY   r   c                     t         )zReturns the stored fields for the given document number.

        :param numerickeys: use field numbers as the dictionary keys instead of
            field names.
        rT   r   s     r   r   zIndexReader.stored_fieldsO  s
     "!r   c              #      K   | j                   }t        | j                               D ]  } ||      r| j                  |         yw)z@Yields the stored fields for all non-deleted documents.
        N)r   r   r   r   )r*   r   r7   s      r   all_stored_fieldszIndexReader.all_stored_fieldsY  sG      __
T//12 	1Ff%((00	1s
   1A
A
c                     t         )z]Returns the total number of documents, DELETED OR UNDELETED,
        in this reader.
        rT   r;   s    r   r   zIndexReader.doc_count_allb  ry   r   c                 D    | j                         | j                         z
  S )zHReturns the total number of UNDELETED documents in this reader.
        )r   deleted_countr;   s    r   	doc_countzIndexReader.doc_countj  s!    
 !!#d&8&8&:::r   c                     t         )z[Returns the total number of instances of the given term in the
        collection.
        rT   rg   s      r   	frequencyzIndexReader.frequencyq  rY   r   c                     t         )z>Returns how many documents the given term appears in.
        rT   rg   s      r   r=   zIndexReader.doc_frequencyx  s
     "!r   c                     t         )zoReturns the total number of terms in the given field. This is used
        by some scoring algorithms.
        rT   r*   rh   s     r   field_lengthzIndexReader.field_length~  rY   r   c                     t         zwReturns the minimum length of the field across all documents. This
        is used by some scoring algorithms.
        rT   r   s     r   min_field_lengthzIndexReader.min_field_length  rY   r   c                     t         r   rT   r   s     r   max_field_lengthzIndexReader.max_field_length  rY   r   c                     t         )zReturns the number of terms in the given field in the given
        document. This is used by some scoring algorithms.
        rT   r*   r7   rh   defaults       r   doc_field_lengthzIndexReader.doc_field_length  rY   r   c                     | j                  ||      }| j                  ||      }|j                         r|j                         S t	        ||f      )zxReturns the first ID in the posting list for the given term. This
        may be optimized in certain backends.
        )rj   postings	is_activeidr   )r*   rh   ri   ps       r   first_idzIndexReader.first_id  sJ    
 ""9d3MM)T*;;=446MIt,--r   c              #   *  K   | j                         D ]|  \  }}| j                  ||      }|j                         s)|||j                         |j	                         |j                         f |j                          |j                         rT~ yw)zLow-level method, yields all postings in the reader as
        ``(fieldname, text, docnum, weight, valuestring)`` tuples.
        N)rq   r   r   r   r+   valuer   )r*   rh   r   ms       r   iter_postingszIndexReader.iter_postings  ss     
 !% 0 	Iui/A++- %QWWYGG ++-	s   9BABBc                     t         )ar  Returns a :class:`~whoosh.matching.Matcher` for the postings of the
        given term.

        >>> pr = reader.postings("content", "render")
        >>> pr.skip_to(10)
        >>> pr.id
        12

        :param fieldname: the field name or field number of the term.
        :param text: the text of the term.
        :rtype: :class:`whoosh.matching.Matcher`
        rT   rg   s      r   r   zIndexReader.postings  
     "!r   c                     t         )zZReturns True if the given document has a term vector for the given
        field.
        rT   r*   r7   rh   s      r   
has_vectorzIndexReader.has_vector  rY   r   Nc                     t         )a8  Returns a :class:`~whoosh.matching.Matcher` object for the
        given term vector.

        >>> docnum = searcher.document_number(path=u'/a/b/c')
        >>> v = searcher.vector(docnum, "content")
        >>> v.all_as("frequency")
        [(u"apple", 3), (u"bear", 2), (u"cab", 2)]

        :param docnum: the document number of the document for which you want
            the term vector.
        :param fieldname: the field name or field number of the field for which
            you want the term vector.
        :rtype: :class:`whoosh.matching.Matcher`
        rT   r*   r7   rh   format_s       r   vectorzIndexReader.vector  s
      "!r   c              #     K   | j                  ||      }|dk(  rU|j                         rD|j                         |j                         f |j	                          |j                         rCyy| j
                  |   j                  }|j                  |      }|j                         rJ|j                          ||j                               f |j	                          |j                         rIyyw)a  Returns an iterator of (termtext, value) pairs for the terms in the
        given term vector. This is a convenient shortcut to calling vector()
        and using the Matcher object when all you want are the terms and/or
        values.

        >>> docnum = searcher.document_number(path=u'/a/b/c')
        >>> searcher.vector_as("frequency", docnum, "content")
        [(u"apple", 3), (u"bear", 2), (u"cab", 2)]

        :param docnum: the document number of the document for which you want
            the term vector.
        :param fieldname: the field name or field number of the field for which
            you want the term vector.
        :param astype: a string containing the name of the format you want the
            term vector's data in, for example "weights".
        r+   N)	r   r   r   r+   r   re   formatdecoderr   )r*   astyper7   rh   vecr   r   s          r   	vector_aszIndexReader.vector_as  s     $ kk&),X--/vvx..
 --/ kk),33Goof-G--/vvx!566
 --/s   A*C4-BC42C4c                 @    ddl m} | j                  |   } || ||      S )zReturns a :class:`whoosh.spelling.Corrector` object that suggests
        corrections based on the terms in the given field.
        r   )ReaderCorrector)whoosh.spellingr   re   )r*   rh   r   fieldobjs       r   	correctorzIndexReader.corrector  s$    
 	4;;y)tY99r   c              #      K   | j                   |   }| j                  ||d|       D ]+  }|j                  |      }t        |||      }||k  s(| - yw)aJ  
        Returns a generator of words in the given field within ``maxdist``
        Damerau-Levenshtein edit distance of the given text.

        Important: the terms are returned in **no particular order**. The only
        criterion is that they are within ``maxdist`` edits of ``text``. You
        may want to run this method multiple times with increasing ``maxdist``
        values to ensure you get the closest matches first. You may also have
        additional information (such as term frequency or an acoustic matching
        algorithm) you can use to rank terms with the same edit distance.

        :param maxdist: the maximum edit distance.
        :param prefix: require suggestions to share a prefix of this length
            with the given word. This is often justifiable since most
            misspellings do not involve the first letter of the word.
            Using a prefix dramatically decreases the time it takes to generate
            the list of words.
        :param seen: an optional set object. Words that appear in the set will
            not be yielded.
        N)limit)re   r}   r   r   )	r*   rh   ri   maxdistrs   r   r   wordks	            r   terms_withinzIndexReader.terms_within  sb     , ;;y)''	4=A 	E&&u-Dt73AG|
		s   AAAc                 L    d | j                  ||      D        }t        ||      S )zwReturns the top 'number' most frequent terms in the given field as a
        list of (frequency, text) tuples.
        c              3   F   K   | ]  \  }}|j                         |f  y wr"   r+   )r   ri   r   s      r   r   z2IndexReader.most_frequent_terms.<locals>.<genexpr>$  s$      7^T8!4( 7s   !)r   r   )r*   rh   numberrs   gens        r   most_frequent_termszIndexReader.most_frequent_terms  s+    
7""9f57$$r   c                     t        | j                               fd| j                  ||      D        }t        ||      S )zsReturns the top 'number' terms with the highest `tf*idf` scores as
        a list of (score, text) tuples.
        c              3      K   | ]6  \  }}|j                         t        |j                         z        z  |f 8 y wr"   )r+   r   r=   )r   ri   r   Ns      r   r   z5IndexReader.most_distinctive_terms.<locals>.<genexpr>.  sC      J!4 !CH,B,B,D(D$EEtL Js   <?)floatr   r   r   )r*   rh   r   rs   r   r   s        @r   most_distinctive_termsz"IndexReader.most_distinctive_terms(  sA    
 $.."#J%)%5%5i%HJ$$r   c                     | dfgS )zReturns a list of (IndexReader, docbase) pairs for the child readers
        of this reader if it is a composite reader. If this is not a composite
        reader, it returns `[(self, 0)]`.
        r   r   r;   s    r   leaf_readerszIndexReader.leaf_readers2  s     q	{r   c                      yNFr   r;   s    r   supports_cacheszIndexReader.supports_caches:      r   c                      yr   r   r   s     r   
has_columnzIndexReader.has_column=  r   r   c                     t         )ap  

        :param fieldname: the name of the field for which to get a reader.
        :param column: if passed, use this Column object instead of the one
            associated with the field in the Schema.
        :param reverse: if passed, reverses the order of keys returned by the
            reader's ``sort_key()`` method. If the column type is not
            reversible, this will raise a ``NotImplementedError``.
        :param translate: if True, wrap the reader to call the field's
            ``from_bytes()`` method on the returned values.
        :return: a :class:`whoosh.columns.ColumnReader` object.
        rT   )r*   rh   columnreverse	translates        r   column_readerzIndexReader.column_reader@  r   r    r   r"      r   )NFF)6r   r   r   r   rN   rR   r
   rX   r[   r^   r`   rc   rj   rP   rm   ro   rq   ru   rx   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r=   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rK   rK      s    " "5
 " " " "	  " "$++!	# " "+5 " " " "1 " " ; ; " " " "
 " " " " " " " "	.	 " "  " " " ""<::%% =B %"r   rK   c                       e Zd Zd(dZd Zd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd)dZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d  Z!d! Z"d*d"Z#d*d#Z$d$ Z%d)d%Z&d& Z'	 	 d+d'Z(y),SegmentReaderNc                    || _         d| _        || _        | j                  j                         | _        || _        |j                         r#|j                  |      }t        ||      | _	        n|| _	        |r|n|j                         | _        | j                  j                  | j                  |      | _        | j                  j                  | j                  |      | _        y r   )re   	is_closed_segment
segment_id_segid_genis_compoundopen_compound_filer   _storager[   _codecterms_reader_termsper_document_reader_perdoc)r*   r`   re   r^   rm   r[   filess          r   r2   zSegmentReader.__init__U  s    mm..0	
  ..w7E +5':DM#DM  %e'--/kk..t}}gF{{66t}}gNr   c                     | j                   S r"   )r   r;   s    r   r[   zSegmentReader.codeco  s    {{r   c                     | j                   S r"   )r   r;   s    r   r^   zSegmentReader.segmentr      }}r   c                     | j                   S r"   )r   r;   s    r   r`   zSegmentReader.storageu  r  r   c                 Z    | j                   rt        | j                  j                         S r"   )r   r   r  r   r;   s    r   r   zSegmentReader.has_deletionsx  "    >>||))++r   c                 Z    | j                   rt        | j                  j                         S r"   )r   r   r  r   r;   s    r   r   zSegmentReader.doc_count}  "    >>||%%''r   c                 Z    | j                   rt        | j                  j                         S r"   )r   r   r  r   r;   s    r   r   zSegmentReader.doc_count_all  r	  r   c                 \    | j                   rt        | j                  j                  |      S r"   )r   r   r  r   r   s     r   r   zSegmentReader.is_deleted  s$    >>||&&v..r   c                     | j                   S r"   r   r;   s    r   rm   zSegmentReader.generation      yyr   c                 h    | j                   j                  d| j                  d| j                  dS )N(z, ))	__class__r   r   r   r;   s    r   __repr__zSegmentReader.__repr__  s&    #~~66#}}. 	.r   c                     | j                   rt        |\  }}|| j                  vry| j                  ||      }||f| j                  v S r   )r   r   re   rj   r   )r*   rW   rh   ri   s       r   rX   zSegmentReader.__contains__  sM    >>	4DKK'""9d34 DKK//r   c                     | j                   rt        d      | j                  j                          | j                  j                          | j
                  r| j
                  j                          d| _         y )NzReader already closedT)r   r   r   rP   r  r   r;   s    r   rP   zSegmentReader.close  sU    >>677 ==MM!r   c                     | j                   rt        |dk\  sJ | j                  | j                  j	                  |      }t        fdt        |      D              S )Nr   c              3   2   K   | ]  }|d    v s|  ywr   Nr   )r   itemre   s     r   r   z.SegmentReader.stored_fields.<locals>.<genexpr>  s     ITtAw&7HDIs   )r   r   re   r  r   dictr   )r*   r7   sfsre   s      @r   r   zSegmentReader.stored_fields  sN    >>{{ll((0IYs^IIIr   c                 Z    | j                   rt        | j                  j                         S r"   )r   r   r  r   r;   s    r   r   zSegmentReader.all_doc_ids  s"    >>||''))r   c                 Z    | j                   rt        | j                  j                         S r"   )r   r   r  r   r;   s    r   r   zSegmentReader.iter_docs  r  r   c                 Z    | j                   rt        | j                  j                         S r"   )r   r   r  r   r;   s    r   r   zSegmentReader.all_stored_fields  s"    >>||--//r   c                 \    | j                   rt        | j                  j                  |      S r"   )r   r   r  r   r   s     r   r   zSegmentReader.field_length  s$    >>||((33r   c                 \    | j                   rt        | j                  j                  |      S r"   )r   r   r  r   r   s     r   r   zSegmentReader.min_field_length  $    >>||,,Y77r   c                 \    | j                   rt        | j                  j                  |      S r"   )r   r   r  r   r   s     r   r   zSegmentReader.max_field_length  r#  r   c                 `    | j                   rt        | j                  j                  |||      S r"   )r   r   r  r   r   s       r   r   zSegmentReader.doc_field_length  s(    >>||,,VYHHr   c                 ^    | j                   rt        | j                  j                  ||      S r"   )r   r   r  r   r   s      r   r   zSegmentReader.has_vector  s&    >>||&&vy99r   c                     | j                   rt        || j                  vrt        d|z        | j                  |   j                  t        d|z        y )NzNo field %rzField %r is not indexed)r   r   re   r   r   r   s     r   _test_fieldzSegmentReader._test_field  sV    >>DKK'}y899;;y!((089DEE 1r   c                 6    | j                   j                         S r"   )r   ro   r;   s    r   ro   z!SegmentReader.indexed_field_names  s    {{..00r   c                     | j                   rt        | j                  fd| j                  j	                         D        S )Nc              3   4   K   | ]  \  }}|v r||f  y wr"   r   )r   rh   ri   re   s      r   r   z*SegmentReader.all_terms.<locals>.<genexpr>  s)      (oi& D! (   )r   r   re   r   termsr*   re   s    @r   rq   zSegmentReader.all_terms  s7    >>(9J9J9L ( 	(r   c                     | j                  |       | j                  ||      }| j                  fd| j                  j	                  ||      D        S )Nc              3   4   K   | ]  \  }}|v r||f  y wr"   r   )r   rt   ri   re   s      r   r   z+SegmentReader.terms_from.<locals>.<genexpr>  s'      $+%F?  $r,  )r(  rj   re   r   ru   )r*   rh   rs   re   s      @r   ru   zSegmentReader.terms_from  sO    #$$Y7$;;)))V<$ 	$r   c                     | j                  |       | j                  ||      }	 | j                  j                  ||      S # t        $ r t        |d|      w xY wN:)r(  rj   r   rx   KeyErrorr   rg   s      r   rx   zSegmentReader.term_info  s]    #""9d3	<;;((D99 	<)T:;;	<s   A Ac                 v    | j                  |       | j                  ||      }t        j                  | ||      S r"   )r(  rj   rK   r}   r*   rh   rs   s      r   r}   zSegmentReader.expand_prefix   s7    #$$Y7((y&AAr   c                 P    | j                  |       t        j                  | |      S r"   )r(  rK   r   r   s     r   r   zSegmentReader.lexicon  s#    #""433r   c                     | j                   rt        | j                  fd| j                  j	                         D        S )Nc              3   :   K   | ]  \  }}|d    v r||f  ywr  r   )r   rW   r   re   s      r   r   z)SegmentReader.__iter__.<locals>.<genexpr>  s-      &^T87f$ x  &s   )r   r   re   r   itemsr.  s    @r   r   zSegmentReader.__iter__	  s7    >>&t{{7H7H7J & 	&r   c              #      K   | j                  |       | j                  }| j                  ||      }| j                  j	                  ||      D ]  \  }}|d   |vr||f  y wNr   )r(  re   rj   r   
items_from)r*   rh   ri   re   rW   r   s         r   r   zSegmentReader.iter_from  sm     #""9d3"kk44YE 	#ND(Awf$""	#s   A#A%c                     | j                  |       | j                  ||      }	 | j                  j                  ||      S # t        $ r Y yw xY wr<  )r(  rj   r   r   r4  rg   s      r   r   zSegmentReader.frequency  sP    #""9d3	;;((D99 		   A 	AAc                     | j                  |       | j                  ||      }	 | j                  j                  ||      S # t        $ r Y yw xY wr<  )r(  rj   r   r=   r4  rg   s      r   r=   zSegmentReader.doc_frequency!  sP    #""9d3	;;,,Y== 		r?  c                 b   ddl m} | j                  rt        || j                  vrt        d|z        | j                  ||      }| j                  |   j                  }| j                  j                  ||||      }t        | j                  j                               }|r |||d      }|S )Nr   )FilterMatcherNo  field %r)scorerT)exclude)whoosh.matching.wrappersrB  r   r   re   r   rj   r   r   matcher	frozensetr  deleted_docs)r*   rh   ri   rD  rB  r   rG  deleteds           r   r   zSegmentReader.postings)  s    :>>DKK'~	9::""9d3++i(//++%%iwv%NDLL5578#GWdCGr   c                     | j                   rt        || j                  vrt        d|z        |xs | j                  |   j                  }|st        d|z        | j                  j	                  |||      S )NrC  z"No vectors are stored for field %r)r   r   re   r   r   	Exceptionr  )r*   r7   rh   r   vformats        r   r   zSegmentReader.vector8  sq    >>DKK'~	9:::T[[3::@9LMM||""69g>>r   c                 |    | j                   rt        | j                  |   }| j                  j	                  ||      S r"   )r   r   re   r   cursor)r*   rh   r   s      r   rO  zSegmentReader.cursorB  s4    >>;;y){{!!)X66r   c                     | j                   |   }|j                  |      }| j                  j                  | j                  | j
                        }| j                  |      }|j                  ||||      S r"   )re   spelling_fieldnamer   automatar   r   rO  r   )	r*   rh   ri   r   rs   r   
spellfieldautofieldcurs	            r   r   zSegmentReader.terms_withinH  se     ;;y)00;
{{##DMM4==A;;z*  4&AAr   c                     | j                   rt        | j                  |   j                  }|xr | j                  j                  |      S r"   )r   r   re   column_typer  r   )r*   rh   coltypes      r   r   zSegmentReader.has_columnT  s=    >>++i(44=4<<229==r   c                    | j                   rt        | j                  |   }|xs |j                  }|st	        d|d|       | j
                  j                  |      r/| j
                  j                  ||      }|rF|j                          n5|j                  |      }t        j                  || j                               }|r"|j                  }t        j                  ||      }|S )NzNo column for field z in )r   r   re   rW  rL  r  r   r   set_reversedefault_valuer	   EmptyColumnReaderr   from_column_valueTranslatingColumnReader)	r*   rh   r   r   r   r   creaderr   fcvs	            r   r   zSegmentReader.column_readerZ  s    >>;;y)/8//($0 1 1 <<""9-ll00FCG##% **73G//9K9K9MNG ,,C55gsCGr   )NNr   r"   NFT))r   r   r   r2   r[   r^   r`   r   r   r   r   rm   r  rX   rP   r   r   r   r   r   r   r   r   r   r(  ro   rq   ru   rx   r}   r   r   r   r   r=   r   r   rO  r   r   r   r   r   r   r   r   T  s    O4,
(
,
/
.0J*
(
0
4
8
8
I
:F1($<B
4&#?7B> =B $r   r   c                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
dd	Zdd
Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZddZd dZd Zd dZd!dZd"dZy)#EmptyReaderc                     || _         y r"   )re   r.  s     r   r2   zEmptyReader.__init__{  s	    r   c                      yr   r   rV   s     r   rX   zEmptyReader.__contains__~  r   r   c                     t        g       S r"   iterr;   s    r   r   zEmptyReader.__iter__      Bxr   c                     ddl m}  |       S )Nr   )EmptyCursor)whoosh.codec.baserk  )r*   rh   rk  s      r   rO  zEmptyReader.cursor  s    1}r   c                     g S r"   r   r;   s    r   ro   zEmptyReader.indexed_field_names  s    	r   c                     t        g       S r"   rg  r;   s    r   rq   zEmptyReader.all_terms  ri  r   c                     t        ||f      r"   r   rg   s      r   rx   zEmptyReader.term_info  s    It,--r   c                     t        g       S r"   rg  rg   s      r   r   zEmptyReader.iter_from  ri  r   c                     t        g       S r"   rg  r6  s      r   r   zEmptyReader.iter_field  ri  r   c                     t        g       S r"   rg  r6  s      r   r   zEmptyReader.iter_prefix  ri  r   c                     t        g       S r"   rg  r   s     r   r   zEmptyReader.lexicon  ri  r   c                      yr   r   r;   s    r   r   zEmptyReader.has_deletions  r   r   c                      yr   r   r   s     r   r   zEmptyReader.is_deleted  r   r   c                     t        d|z        NzNo document number %sr4  r   s     r   r   zEmptyReader.stored_fields      .788r   c                     t        g       S r"   rg  r;   s    r   r   zEmptyReader.all_stored_fields  ri  r   c                      yr<  r   r;   s    r   r   zEmptyReader.doc_count_all      r   c                      yr<  r   r;   s    r   r   zEmptyReader.doc_count  r}  r   c                      yr<  r   rg   s      r   r   zEmptyReader.frequency  r}  r   c                      yr<  r   rg   s      r   r=   zEmptyReader.doc_frequency  r}  r   c                      yr<  r   r   s     r   r   zEmptyReader.field_length  r}  r   c                      yr<  r   r   s     r   r   zEmptyReader.min_field_length  r}  r   c                      yr<  r   r   s     r   r   zEmptyReader.max_field_length  r}  r   c                     |S r"   r   r   s       r   r   zEmptyReader.doc_field_length  s    r   Nc                 "    t        |d|      r2  rp  )r*   rh   ri   rD  s       r   r   zEmptyReader.postings  s    i677r   c                      yr   r   r   s      r   r   zEmptyReader.has_vector  r   r   c                     t        d|z        rx  ry  r   s       r   r   zEmptyReader.vector  rz  r   c                     t        g       S r"   rg  r*   rh   r   rs   s       r   r   zEmptyReader.most_frequent_terms  ri  r   c                     t        g       S r"   rg  r  s       r   r   z"EmptyReader.most_distinctive_terms  ri  r   r   r   r"   r   )r   N)r   r   r   r2   rX   r   rO  ro   rq   rx   r   r   r   r   r   r   r   r   r   r   r   r=   r   r   r   r   r   r   r   r   r   r   r   r   rc  rc  z  s    
.989r   rc  c                       e Zd ZdZd'dZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z	 	 d(dZd Zd Zd Z d  Z!d! Z"d" Z#d)d#Z$d$ Z%d'd%Z&d& Z'y)*MultiReaderrL   Nc                 6   || _         || _        d | _        |r|d   j                  | _        g | _        d| _        | j                   D ]J  }| j                  j                  | j                         | xj                  |j                         z  c_        L d| _        y )Nr   F)readersr   re   doc_offsetsbaseappendr   r   )r*   r  rm   rs       r   r2   zMultiReader.__init__  s    	!!*++DK	 	+A##DII.II**I	+ r   c                 H    t        dt        | j                  |      dz
        S )Nr   r4   )r5   r   r  r   s     r   _document_segmentzMultiReader._document_segment  s!    1l4#3#3V<q@AAr   c                 P    | j                  |      }| j                  |   }|||z
  fS r"   )r  r  )r*   r7   
segmentnumoffsets       r   _segment_and_docnumzMultiReader._segment_and_docnum  s1    ++F3
!!*-6F?**r   c                 p    t        | j                  D cg c]  }|j                  |       c}      S c c}w r"   )MultiCursorr  rO  )r*   rh   r  s      r   rO  zMultiReader.cursor  s(    FAAHHY/FGGFs   3c                      yr   r   r;   s    r   rc   zMultiReader.is_atomic  r   r   c                 B    t        | j                  | j                        S r"   )r   r  r  r;   s    r   r   zMultiReader.leaf_readers  s    DLL$"2"233r   c                     | j                   j                  |       | j                  j                  | j                         | xj                  |j	                         z  c_        y r"   )r  r  r  r  r   )r*   readers     r   
add_readerzMultiReader.add_reader  sC    F#		*		V))++	r   c                 T    | j                   D ]  }|j                           d| _        y rb   )r  rP   r   )r*   ds     r   rP   zMultiReader.close  s%     	AGGI	r   c                     | j                   S r"   r  r;   s    r   rm   zMultiReader.generation  r  r   c                 T    | j                   D ]  }|j                  |      }||c S  y r"   )r  r   )r*   rh   r  fmts       r   r   zMultiReader.format  s-     	A((9%C
	r   c                 T    | j                   D ]  }|j                  |      }||c S  y r"   )r  vector_format)r*   rh   r  vfmts       r   r  zMultiReader.vector_format  s.     	A??9-D	r   c                 @    t        fd| j                  D              S )Nc              3   @   K   | ]  }|j                          y wr"   )rX   )r   r  rW   s     r   r   z+MultiReader.__contains__.<locals>.<genexpr>  s     >A1>>$'>   anyr  rV   s    `r   rX   zMultiReader.__contains__  s    >>>>r   c              #     K   i }|D ]  }||t        |      <    g }|D ]*  }	 t        |      }|j                  |t        |      f       , t	        |      }|dk(  r|d   \  }}||   }| |D ]  }|  y t        |       |rY|d   d   }|rG|d   d   |k(  r<||d   d      }	 t        |      }t        ||t        |      f       |r|d   d   |k(  r<| |rXy y # t        $ r Y w xY w# t        $ r t        |       |dz  }Y Aw xY ww)Nr4   r   )r   r   StopIterationr  lenr   r   r   )	r*   iterlistitermapitcurrentrW   activeitidnextterms	            r   _merge_termszMultiReader._merge_terms  s^      	!B GBrFO	!
  	+BBx NND"R&>*	+ W Q; JD$BJ 
 	1:a=D WQZ]d2WQZ]+ #BxH(BrF);<	 WQZ]d2 J # ! 6 %  G$aKF sX   DCA8D&"C. DDD	C+(D*C++D.D
D	D

Dc                     t               }| j                  D ]!  }|j                  |j                                # t	        |      S r"   )setr  updatero   rh  )r*   namesr  s      r   ro   zMultiReader.indexed_field_namesF  s;     	2ALL..01	2E{r   c                 z    | j                  | j                  D cg c]  }|j                          c}      S c c}w r"   )r  r  rq   )r*   r  s     r   rq   zMultiReader.all_termsL  s+      !FA!++-!FGG!Fs   8c           	      ~    | j                  | j                  D cg c]  }|j                  ||       c}      S c c}w r"   )r  r  ru   )r*   rh   rs   r  s       r   ru   zMultiReader.terms_fromO  s?      +/<<"9&' #$,,y&"A "9 : 	: "9s   :c                     ||f}t        | j                  | j                        D cg c]  \  }}||v r|j                  ||      |f }}}|st	        |      t        |      S c c}}w r"   )r   r  r  rx   r   combine_terminfos)r*   rh   ri   rW   r  r  tiss          r   rx   zMultiReader.term_infoS  sz    4  t||T%5%56E)!V:>!) It,f5 E E
 t$$ %%Es   "A%c                 D    t        fd| j                  D              S )Nc              3   B   K   | ]  }|j                          y wr"   )r   r   r  rh   ri   s     r   r   z(MultiReader.frequency.<locals>.<genexpr>b  s     FA1;;y$/F   sumr  rg   s    ``r   r   zMultiReader.frequencya  s    FFFFr   c                 D    t        fd| j                  D              S )Nc              3   B   K   | ]  }|j                          y wr"   r=   r  s     r   r   z,MultiReader.doc_frequency.<locals>.<genexpr>e  s     J1??9d3Jr  r  rg   s    ``r   r=   zMultiReader.doc_frequencyd  s    JT\\JJJr   c                    g }g }||f}t        | j                        D ]M  \  }}||v s| j                  |   }|j                  ||      }	|j	                  |	       |j	                  |       O |st        ||      t        ||      S r"   )	enumerater  r  r   r  r   r   )
r*   rh   ri   postreaders
docoffsetsrW   ir  r  prs
             r   r   zMultiReader.postingsg  s     
4 dll+ 	*DAqqy))!,ZZ	40""2&!!&)	* y$//K44r   c                     t        | j                        D ]:  \  }}	 |j                  ||      }|t        ||f      | j                  |   |z   c S  t        ||f      # t
        t        f$ r Y [w xY wr"   )r  r  r   r   r  r4  )r*   rh   ri   r  r  r   s         r   r   zMultiReader.first_idz  s    dll+ 		4DAq4ZZ	40 :&	4'899++A.33		4 It,-- l+ s   A  A21A2c                 :    t        d | j                  D              S )Nc              3   <   K   | ]  }|j                           y wr"   )r   )r   r  s     r   r   z,MultiReader.has_deletions.<locals>.<genexpr>  s     ;1??$;   r  r;   s    r   r   zMultiReader.has_deletions  s    ;dll;;;r   c                 f    | j                  |      \  }}| j                  |   j                  |      S r"   )r  r  r   r*   r7   r  
segmentdocs       r   r   zMultiReader.is_deleted  s1    !%!9!9&!A
J||J'22:>>r   c                 f    | j                  |      \  }}| j                  |   j                  |      S r"   )r  r  r   r  s       r   r   zMultiReader.stored_fields  s1    !%!9!9&!A
J||J'55jAAr   c                 @    t        fd| j                  D              S )Nc              3   @   K   | ]  }|j                          y wr"   )r   r   r  rh   s     r   r   z)MultiReader.has_column.<locals>.<genexpr>  s     Aq1<<	*Ar  r  r   s    `r   r   zMultiReader.has_column  s    ADLLAAAr   c                    g }g }t        | j                        D ][  \  }}|j                  |      s|j                  ||||      }	|j	                  |	       |j	                  | j
                  |          ] t        j                  ||      S )N)r   r   r   )r  r  r   r   r  r  r	   MultiColumnReader)
r*   rh   r   r   r   crsr  r  r  crs
             r   r   zMultiReader.column_reader  s    dll+ 	8DAq||I&__Yvw/8 % :

2""4#3#3A#67	8 ((k::r   c              #   `   K   | j                   D ]  }|j                         D ]  }|   y wr"   )r  r   )r*   r  results      r   r   zMultiReader.all_stored_fields  s6     ll 	F 224 	r   c                 :    t        d | j                  D              S )Nc              3   <   K   | ]  }|j                           y wr"   )r   r   drs     r   r   z,MultiReader.doc_count_all.<locals>.<genexpr>  s     ="2##%=r  r  r;   s    r   r   zMultiReader.doc_count_all  s    ====r   c                 :    t        d | j                  D              S )Nc              3   <   K   | ]  }|j                           y wr"   )r   r  s     r   r   z(MultiReader.doc_count.<locals>.<genexpr>  s     9b2<<>9r  r  r;   s    r   r   zMultiReader.doc_count  s    9DLL999r   c                 @    t        fd| j                  D              S )Nc              3   @   K   | ]  }|j                          y wr"   )r   )r   r  rh   s     r   r   z+MultiReader.field_length.<locals>.<genexpr>  s     E"2??9-Er  r  r   s    `r   r   zMultiReader.field_length  s    EEEEr   c                 @    t        fd| j                  D              S )Nc              3   @   K   | ]  }|j                          y wr"   )r   r  s     r   r   z/MultiReader.min_field_length.<locals>.<genexpr>       GQ1%%i0Gr  )r6   r  r   s    `r   r   zMultiReader.min_field_length      G$,,GGGr   c                 @    t        fd| j                  D              S )Nc              3   @   K   | ]  }|j                          y wr"   )r   r  s     r   r   z/MultiReader.max_field_length.<locals>.<genexpr>  r  r  )r5   r  r   s    `r   r   zMultiReader.max_field_length  r  r   c                 p    | j                  |      \  }}| j                  |   }|j                  |||      S )N)r   )r  r  r   )r*   r7   rh   r   r  r  r  s          r   r   zMultiReader.doc_field_length  s=    !%!9!9&!A
Jj)&&z9g&NNr   c                 h    | j                  |      \  }}| j                  |   j                  ||      S r"   )r  r  r   )r*   r7   rh   r  r  s        r   r   zMultiReader.has_vector  s3    !%!9!9&!A
J||J'22:yIIr   c                 h    | j                  |      \  }}| j                  |   j                  ||      S r"   )r  r  r   )r*   r7   rh   r   r  r  s         r   r   zMultiReader.vector  s3    !%!9!9&!A
J||J'..z9EEr   c                 j    | j                  |      \  }}| j                  |   j                  |||      S r"   )r  r  r   )r*   r   r7   rh   r  r  s         r   r   zMultiReader.vector_as  s:    !%!9!9&!A
J||J'11&*2;= 	=r   r"   ra  r   )(r   r   r   r   r2   r  r  rO  rc   r   r  rP   rm   r   r  rX   r  ro   rq   ru   rx   r   r=   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r    s    B+
H4,

?.`H:&GK5&. <?BB =B $
;
>:FHHO
JF=r   r  c           	         t        |       dk(  r4| d   \  }}|xj                  |z  c_        |xj                  |z  c_        |S t        d | D              }t        d | D              }t	        d | D              }t        d | D              }t        d | D              }t	        d | D              }t        d	 | D              }	t        |||||||	      S )
Nr4   r   c              3   B   K   | ]  \  }}|j                           y wr"   r   r   ti_s      r   r   z$combine_terminfos.<locals>.<genexpr>  s     )EBBIIK)   c              3   B   K   | ]  \  }}|j                           y wr"   r  r  s      r   r   z$combine_terminfos.<locals>.<genexpr>  s     1EBR1r  c              3   B   K   | ]  \  }}|j                           y wr"   )r?   r  s      r   r   z$combine_terminfos.<locals>.<genexpr>       .QR]]_.r  c              3   B   K   | ]  \  }}|j                           y wr"   )rB   r  s      r   r   z$combine_terminfos.<locals>.<genexpr>  r  r  c              3   B   K   | ]  \  }}|j                           y wr"   )rD   r  s      r   r   z$combine_terminfos.<locals>.<genexpr>  r  r  c              3   H   K   | ]  \  }}|j                         |z     y wr"   )rF   r   r  r  s      r   r   z$combine_terminfos.<locals>.<genexpr>       9zr6biikF"9    "c              3   H   K   | ]  \  }}|j                         |z     y wr"   )rI   r  s      r   r   z$combine_terminfos.<locals>.<genexpr>  r   r  )r  r(   r)   r  r6   r5   r    )
r  r  r  wr,   mlxlxwmidxids
             r   r  r    s    
3x1}V
F
		V	
		V		 	)S))A	1S1	1B	.#.	.B	.#.	.B	.#.	.B 9S9
9C
9S9
9CAr2r2sC00r   c                   6    e Zd Zd Zd Zd Zd Zd Zd Zd Z	y)	r  c                     |D cg c]  }|j                         s| c}| _        g | _        d | _        | j	                          y c c}w r"   )is_valid_cursors_low_textr   )r*   cursorscs      r   r2   zMultiCursor.__init__  s8    $+<qqzz|<	
		 =s
   AAc                     g }d }| j                   D ]J  }|j                         s|j                         }|r||k(  r|j                  |       =|s@||k  sF|g}|}L || _        || _        |S r"   )r  r  rW   r  r  r  )r*   lowlowtermr  cterms        r   	_find_lowzMultiCursor._find_low  sr     	$Azz|5G+JJqMUW_#C#G	$ 	
r   c                 d    | j                   D ]  }|j                           | j                         S r"   )r  firstr  r*   r  s     r   r  zMultiCursor.first  s+     	AGGI	~~r   c                 f    | j                   D ]  }|j                  |        | j                         S r"   )r  findr  )r*   rW   r  s      r   r  zMultiCursor.find  s-     	AFF4L	~~r   c                 d    | j                   D ]  }|j                           | j                         S r"   )r  r   r  r  s     r   r   zMultiCursor.next  s+     	AFFH	~~r   c                 x    | j                   D cg c]  }|j                          }}|rt        |      S d S c c}w r"   )r  rx   r  )r*   r  r  s      r   rx   zMultiCursor.term_info  s7    &*ii0q{{}00), %6$6 1s   7c                 :    t        d | j                  D              S )Nc              3   <   K   | ]  }|j                           y wr"   )r  )r   r  s     r   r   z'MultiCursor.is_valid.<locals>.<genexpr>  s     7A1::<7r  )r  r  r;   s    r   r  zMultiCursor.is_valid  s    7777r   N)
r   r   r   r2   r  r  r  r   rx   r  r   r   r   r  r    s%    " 
 
 
78r   r  N)%r   mathr   bisectr   heapqr   r   r   r   whooshr	   whoosh.compatr
   r   r   r   r   whoosh.filedb.filestorer   whoosh.matchingr   whoosh.support.levenshteinr   whoosh.systemr   rL  r   r   objectr    rK   r   rc  r  r  r  r   r   r   <module>r)     s   8   9 9  ( 7 7 2 ( / $
-9 -	9 	Jv J^D"& D"RaK aL	U+ Utx=+ x=v1*,8& ,8r   