
    =wg                     "   d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	m
Z
mZ ddlmZmZmZmZ ddlmZ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 y)zKThis module contains classes and functions related to searching the index.
    )divisionN)ceil)classify	highlightqueryscoring)	iteritems
itervaluesiterkeysxrange)DocIdSetBitSet)TermNotFound)	lru_cachec                       e Zd ZdZdZy)NoTermsExceptionzException raised you try to access matched terms on a :class:`Results`
    object was created without them. To record which terms matched in which
    document, you need to call the :meth:`Searcher.search` method with
    ``terms=True``.
    z,Results were created without recording termsN)__name__
__module____qualname____doc__message     G/var/www/horilla/myenv/lib/python3.12/site-packages/whoosh/searching.pyr   r   ,   s     =Gr   r   c                       e Zd ZdZy)	TimeLimitzRaised by :class:`TimeLimitedCollector` if the time limit is reached
    before the search finishes. If you have a reference to the collector, you
    can get partial results by calling :meth:`TimeLimitedCollector.results`.
    N)r   r   r   r   r   r   r   r   r   6   s    
 	r   r   c                   (    e Zd ZdZ	 	 ddZd Zd Zy)SearchContextzA container for information about the current search that may be used
    by the collector or the query objects to change how they operate.
    Nc                 <    || _         || _        || _        || _        y)a  
        :param needs_current: if True, the search requires that the matcher
            tree be "valid" and able to access information about the current
            match. For queries during matcher instantiation, this means they
            should not instantiate a matcher that doesn't allow access to the
            current match's value, weight, and so on. For collectors, this
            means they should advanced the matcher doc-by-doc rather than using
            shortcut methods such as all_ids().
        :param weighting: the Weighting object to use for scoring documents.
        :param top_query: a reference to the top-level query object.
        :param limit: the number of results requested by the user.
        N)needs_current	weighting	top_querylimit)selfr    r!   r"   r#   s        r   __init__zSearchContext.__init__F   s!     +""
r   c                 N    | j                   j                  d| j                  dS )N())	__class__r   __dict__r$   s    r   __repr__zSearchContext.__repr__Z   s    >>22DMMBBr   c                 f    t        j                   |       }|j                  j                  |       |S N)copyr*   update)r$   kwargsctxs      r   setzSearchContext.set]   s&    iioF#
r   )FNNr   )r   r   r   r   r%   r,   r3   r   r   r   r   r   A   s!     GK(Cr   r   c                      e Zd ZdZej
                  dddf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d1dZd Zd Zd Zd2dZd Zd Zd Zd Zd Zd Z d Z!d  Z"d! Z#d" Z$d3d$Z%d#e&jN                  dfd%Z(d#e&jN                  dfd&Z)dd'd#e&jN                  d(dfd)Z*d4d*Z+d+ Z,d5d,Z-	 	 	 	 d6d-Z.d. Z/d1d/Z0	 	 d7d0Z1y)8SearcherzkWraps an :class:`~whoosh.reading.IndexReader` object and provides
    methods for searching the index.
    TNc           	      &   || _         d| _        || _        || _        | j                   j	                         | _        i | _        |rNt        j                  |      | _	        |j                  | _
        |j                  | _        |j                  | _        n0d| _	        | j                   j                  | _
        i | _        i | _        t        |      t        u r |       | _        n|| _        d| _        d| _        | j                   j#                         sT| j                   j%                         | _        | j                  D cg c]  \  }}| j'                  |      |f c}}| _        dD ]#  }t)        | |t+        | j                   |             % yc c}}w )a  
        :param reader: An :class:`~whoosh.reading.IndexReader` object for
            the index to search.
        :param weighting: A :class:`whoosh.scoring.Weighting` object to use to
            score found documents.
        :param closereader: Whether the underlying reader will be closed when
            the searcher is closed.
        :param fromindex: An optional reference to the index of the underlying
            reader. This is required for :meth:`Searcher.up_to_date` and
            :meth:`Searcher.refresh` to work.
        FN)stored_fieldsall_stored_fields
has_vectorvector	vector_aslexiconfield_terms	frequencydoc_frequency	term_infodoc_field_length	corrector	iter_docs)ixreader	is_closed_closereader_ixdoc_count_all	_doccount_field_cachesweakrefrefparentschema
_idf_cache_filter_cachetyper!   leafreaderssubsearchers	is_atomicleaf_readers_subsearchersetattrgetattr)	r$   readerr!   closereader	fromindexrM   roffsetnames	            r   r%   zSearcher.__init__j   sO    '446!++f-DK --DK$//DO!'!5!5DDK--..DK DO!#D	?d"&[DN&DN }}&&(#}}99;D$($4$4!6IAv$"3"3A"6!? !6DC 	>D D$t <=		>	!6s   >Fc                     | S r.   r   r+   s    r   	__enter__zSearcher.__enter__   s    r   c                 $    | j                          y r.   )close)r$   exc_infos     r   __exit__zSearcher.__exit__   s    

r   c                 T    | j                  || j                  | j                  |       S )N)r[   r!   rM   )r)   rG   r!   )r$   rY   s     r   rV   zSearcher._subsearcher   s+    ~~f(,t  E 	Er   c                 <    | j                   D ]  \  }}||u s|c S  y r.   )rS   )r$   subsearcherssr]   s       r   _offset_for_subsearcherz Searcher._offset_for_subsearcher   s(    ++ 	JB[ 	r   c                 D    | j                         r| dfgS | j                  S )Nr   )rT   rS   r+   s    r   leaf_searcherszSearcher.leaf_searchers   s$    >>1I;$$$r   c                 >    | j                         j                         S r.   )rY   rT   r+   s    r   rT   zSearcher.is_atomic   s    {{}&&((r   c                     | j                   d uS r.   )rM   r+   s    r   
has_parentzSearcher.has_parent   s    {{$&&r   c                 F    | j                         r| j                         S | S )z]Returns the parent of this searcher (if has_parent() is True), or
        else self.
        )rn   rM   r+   s    r   
get_parentzSearcher.get_parent   s    
 ??;;= Kr   c                 6    | j                   j                         S )z@Returns the number of UNDELETED documents in the index.
        )rD   	doc_countr+   s    r   rr   zSearcher.doc_count   s     }}&&((r   c                     | j                   S )z[Returns the total number of documents, DELETED OR UNDELETED, in
        the index.
        )rI   r+   s    r   rH   zSearcher.doc_count_all   s    
 ~~r   c                     | j                         r| j                         j                  |      S | j                         j                  |      S r.   )rn   rp   field_lengthrY   r$   	fieldnames     r   ru   zSearcher.field_length   s;    ????$11)<<;;=--i88r   c                     | j                         r| j                         j                  |      S | j                         j                  |      S r.   )rn   rp   max_field_lengthrY   rv   s     r   ry   zSearcher.max_field_length   s;    ????$55i@@;;=11)<<r   c                     | j                   st        d      | j                   j                         | j                  j	                         k(  S )zReturns True if this Searcher represents the latest version of the
        index, for backends that support versioning.
        No reference to index)rG   	Exceptionlatest_generationrD   
generationr+   s    r   
up_to_datezSearcher.up_to_date   s<    
 xx344xx))+t}}/G/G/IIIr   c                 P   | j                   st        d      | j                   j                         | j                         j	                         k(  r| S d| _        | j                   j                  | j                        }| j                  || j                   | j                        S )a  Returns a fresh searcher for the latest version of the index::

            my_searcher = my_searcher.refresh()

        If the index has not changed since this searcher was created, this
        searcher is simply returned.

        This method may CLOSE underlying resources that are no longer needed
        by the refreshed searcher, so you CANNOT continue to use the original
        searcher after calling ``refresh()`` on it.
        r{   T)reuse)r[   r!   )	rG   r|   r}   rY   r~   rE   rD   r)   r!   )r$   	newreaders     r   refreshzSearcher.refresh   s     xx34488%%'4;;=+C+C+EEK HHOO$--O8	~~i488(,  8 	8r   c                 ^    | j                   r| j                  j                          d| _        y )NT)rF   rD   rb   rE   r+   s    r   rb   zSearcher.close   s"    MM!r   c                 |    | j                   |   j                  s|S | j                  |      | j                  xs dz  S N   )rN   scorableru   rI   )r$   rw   defaults      r   avg_field_lengthzSearcher.avg_field_length  s8    {{9%..N  +t~~/BCCr   c                     | j                   S )zEReturns the underlying :class:`~whoosh.reading.IndexReader`.
        )rD   r+   s    r   rY   zSearcher.reader	  s     }}r   c                 >    d|vr| j                   |d<   t        di |S )z>Generates a :class:`SearchContext` for this searcher.
        r!   r   )r!   r   )r$   r1   s     r   contextzSearcher.context  s(     f$"&..F;&v&&r   c                 (    | j                  dd      S )zWShortcut returns a SearchContext set for unscored (boolean)
        searching.
        FN)r    r!   )r   r+   s    r   boolean_contextzSearcher.boolean_context  s    
 ||%4|@@r   c                    |xs | j                   }|j                  | |||      }| j                         r| j                  j	                  |||      S ddlm} g }g }||f}	| j                  D ]e  \  }
}|
j                         }|	|v s|j                  |
|||      }|j	                  |||      }|j                  |       |j                  |       g |st        ||       ||||      S )a  Returns a :class:`whoosh.matching.Matcher` for the postings of the
        given term. Unlike the :func:`whoosh.reading.IndexReader.postings`
        method, this method automatically sets the scoring functions on the
        matcher from the searcher's weighting object.
        )qf)scorerr   )MultiMatcher)r!   r   rT   rD   postingswhoosh.matchingr   rS   rY   appendr   )r$   rw   textr!   r   globalscorerr   matchers
docoffsetstermrg   r]   r\   r   ms                  r   r   zSearcher.postings  s    /	 ''i"'E>>==)))T,)OO4HJt$D'+'8'8 .#V&&(19 '--k9dr-RF

9d6
BAOOA&%%f-. "9d33*lCCr   c                 |    | j                   }||f}||v r||   S | j                  j                  | ||      }|||<   |S )zCalculates the Inverse Document Frequency of the current term (calls
        idf() on the searcher's Weighting object).
        )rO   r!   idf)r$   rw   r   cacher   r   s         r   r   zSearcher.idf?  sN     4 5=;nn  y$7d
r   c                 8     | j                   di |D ]  }|c S  y)a3  Convenience method returns the stored fields of a document
        matching the given keyword arguments, where the keyword keys are
        field names and the values are terms that must appear in the field.

        This method is equivalent to::

            searcher.stored_fields(searcher.document_number(<keyword args>))

        Where Searcher.documents() returns a generator, this function returns
        either a dictionary or None. Use it when you assume the given keyword
        arguments either match zero or one documents (i.e. at least one of the
        fields is a unique key).

        >>> stored_fields = searcher.document(path=u"/a/b")
        >>> if stored_fields:
        ...   print(stored_fields['title'])
        ... else:
        ...   print("There is no document with the path /a/b")
        Nr   )	documents)r$   kwps      r   documentzSearcher.documentQ  s&    *  %"% 	AH	r   c                 R    | j                   fd | j                  di |D        S )af  Convenience method returns the stored fields of a document
        matching the given keyword arguments, where the keyword keys are field
        names and the values are terms that must appear in the field.

        Returns a generator of dictionaries containing the stored fields of any
        documents matching the keyword arguments. If you do not specify any
        arguments (``Searcher.documents()``), this method will yield **all**
        documents.

        >>> for stored_fields in searcher.documents(emailto=u"matt@whoosh.ca"):
        ...   print("Email subject:", stored_fields['subject'])
        c              3   @   K   | ]  }j                  |        y wr.   )r7   ).0docnumrD   s     r   	<genexpr>z%Searcher.documents.<locals>.<genexpr>x  s$      ; &&v. ;s   r   )rD   document_numbers)r$   r   rD   s     @r   r   zSearcher.documentsi  s0     ==;3d339b9; 	;r   c                 p    t        |      D ](  \  }}| j                  |   }|j                  |      ||<   * y r.   )r	   rN   to_bytes)r$   r   kvfields        r   _kw_to_textzSearcher._kw_to_text{  s8    bM 	&DAqKKNENN1%BqE	&r   c                     g }t        |      D ]*  \  }}|j                  t        j                  ||             , |r%t        j                  |      j                         }|S t        j                         }|S r.   )r	   r   r   TermAnd	normalizeEvery)r$   r   
subquerieskeyvalueqs         r   _query_for_kwzSearcher._query_for_kw  sj    
#B- 	6JCejje45	6		*%//1A  Ar   c                 ~   | j                  |       t        |      dk(  r@t        |j                               d   \  }}	 | j	                         j                  ||      S | j                  |      j                  | | j                               }|j                         r|j                         S y# t        $ r Y yw xY w)a)  Returns the document number of the document matching the given
        keyword arguments, where the keyword keys are field names and the
        values are terms that must appear in the field.

        >>> docnum = searcher.document_number(path=u"/a/b")

        Where Searcher.document_numbers() returns a generator, this function
        returns either an int or None. Use it when you assume the given keyword
        arguments either match zero or one documents (i.e. at least one of the
        fields is a unique key).

        :rtype: int
        r   r   N)r   lenlistitemsrY   first_idr   r   matcherr   	is_activeid)r$   r   r   r   r   s        r   document_numberzSearcher.document_number  s    $ 	r7a<
#A&DAq{{}--a33 ""2&..tT5I5I5KLA{{}ttv 	   s    B0 0	B<;B<c                 d    | j                  |       | j                  | j                  |            S )a  Returns a generator of the document numbers for documents matching
        the given keyword arguments, where the keyword keys are field names and
        the values are terms that must appear in the field. If you do not
        specify any arguments (``Searcher.document_numbers()``), this method
        will yield **all** document numbers.

        >>> docnums = list(searcher.document_numbers(emailto="matt@whoosh.ca"))
        )r   docs_for_queryr   )r$   r   s     r   r   zSearcher.document_numbers  s.     	""4#5#5b#9::r   c                 ~    t               }|D ]-  \  }} | j                  di ||i}||j                  |       / |S )Nr   )r3   r   add)r$   uniquesdelsetr^   r   r   s         r   _find_uniquezSearcher._find_unique  sN    " 	#KD%)T)):T5M:F!

6"	# r   c                 V    t        | j                  |      | j                               S )N)size)r   r   rH   )r$   fqs     r   _query_to_combzSearcher._query_to_comb  s#    d))"-D4F4F4HIIr   c                 N   |y t        |t        t        f      r|}|S t        |t              r|j	                         }|S t        |t
              r|j                  j	                         }|S t        |t        j                        r| j                  |      }|S t        d|z        )Nz+Don't know what to do with filter object %r)
isinstancer3   r   ResultsdocsResultsPageresultsr   Queryr   r|   )r$   objcs      r   _filter_to_combzSearcher._filter_to_comb  s    ;cC?+A  W%
A  [)  "A  U[[)##C(A
  I!" # #r      c                 j    | j                         j                  |      }|j                  ||||      S )a  Returns a sorted list of suggested corrections for the given
        mis-typed word ``text`` based on the contents of the given field::

            >>> searcher.suggest("content", "specail")
            ["special"]

        This is a convenience method. If you are planning to get suggestions
        for multiple words in the same field, it is more efficient to get a
        :class:`~whoosh.spelling.Corrector` object and use it directly::

            corrector = searcher.corrector("fieldname")
            for word in words:
                print(corrector.suggest(word))

        :param limit: only return up to this many suggestions. If there are not
            enough terms in the field within ``maxdist`` of the given word, the
            returned list will be shorter than this number.
        :param maxdist: the largest edit distance from the given word to look
            at. Numbers higher than 2 are not very effective or efficient.
        :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.
        )r#   maxdistprefix)rY   rB   suggest)r$   rw   r   r#   r   r   r   s          r   r   zSearcher.suggest  s1    6 KKM##I.yyUGFyKKr   c                     t        j                  | j                  ||      }|D ]  }|j                  |        |j	                  ||      S )a3  Returns the 'numterms' most important terms from the documents
        listed (by number) in 'docnums'. You can get document numbers for the
        documents your interested in with the document_number() and
        document_numbers() methods.

        "Most important" is generally defined as terms that occur frequently in
        the top hits but relatively infrequently in the collection as a whole.

        >>> docnum = searcher.document_number(path=u"/a/b")
        >>> keywords_and_scores = searcher.key_terms([docnum], "content")

        This method returns a list of ("term", score) tuples. The score may be
        useful if you want to know the "strength" of the key terms, however to
        just get the terms themselves you can just do this:

        >>> kws = [kw for kw, score in searcher.key_terms([docnum], "content")]

        :param fieldname: Look at the terms in this field. This field must
            store vectors.
        :param docnums: A sequence of document numbers specifying which
            documents to extract key terms from.
        :param numterms: Return this number of important terms.
        :param model: The classify.ExpansionModel to use. See the classify
            module.
        :param normalize: normalize the scores.
        :returns: a list of ("term", score) tuples.
        modelr   )r   ExpanderrD   add_documentexpanded_terms)r$   docnumsrw   numtermsr   r   expanderr   s           r   	key_termszSearcher.key_terms  sO    < $$T]]IUK 	*F!!&)	*&&x9&EEr   c                     t        j                  | j                  ||      }|j                  |       |j	                  ||      S )zReturn the 'numterms' most important terms from the given text.

        :param numterms: Return this number of important terms.
        :param model: The classify.ExpansionModel to use. See the classify
            module.
        r   r   )r   r   rD   add_textr   )r$   rw   r   r   r   r   r   s          r   key_terms_from_textzSearcher.key_terms_from_text  s@     $$T]]IUK$&&x9&EEr   
   Fc	                 (   |r| j                  |||||      }	n| j                  |g||||      }	t        j                  |	D 
cg c]  \  }
}t        j                  ||
|       c}}
      }| j                  |||t        |g            S c c}}
w )a  Returns a :class:`Results` object containing documents similar to
        the given document, based on "key terms" in the given field::

            # Get the ID for the document you're interested in
            docnum = search.document_number(path=u"/a/b/c")

            r = searcher.more_like(docnum)

            print("Documents like", searcher.stored_fields(docnum)["title"])
            for hit in r:
                print(hit["title"])

        :param fieldname: the name of the field to use to test similarity.
        :param text: by default, the method will attempt to load the contents
            of the field from the stored fields for the document, or from a
            term vector. If the field isn't stored or vectored in the index,
            but you have access to the text another way (for example, loading
            from a file or a database), you can supply it using the ``text``
            parameter.
        :param top: the number of results to return.
        :param numterms: the number of "key terms" to extract from the hit and
            search for. Using more terms is slower but gives potentially more
            and more accurate results.
        :param model: (expert) a :class:`whoosh.classify.ExpansionModel` to use
            to compute "key terms".
        :param normalize: whether to normalize term weights.
        :param filter: a query, Results object, or set of docnums. The results
            will only contain documents that are also in the filter object.
        )r   r   r   )boost)r#   filtermask)r   r   r   Orr   searchr3   )r$   r   rw   r   topr   r   r   r   ktswordweightr   s                r   	more_likezSearcher.more_like   s    @ **9dX16) + MC ..&9x',	 ! CC HH*-/&$ jjD? / 0 {{1CS&]{KK/s   "B
c                 l    |dk  rt        d       | j                  |fd||z  i|}t        |||      S )aw  This method is Like the :meth:`Searcher.search` method, but returns
        a :class:`ResultsPage` object. This is a convenience function for
        getting a certain "page" of the results for the given query, which is
        often useful in web search interfaces.

        For example::

            querystring = request.get("q")
            query = queryparser.parse("content", querystring)

            pagenum = int(request.get("page", 1))
            pagelen = int(request.get("perpage", 10))

            results = searcher.search_page(query, pagenum, pagelen=pagelen)
            print("Page %d of %d" % (results.pagenum, results.pagecount))
            print("Showing results %d-%d of %d"
                  % (results.offset + 1, results.offset + results.pagelen + 1,
                     len(results)))
            for hit in results:
                print("%d: %s" % (hit.rank + 1, hit["title"]))

        (Note that results.pagelen might be less than the pagelen argument if
        there aren't enough results to fill a page.)

        Any additional keyword arguments you supply are passed through to
        :meth:`Searcher.search`. For example, you can get paged results of a
        sorted search::

            results = searcher.search_page(q, 2, sortedby="date", reverse=True)

        Currently, searching for page 100 with pagelen of 10 takes the same
        amount of time as using :meth:`Searcher.search` to find the first 1000
        results. That is, this method does not have any special optimizations
        or efficiencies for getting a page from the middle of the full results
        list. (A future enhancement may allow using previous page results to
        improve the efficiency of finding the next page.)

        This method will raise a ``ValueError`` if you ask for a page number
        higher than the number of pages in the resulting query.

        :param query: the :class:`whoosh.query.Query` object to match.
        :param pagenum: the page number to retrieve, starting at ``1`` for the
            first page.
        :param pagelen: the number of results per page.
        :returns: :class:`ResultsPage`
        r   pagenum must be >= 1r#   )
ValueErrorr   r   )r$   r   pagenumpagelenr1   r   s         r   search_pagezSearcher.search_pageL  sF    ` Q;344$++eG7W+<GG7GW55r   c                     ddl m}  ||| j                  j                        }|j	                  |      } | j
                  |fi |S )Nr   )QueryParser)rN   )whoosh.qparserr   rD   rN   parser   )r$   defaultfieldquerystringr1   r   qpr   s          r   findzSearcher.find  s?    .dmm.B.BCHH[!t{{1'''r   c              #      K   |r|j                   }n|j                  }| j                  r)| j                  D ]  \  }} ||      D ]	  }||z      y ||       D ]  }|  yw)z}Returns an iterator of document numbers for documents matching the
        given :class:`whoosh.query.Query` object.
        N)deletion_docsr   rS   )r$   r   for_deletionmethodsr]   r   s          r   r   zSearcher.docs_for_query  sv      __FVVF!.. *	6$Qi *F 6/)** !, s   A#A%c                    ddl m} ||dk  rt        d      |s|s|j                         }nV|r|j	                  |||      }n?|s|s|r|| j                         k\  r|j                  |      }n|j                  ||      }|r|j                  |||      }|r|j                  |      }|r|j                  ||||	      }|	s|
r|j                  ||	|
      }|S )
a(  Low-level method: returns a configured
        :class:`whoosh.collectors.Collector` object based on the given
        arguments. You can use this object with
        :meth:`Searcher.search_with_collector` to search.

        See the documentation for the :meth:`Searcher.search` method for a
        description of the parameters.

        This method may be useful to get a basic collector object and then wrap
        it with another collector from ``whoosh.collectors`` or with a custom
        collector of your own::

            # Equivalent of
            # results = mysearcher.search(myquery, limit=10)
            # but with a time limt...

            # Create a TopCollector
            c = mysearcher.collector(limit=10)

            # Wrap it with a TimeLimitedCollector with a time limit of
            # 10.5 seconds
            from whoosh.collectors import TimeLimitedCollector
            c = TimeLimitCollector(c, 10.5)

            # Search using the custom collector
            results = mysearcher.search_with_collector(myquery, c)
        r   )
collectorsr   zlimit must be >= 1)r#   reverse)r  )
usequality)maptype)r#   order)whooshr  r   UnsortedCollectorSortingCollectorrr   UnlimitedCollectorTopCollectorFacetCollectorTermsCollectorCollapseCollectorFilterCollector)r$   r#   sortedbyr  	groupedbycollapsecollapse_limitcollapse_orderoptimizer   r   termsr  scoredr  r   s                   r   	collectorzSearcher.collector  s    @ 	&122h,,.A++HE4; , =A'%4>>;K2K--g->A ''('CA))!Y)HA))!,A,,Q3A - CA T**1fd;Ar   c                 j     | j                   di |}| j                  ||       |j                         S )ae  Runs a :class:`whoosh.query.Query` object on this searcher and
        returns a :class:`Results` object. See :doc:`/searching` for more
        information.

        This method takes many keyword arguments (documented below).

        See :doc:`/facets` for information on using ``sortedby`` and/or
        ``groupedby``. See :ref:`collapsing` for more information on using
        ``collapse``, ``collapse_limit``, and ``collapse_order``.

        :param query: a :class:`whoosh.query.Query` object to use to match
            documents.
        :param limit: the maximum number of documents to score. If you're only
            interested in the top N documents, you can set limit=N to limit the
            scoring for a faster search. Default is 10.
        :param scored: whether to score the results. Overriden by ``sortedby``.
            If both ``scored=False`` and ``sortedby=None``, the results will be
            in arbitrary order, but will usually be computed faster than
            scored or sorted results.
        :param sortedby: see :doc:`/facets`.
        :param reverse: Reverses the direction of the sort. Default is False.
        :param groupedby: see :doc:`/facets`.
        :param optimize: use optimizations to get faster results when possible.
            Default is True.
        :param filter: a query, Results object, or set of docnums. The results
            will only contain documents that are also in the filter object.
        :param mask: a query, Results object, or set of docnums. The results
            will not contain any documents that are in the mask object.
        :param terms: if True, record which terms were found in each matching
            document. See :doc:`/searching` for more information. Default is
            False.
        :param maptype: by default, the results of faceting with ``groupedby``
            is a dictionary mapping group names to ordered lists of document
            numbers in the group. You can pass a
            :class:`whoosh.sorting.FacetMap` subclass to this keyword argument
            to specify a different (usually faster) method for grouping. For
            example, ``maptype=sorting.Count`` would store only the count of
            documents in each group, instead of the full list of document IDs.
        :param collapse: a :doc:`facet </facets>` to use to collapse the
            results. See :ref:`collapsing` for more information.
        :param collapse_limit: the maximum number of documents to allow with
            the same collapse key. See :ref:`collapsing` for more information.
        :param collapse_order: an optional ordering :doc:`facet </facets>`
            to control which documents are kept when collapsing. The default
            (``collapse_order=None``) uses the results order (e.g. the highest
            scoring documents in a scored search).
        :rtype: :class:`Results`
        r   )r"  search_with_collectorr   )r$   r   r1   r   s       r   r   zSearcher.search  s4    h DNN$V$""1a(yy{r   c                 r    |xs | j                         }|j                  | ||       |j                          y)ab  Low-level method: runs a :class:`whoosh.query.Query` object on this
        searcher using the given :class:`whoosh.collectors.Collector` object
        to collect the results::

            myquery = query.Term("content", "cabbage")

            uc = collectors.UnlimitedCollector()
            tc = TermsCollector(uc)

            mysearcher.search_with_collector(myquery, tc)
            print(tc.docterms)
            print(tc.results())

        Note that this method does not return a :class:`Results` object. You
        need to access the collector to get a results object or other
        information the collector might hold after the search.

        :param q: a :class:`whoosh.query.Query` object to use to match
            documents.
        :param collector: a :class:`whoosh.collectors.Collector` object to feed
            the results into.
        N)r   preparerun)r$   r   r"  r   s       r   r$  zSearcher.search_with_collector  s/    2 +T\\^$7+r   c                    | j                         }|i }|i }i }	t        |      D ]  \  }
}|j                  |
|
      }
||	|
<    |	}| j                  j	                         }|D ];  }
|j                  |
|
      }
|
|vs| j                         j                  |
      ||
<   = ||g }|j                         D ]g  }|j                  |j                  |j                        }|j                  }||v s:||f|vsA|j                  |j                  |j                  f       i ddl
m} |j                  |||      }|j                  ||      S )a  
        Returns a corrected version of the given user query using a default
        :class:`whoosh.spelling.ReaderCorrector`.

        The default:

        * Corrects any words that don't appear in the index.

        * Takes suggestions from the words in the index. To make certain fields
          use custom correctors, use the ``correctors`` argument to pass a
          dictionary mapping field names to :class:`whoosh.spelling.Corrector`
          objects.

        Expert users who want more sophisticated correction behavior can create
        a custom :class:`whoosh.spelling.QueryCorrector` and use that instead
        of this method.

        Returns a :class:`whoosh.spelling.Correction` object with a ``query``
        attribute containing the corrected :class:`whoosh.query.Query` object
        and a ``string`` attributes containing the corrected query string.

        >>> from whoosh import qparser, highlight
        >>> qtext = 'mary "litle lamb"'
        >>> q = qparser.QueryParser("text", myindex.schema)
        >>> mysearcher = myindex.searcher()
        >>> correction = mysearcher().correct_query(q, qtext)
        >>> correction.query
        <query.And ...>
        >>> correction.string
        'mary "little lamb"'
        >>> mysearcher.close()

        You can use the ``Correction`` object's ``format_string`` method to
        format the corrected query string using a
        :class:`whoosh.highlight.Formatter` object. For example, you can format
        the corrected string as HTML, emphasizing the changed words.

        >>> hf = highlight.HtmlFormatter(classname="change")
        >>> correction.format_string(hf)
        'mary "<strong class="change term0">little</strong> lamb"'

        :param q: the :class:`whoosh.query.Query` object to correct.
        :param qstring: the original user query from which the query object was
            created. You can pass None instead of a string, in which the
            second item in the returned tuple will also be None.
        :param correctors: an optional dictionary mapping fieldnames to
            :class:`whoosh.spelling.Corrector` objects. By default, this method
            uses the contents of the index to spell check the terms in the
            query. You can use this argument to "override" some fields with a
            different correct, for example a
            :class:`whoosh.spelling.GraphCorrector`.
        :param terms: a sequence of ``("fieldname", "text")`` tuples to correct
            in the query. By default, this method corrects terms that don't
            appear in the index. You can use this argument to override that
            behavior and explicitly specify the terms that should be corrected.
        :param maxdist: the maximum number of "edits" (insertions, deletions,
            subsitutions, or transpositions of letters) allowed between the
            original word and any suggestion. Values higher than ``2`` may be
            slow.
        :param prefix: suggested replacement words must share this number of
            initial characters with the original word. Increasing this even to
            just ``1`` can dramatically speed up suggestions, and may be
            justifiable since spellling mistakes rarely involve the first
            letter of a word.
        :param aliases: an optional dictionary mapping field names in the query
            to different field names to use as the source of spelling
            suggestions. The mappings in ``correctors`` are applied after this.
        :rtype: :class:`whoosh.spelling.Correction`
        r   )spelling)rY   r	   getrN   namesrB   
all_tokensrw   r   r   r  r)  SimpleQueryCorrectorcorrect_query)r$   r   qstring
correctorsr   r   r   aliasesrY   drw   corr
fieldnamestokenanamer   r)  sqcs                     r   r.  zSearcher.correct_query5  s\   P  ?GJ (4 	 OItIy9IAiL	  
 [[&&(
# 	KIIy9I
*(,(?(?	(J
9%	K =E @EOOU__EzzJ&E4=+F LL%//5::!>?@ 	$++JwG  G,,r   r.   r   )r      r   r   F)r   NFNNr   NTNNFNT)NNr8  r   N)2r   r   r   r   r   BM25Fr%   r`   rd   rV   ri   rk   rT   rn   rp   rr   rH   ru   ry   r   r   rb   r   rY   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Bo1Modelr   r   r   r   r  r   r"  r   r$  r.  r   r   r   r5   r5   e   sK    *1D2>hE
%)'	)9=J82
D

'ADB$0;$&
<;J"L< 67 ))T!FF =>"*"3"3tF 15"q ))U4*LX46l(* KOBF?D'+=~8t> NO(,n-r   r5   c                   ^   e Zd ZdZ	 	 d*dZd Zd Zd Zd Zd Z	d	 Z
e
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,dZd Zd Zd Zd Z eee      Zd Z d Z! ee e!      Z"d Z#d Z$ ee#e$      Z%d  Z&d! Z' ee&e'      Z(d"d#e)jT                  d$fd%Z+d& Z,d' Z-d-d(Z.d) Z/y).r   a  This object is returned by a Searcher. This object represents the
    results of a search query. You can mostly use it as if it was a list of
    dictionaries, where each dictionary is the stored fields of the document at
    that position in the results.

    Note that a Results object keeps a reference to the Searcher that created
    it, so keeping a reference to a Results object keeps the Searcher alive and
    so keeps all files used by it open.
    Nc                     || _         || _        || _        || _        |xs i | _        || _        |xs t        j                         | _        d| _	        d| _
        i | _        y)a  
        :param searcher: the :class:`Searcher` object that produced these
            results.
        :param query: the original query that created these results.
        :param top_n: a list of (score, docnum) tuples representing the top
            N search results.
        N)searcherr   top_ndocset
_facetmapsruntimer   Highlighterhighlighterr"  _total_char_cache)r$   r?  r   r@  rA  	facetmapsrC  rE  s           r   r%   zResults.__init__  s^     !
#/r&A)*?*?*Ar   c                 h    dt        | j                        d| j                  d| j                  dS )Nz<Top z Results for z	 runtime=>)r   r@  r   rC  r+   s    r   r,   zResults.__repr__  s'    7:4::7;vv7;||E 	Er   c                 p    | j                   | j                  j                         | _         | j                   S )a  Returns the total number of documents that matched the query. Note
        this may be more than the number of scored documents, given the value
        of the ``limit`` keyword argument to :meth:`Searcher.search`.

        If this Results object was created by searching with a ``limit``
        keyword, then computing the exact length of the result set may be
        expensive for large indexes or large result sets. You may consider
        using :meth:`Results.has_exact_length`,
        :meth:`Results.estimated_length`, and
        :meth:`Results.estimated_min_length` to display an estimated size of
        the result set instead of an exact number.
        )rF  r"  countr+   s    r   __len__zResults.__len__  s,     ;;....0DK{{r   c           
         t        |t              ro|j                  t        | j                              \  }}}t        |||      D cg c]0  }t        | | j                  |   d   || j                  |   d         2 c}S |t        | j                        k\  r%t        d|dt        | j                        d      t        | | j                  |   d   || j                  |   d         S c c}w )Nr   r   zresults[z]: Results only has z hits)r   sliceindicesr   r@  r   Hit
IndexError)r$   nstartstopstepis         r   __getitem__zResults.__getitem__  s    a !		#djj/ :E4#E468 djjmA.4::a=3CD 8 8 C

O# $%s4::"8 9 9tTZZ]1-q$**Q-2BCC8s   5C+c              #      K   t        t        | j                              D ]2  }t        | | j                  |   d   || j                  |   d          4 yw)zFYields a :class:`Hit` object for each result in ranked order.
        r   r   N)r   r   r@  rQ  )r$   rW  s     r   __iter__zResults.__iter__  sQ      DJJ( 	CAdDJJqM!,aAq1ABB	Cs   AAc                 &    || j                         v S )zEReturns True if the given document number matched the query.
        )r   )r$   r   s     r   __contains__zResults.__contains__  s     $$r   c                 $    | j                          S r.   )is_emptyr+   s    r   __nonzero__zResults.__nonzero__  s    ==?""r   c                 (    | j                         dk(  S )z9Returns True if not documents matched the query.
        r   )scored_lengthr+   s    r   r^  zResults.is_empty  s     !!#q((r   c                 (    d | j                   D        S )zfReturns an iterator of (docnum, score) pairs for the scored
        documents in the results.
        c              3   *   K   | ]  \  }}||f  y wr.   r   )r   scorer   s      r   r   z Results.items.<locals>.<genexpr>  s     @ME6@s   r@  r+   s    r   r   zResults.items   s    
 ATZZ@@r   c                 X    | j                   j                  | j                  |   d         S )zReturns the stored fields for the document at the ``n`` th position
        in the results. Use :meth:`Results.docnum` if you want the raw
        document number instead of the stored fields.
        r   )r?  r7   r@  r$   rS  s     r   fieldszResults.fields  s&     }}**4::a=+;<<r   c                 6    | j                   j                         S )zYReturns the available facet names, for use with the ``groups()``
        method.
        )rB  keysr+   s    r   facet_nameszResults.facet_names  s    
 ##%%r   c                     ||dk(  r?t        | j                        dk(  r't        | j                  j                               d   }n,|| j                  vrt	        |d| j                               | j                  |   j                         S )an  If you generated facet groupings for the results using the
        `groupedby` keyword argument to the ``search()`` method, you can use
        this method to retrieve the groups. You can use the ``facet_names()``
        method to get the list of available facet names.

        >>> results = searcher.search(my_query, groupedby=["tag", "price"])
        >>> results.facet_names()
        ["tag", "price"]
        >>> results.groups("tag")
        {"new": [12, 1, 4], "apple": [3, 10, 5], "search": [11]}

        If you only used one facet, you can call the method without a facet
        name to get the groups for the facet.

        >>> results = searcher.search(my_query, groupedby="tag")
        >>> results.groups()
        {"new": [12, 1, 4], "apple": [3, 10, 5, 0], "search": [11]}

        By default, this returns a dictionary mapping category names to a list
        of document numbers, in the same relative order as they appear in the
        results.

        >>> results = mysearcher.search(myquery, groupedby="tag")
        >>> docnums = results.groups()
        >>> docnums['new']
        [12, 1, 4]

        You can then use :meth:`Searcher.stored_fields` to get the stored
        fields associated with a document ID.

        If you specified a different ``maptype`` for the facet when you
        searched, the values in the dictionary depend on the
        :class:`whoosh.sorting.FacetMap`.

        >>> myfacet = sorting.FieldFacet("tag", maptype=sorting.Count)
        >>> results = mysearcher.search(myquery, groupedby=myfacet)
        >>> counts = results.groups()
        {"new": 3, "apple": 4, "search": 1}
        facetr   r   z not in facet names )r   rB  r   rj  KeyErrorrk  as_dict)r$   r^   s     r   groupszResults.groups  s    R LDGOT__1E1J ,,./2D("D$4$4$68 9 9t$,,..r   c                 j    | j                   r| j                   j                         S | j                  duS )zjReturns True if this results object already knows the exact number
        of matching documents.
        N)r"  computes_countrF  r+   s    r   has_exact_lengthzResults.has_exact_lengthH  s-    
 >>>>0022;;d**r   c                     | j                         rt        |       S | j                  j                  | j                  j                               S )z}The estimated maximum number of matching documents, or the
        exact number of matching documents if it's known.
        )rs  r   r   estimate_sizer?  rY   r+   s    r   estimated_lengthzResults.estimated_lengthR  s<    
   "t966''(<(<(>??r   c                     | j                         rt        |       S | j                  j                  | j                  j                               S )z}The estimated minimum number of matching documents, or the
        exact number of matching documents if it's known.
        )rs  r   r   estimate_min_sizer?  rY   r+   s    r   estimated_min_lengthzResults.estimated_min_length\  s<    
   "t966++DMM,@,@,BCCr   c                 ,    t        | j                        S )a  Returns the number of scored documents in the results, equal to or
        less than the ``limit`` keyword argument to the search.

        >>> r = mysearcher.search(myquery, limit=20)
        >>> len(r)
        1246
        >>> r.scored_length()
        20

        This may be fewer than the total number of documents that match the
        query, which is what ``len(Results)`` returns.
        )r   r@  r+   s    r   ra  zResults.scored_lengthf  s     4::r   c                     | j                   (t        | j                  j                               | _         | j                   S )zbReturns a set-like object containing the document numbers that
        matched the query.
        )rA  r3   r"  all_idsr+   s    r   r   zResults.docsv  s1    
 ;;dnn4467DK{{r   c                     t        j                   |       }t        j                  | j                        |_        t        j                  | j                        |_        |S )z4Returns a deep copy of this results object.
        )r/   deepcopyrA  r@  )r$   r\   s     r   r/   zResults.copy  s>    
 IIdO==---

+r   c                 &    | j                   |   d   S )zReturns the score for the document at the Nth position in the list
        of ranked documents. If the search was not scored, this may return
        None.
        r   re  rg  s     r   rd  zResults.score  s     zz!}Qr   c                 &    | j                   |   d   S )ziReturns the document number of the result at position n in the list
        of ranked documents.
        r   re  rg  s     r   r   zResults.docnum  s     zz!}Qr   c                 n    | j                   j                  | j                  j                         ||      S )N)rw   expand)r   existing_termsr?  rY   )r$   r  rw   s      r   query_termszResults.query_terms  s4    vv$$T]]%9%9%;/8 % I 	Ir   c                 6    t        | d      xr t        | d      S )zReturns True if the search recorded which terms matched in which
        documents.

        >>> r = searcher.search(myquery)
        >>> r.has_matched_terms()
        False
        >>>
        doctermstermdocs)hasattrr+   s    r   has_matched_termszResults.has_matched_terms  s     tZ(FWT:-FFr   c                 t    | j                         st        t        | j                  j	                               S )aS  Returns the set of ``("fieldname", "text")`` tuples representing
        terms from the query that matched one or more of the TOP N documents
        (this does not report terms for documents that match the query but did
        not score high enough to make the top N results). You can compare this
        set to the terms from the original query to find terms which didn't
        occur in any matching documents.

        This is only valid if you used ``terms=True`` in the search call to
        record matching terms. Otherwise it will raise an exception.

        >>> q = myparser.parse("alfa OR bravo OR charlie")
        >>> results = searcher.search(q, terms=True)
        >>> results.terms()
        set([("content", "alfa"), ("content", "charlie")])
        >>> q.all_terms() - results.terms()
        set([("content", "bravo")])
        )r  r   r3   r  rj  r+   s    r   matched_termszResults.matched_terms  s.    & %%'""4==%%'((r   c                 .    | j                   j                  S r.   rE  
fragmenterr+   s    r   _get_fragmenterzResults._get_fragmenter  s    ***r   c                 &    || j                   _        y r.   r  r$   fs     r   _set_fragmenterzResults._set_fragmenter  s    &'#r   c                 .    | j                   j                  S r.   rE  	formatterr+   s    r   _get_formatterzResults._get_formatter  s    )))r   c                 &    || j                   _        y r.   r  r  s     r   _set_formatterzResults._set_formatter  s    %&"r   c                 .    | j                   j                  S r.   rE  r   r+   s    r   _get_scorerzResults._get_scorer  s    &&&r   c                 &    || j                   _        y r.   r  )r$   r
  s     r   _set_scorerzResults._set_scorer  s    "#r   c                 .    | j                   j                  S r.   rE  r  r+   s    r   
_get_orderzResults._get_order  s    %%%r   c                 &    || j                   _        y r.   r  )r$   os     r   
_set_orderzResults._set_order  s    !"r   r   r   Tc                     t        |       sg S t        |t        |             }| j                  j                         }t	        j
                  |||      }| j                  d| D ]  \  }}	|j                  |	        |j                  ||      S )a  Returns the 'numterms' most important terms from the top 'docs'
        documents in these results. "Most important" is generally defined as
        terms that occur frequently in the top hits but relatively infrequently
        in the collection as a whole.

        :param fieldname: Look at the terms in this field. This field must
            store vectors.
        :param docs: Look at this many of the top documents of the results.
        :param numterms: Return this number of important terms.
        :param model: The classify.ExpansionModel to use. See the classify
            module.
        :returns: list of unicode strings.
        r   Nr   )	r   minr?  rY   r   r   r@  r   r   )
r$   rw   r   r   r   r   rY   r   _r   s
             r   r   zResults.key_terms  s      4yI4T#%%'$$VYeDET* 	*IAv!!&)	* &&x9&EEr   c                     | j                         }|j                  D ]%  }|d   |vs| j                  j                  |       ' ||j                         z  | _        t	        | j                        | _        y)zAppends hits from 'results' (that are not already in this
        results object) to the end of these results.

        :param results: another results object.
        r   N)r   r@  r   rA  r   rF  )r$   r   r   items       r   extendzResults.extend  sd     yy{MM 	(DAwd"

!!$'	( W\\^+$++&r   c                     t        |      sy|j                         }| j                  D cg c]  }|d   |v s| }}| j                         |z  | _        || _        yc c}w )zHRemoves any hits that are not also in the other results object.
        Nr   r   r   r@  rA  )r$   r   	otherdocsr  r   s        r   r   zResults.filter	  sZ     7|LLN	"&**E$Q90DEEiikI-
 Fs
   AAc                 
   t        |      sy|j                         }| j                  D cg c]  }|d   |v s| }}| j                  D cg c]  }|d   |vs| }}|r||z   }|| _        y||z   }|| _        yc c}w c c}w )a  Re-sorts the results so any hits that are also in 'results' appear
        before hits not in 'results', otherwise keeping their current relative
        positions. This does not add the documents in the other results object
        to this one.

        :param results: another results object.
        :param reverse: if True, lower the position of hits in the other
            results object instead of raising them.
        Nr   )r   r   r@  )r$   r   r  r  r  areinnotinr   s           r   upgradezResults.upgrade  s     7|LLN	"&**E$Q90DEE"&**I$Qy0HIIEME 
 EME
 FIs   A;A;B B c                 n   t        |      sy| j                         }|j                         }| j                  D cg c]  }|d   |v s| }}| j                  D cg c]  }|d   |vs| }}|j                  D cg c]  }|d   |vs| }}||z  | _        ||z   |z   | _        yc c}w c c}w c c}w )a  Combines the effects of extend() and upgrade(): hits that are also
        in 'results' are raised. Then any hits from the other results object
        that are not in this results object are appended to the end.

        :param results: another results object.
        Nr   r  )r$   r   r   r  r  r  r  others           r   upgrade_and_extendzResults.upgrade_and_extend.  s     7|yy{LLN	"&**E$Q90DEE"&**I$Qy0HII")--G$47$3FGGY&U]U*
 FIGs#   B(B(B-)B-=B2
B2)NNr   Nr.   )FNr:  )0r   r   r   r   r%   r,   rM  rX  rZ  r\  r_  __bool__r^  r   rh  rk  rp  rs  rv  ry  ra  r   r/   rd  r   r  r  r  r  r  propertyr  r  r  r  r  r  r   r  r  r  r   r<  r   r  r   r  r  r   r   r   r   r     s    CG(,*E
$	DC%# H)A=&0/d+@D 	  I
G).+( /?;J*' 8I'$ k;/F&# Z,E(*Q ))TF8'
2+r   r   c                       e Zd ZdZddZd Zd ZddZdddej                  d	df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dZy)rQ  a  Represents a single search result ("hit") in a Results object.

    This object acts like a dictionary of the matching document's stored
    fields. If for some reason you need an actual ``dict`` object, use
    ``Hit.fields()`` to get one.

    >>> r = searcher.search(query.Term("content", "render"))
    >>> r[0]
    < Hit {title = u"Rendering the scene"} >
    >>> r[0].rank
    0
    >>> r[0].docnum == 4592
    True
    >>> r[0].score
    2.52045682
    >>> r[0]["title"]
    "Rendering the scene"
    >>> r[0].keys()
    ["title"]
    Nc                     || _         |j                  | _        | j                  j                         | _        |x| _        | _        || _        || _        d| _        y)a?  
        :param results: the Results object this hit belongs to.
        :param pos: the position in the results list of this hit, for example
            pos = 0 means this is the first (highest scoring) hit.
        :param docnum: the document number of this hit.
        :param score: the score of this hit.
        N)r   r?  rY   posrankr   rd  _fields)r$   r   r   r  rd  s        r   r%   zHit.__init__Z  sO     ((mm**,""49
r   c                     | j                   *| j                  j                  | j                        | _         | j                   S )zbReturns a dictionary of the stored fields of the document this
        object represents.
        )r  r?  r7   r   r+   s    r   rh  z
Hit.fieldsk  s2    
 <<==66t{{CDL||r   c                     | j                   j                         st        | j                   j                  j	                  | j
                  g       S )a  Returns the set of ``("fieldname", "text")`` tuples representing
        terms from the query that matched in this document. You can
        compare this set to the terms from the original query to find terms
        which didn't occur in this document.

        This is only valid if you used ``terms=True`` in the search call to
        record matching terms. Otherwise it will raise an exception.

        >>> q = myparser.parse("alfa OR bravo OR charlie")
        >>> results = searcher.search(q, terms=True)
        >>> for hit in results:
        ...   print(hit["title"])
        ...   print("Contains:", hit.matched_terms())
        ...   print("Doesn't contain:", q.all_terms() - hit.matched_terms())
        )r   r  r   r  r*  r   r+   s    r   r  zHit.matched_termst  s;    " ||--/""||$$((b99r   c                 Z    | j                   j                  }|j                  | ||||      S )a6  Returns highlighted snippets from the given field::

            r = searcher.search(myquery)
            for hit in r:
                print(hit["title"])
                print(hit.highlights("content"))

        See :doc:`/highlight`.

        To change the fragmeter, formatter, order, or scorer used in
        highlighting, you can set attributes on the results object::

            from whoosh import highlight

            results = searcher.search(myquery, terms=True)
            results.fragmenter = highlight.SentenceFragmenter()

        ...or use a custom :class:`whoosh.highlight.Highlighter` object::

            hl = highlight.Highlighter(fragmenter=sf)
            results.highlighter = hl

        :param fieldname: the name of the field you want to highlight.
        :param text: by default, the method will attempt to load the contents
            of the field from the stored fields for the document. If the field
            you want to highlight isn't stored in the index, but you have
            access to the text another way (for example, loading from a file or
            a database), you can supply it using the ``text`` parameter.
        :param top: the maximum number of fragments to return.
        :param minscore: the minimum score for fragments to appear in the
            highlights.
        )r   r   minscore)r   rE  highlight_hit)r$   rw   r   r   r  hliters         r   
highlightszHit.highlights  s7    D ))##D)$C-5 $ 7 	7r   r   r   Tc           
      \    | j                   j                  | j                  |||||||      S )a  Returns a new Results object containing documents similar to this
        hit, based on "key terms" in the given field::

            r = searcher.search(myquery)
            for hit in r:
                print(hit["title"])
                print("Top 3 similar documents:")
                for subhit in hit.more_like_this("content", top=3):
                  print("  ", subhit["title"])

        :param fieldname: the name of the field to use to test similarity.
        :param text: by default, the method will attempt to load the contents
            of the field from the stored fields for the document, or from a
            term vector. If the field isn't stored or vectored in the index,
            but you have access to the text another way (for example, loading
            from a file or a database), you can supply it using the ``text``
            parameter.
        :param top: the number of results to return.
        :param numterms: the number of "key terms" to extract from the hit and
            search for. Using more terms is slower but gives potentially more
            and more accurate results.
        :param model: (expert) a :class:`whoosh.classify.ExpansionModel` to use
            to compute "key terms".
        :param normalize: whether to normalize term weights.
        )r   r   r   r   r   r   )r?  r   r   )r$   rw   r   r   r   r   r   r   s           r   more_like_thiszHit.more_like_this  s8    8 }}&&t{{ID+.1:6 ' K 	Kr   c                 X    d| j                   j                  d| j                         dS )N< rJ  )r)   r   rh  r+   s    r   r,   zHit.__repr__  s     NN33T[[]CCr   c                     t        |t              r!| j                         |j                         k(  S t        |t              r| j                         |k(  S y)NF)r   rQ  rh  dict)r$   r  s     r   __eq__z
Hit.__eq__  sA    eS!;;=ELLN22t$;;=E))r   c                 4    t        | j                               S r.   )r   rh  r+   s    r   rM  zHit.__len__  s    4;;=!!r   c                 4    t        | j                               S r.   r   rh  r+   s    r   rZ  zHit.__iter__      &&r   c                     || j                         v r| j                  |   S | j                  }|j                  |      r |j	                  |      }|| j
                     S t        |      r.   )rh  r  rY   
has_columncolumn_readerr   rn  )r$   rw   rY   crs       r   rX  zHit.__getitem__  s_    %<<	**Y'%%i0Bdkk?"y!!r   c                 `    || j                         v xs | j                  j                  |      S r.   )rh  rY   r  )r$   r   s     r   r\  zHit.__contains__  s,    t{{}$ /;;))#.	0r   c                 P    t        | j                         j                               S r.   )r   rh  r   r+   s    r   r   z	Hit.items  s    DKKM'')**r   c                 P    t        | j                         j                               S r.   )r   rh  rj  r+   s    r   rj  zHit.keys  s    DKKM&&())r   c                 P    t        | j                         j                               S r.   )r   rh  valuesr+   s    r   r  z
Hit.values  s    DKKM((*++r   c                 4    t        | j                               S r.   )r	   rh  r+   s    r   r	   zHit.iteritems  s    ''r   c                 4    t        | j                               S r.   r  r+   s    r   r   zHit.iterkeys  r  r   c                 4    t        | j                               S r.   )r
   rh  r+   s    r   r
   zHit.itervalues  s    $++-((r   c                 B    | j                         j                  ||      S r.   )rh  r*  )r$   r   r   s      r   r*  zHit.get  s    {{}  g..r   c                     t        d      Nz!You cannot modify a search resultNotImplementedErrorr$   r   r   s      r   __setitem__zHit.__setitem__      !"EFFr   c                     t        d      r  r  r  s      r   __delitem__zHit.__delitem__  r  r   c                     t        d      r  r  r+   s    r   clearz	Hit.clear
  r  r   c                     t        d      r  r  )r$   r  r1   s      r   r0   z
Hit.update  r  r   )NN)N   r   r.   )r   r   r   r   r%   rh  r  r  r   r<  r  r,   r  rM  rZ  rX  r\  r   rj  r  r	   r   r
   r*  r  r  r  r0   r   r   r   rQ  rQ  D  s    *":*$7L .2rA%..$tK@D"'	"0+*,(')/GGGGr   rQ  c                   B    e Zd ZdZddZd Zd Zd Zd Zd Z	d Z
d	 Zy
)r   a  Represents a single page out of a longer list of results, as returned
    by :func:`whoosh.searching.Searcher.search_page`. Supports a subset of the
    interface of the :class:`~whoosh.searching.Results` object, namely getting
    stored fields with __getitem__ (square brackets), iterating, and the
    ``score()`` and ``docnum()`` methods.

    The ``offset`` attribute contains the results number this page starts at
    (numbered from 0). For example, if the page length is 10, the ``offset``
    attribute on the second page will be ``10``.

    The ``pagecount`` attribute contains the number of pages available.

    The ``pagenum`` attribute contains the page number. This may be less than
    the page you requested if the results had too few pages. For example, if
    you do::

        ResultsPage(results, 5)

    but the results object only contains 3 pages worth of hits, ``pagenum``
    will be 3.

    The ``pagelen`` attribute contains the number of results on this page
    (which may be less than the page length you requested if this is the last
    page of the results).

    The ``total`` attribute contains the total number of hits in the results.

    >>> mysearcher = myindex.searcher()
    >>> pagenum = 2
    >>> page = mysearcher.find_page(pagenum, myquery)
    >>> print("Page %s of %s, results %s to %s of %s" %
    ...       (pagenum, page.pagecount, page.offset+1,
    ...        page.offset+page.pagelen, page.total))
    >>> for i, fields in enumerate(page):
    ...   print("%s. %r" % (page.offset + i + 1, fields))
    >>> mysearcher.close()

    To set highlighter attributes (for example ``formatter``), access the
    underlying :class:`Results` object::

        page.results.formatter = highlight.UppercaseFormatter()

    c                 V   || _         t        |      | _        |dk  rt        d      t	        t        | j                  |z              | _        t        | j                  |      | _        | j                  dz
  |z  }||z   | j                  kD  r| j                  |z
  }|| _	        || _
        y)z
        :param results: a :class:`~whoosh.searching.Results` object.
        :param pagenum: which page of the results to use, numbered from ``1``.
        :param pagelen: the number of hits per page.
        r   r   N)r   r   totalr   intr   	pagecountr  r   r]   r   )r$   r   r   r   r]   s        r   r%   zResultsPage.__init__>  s     \
Q;344T$**w"6784>>73,,"g-W

*jj6)Gr   c                    | j                   }t        |t              rK|j                  | j                        \  }}}| j
                  j                  t        ||z   ||z   |            S | j
                  j                  ||z         S r.   )r]   r   rO  rP  r   r   rX  )r$   rS  r]   rT  rU  rV  s         r   rX  zResultsPage.__getitem__T  sy    a !		$,, 7E4<<++E%&.26--G H H <<++AJ77r   c                 t    t        | j                  | j                  | j                  | j                  z          S r.   )iterr   r]   r   r+   s    r   rZ  zResultsPage.__iter__]  s)    DLLT[[4<<-GHIIr   c                     | j                   S r.   )r  r+   s    r   rM  zResultsPage.__len__`  s    zzr   c                 6    | j                   j                         S r.   )r   ra  r+   s    r   ra  zResultsPage.scored_lengthc  s    ||))++r   c                 R    | j                   j                  || j                  z         S )zGReturns the score of the hit at the nth position on this page.
        )r   rd  r]   rg  s     r   rd  zResultsPage.scoref  s!     ||!!!dkk/22r   c                 R    | j                   j                  || j                  z         S )zYReturns the document number of the hit at the nth position on this
        page.
        )r   r   r]   rg  s     r   r   zResultsPage.docnumk  s!     ||""1t{{?33r   c                 V    | j                   dk(  xs | j                  | j                   k(  S )zIReturns True if this object represents the last page of results.
        r   )r  r   r+   s    r   is_last_pagezResultsPage.is_last_pageq  s%     ~~"Ddlldnn&DDr   Nr9  )r   r   r   r   r%   rX  rZ  rM  ra  rd  r   r  r   r   r   r   r     s2    *X,8J,3
4Er   r   )!r   
__future__r   r/   rK   mathr   r  r   r   r   r   whoosh.compatr	   r
   r   r   whoosh.idsetsr   r   whoosh.readingr   whoosh.util.cacher   r|   r   r   objectr   r5   r   rQ  r   r   r   r   <module>r     s   8      6 6 A A * ' '=y =		 	F H~-v ~-B[+f [+|JG& JGZdE& dEr   