# Copyright 2007 Matt Chaput. All rights reserved.## Redistribution and use in source and binary forms, with or without# modification, are permitted provided that the following conditions are met:## 1. Redistributions of source code must retain the above copyright notice,# this list of conditions and the following disclaimer.## 2. Redistributions in binary form must reproduce the above copyright# notice, this list of conditions and the following disclaimer in the# documentation and/or other materials provided with the distribution.## THIS SOFTWARE IS PROVIDED BY MATT CHAPUT ``AS IS'' AND ANY EXPRESS OR# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO# EVENT SHALL MATT CHAPUT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.## The views and conclusions contained in the software and documentation are# those of the authors and should not be interpreted as representing official# policies, either expressed or implied, of Matt Chaput."""This module contains classes and functions related to searching the index."""from__future__importdivisionimportcopy,logging,threading,weakreffromcollectionsimportdefaultdictfromheapqimportheappush,heapreplace,nlargest,nsmallestfrommathimportceilfromwhooshimportclassify,highlight,query,scoring,sortingfromwhoosh.compatimportiteritems,itervalues,iterkeys,xrangefromwhoosh.readingimportTermNotFoundfromwhoosh.support.bitvectorimportDocIdSet,BitSetfromwhoosh.utilimportnow,lru_cachelog=logging.getLogger(__name__)classTimeLimit(Exception):passclassNoTermsException(Exception):"""Exception 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``. """message="Results were created without recording terms"# Searcher classclassSearcher(object):"""Wraps an :class:`~whoosh.reading.IndexReader` object and provides methods for searching the index. """def__init__(self,reader,weighting=scoring.BM25F,closereader=True,fromindex=None,parent=None):""" :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. """self.ixreader=readerself.is_closed=Falseself._closereader=closereaderself._ix=fromindexself._doccount=self.ixreader.doc_count_all()ifparent:self.parent=weakref.ref(parent)self.schema=parent.schemaself._idf_cache=parent._idf_cacheself._filter_cache=parent._filter_cacheelse:self.parent=Noneself.schema=self.ixreader.schemaself._idf_cache={}self._filter_cache={}iftype(weighting)istype:self.weighting=weighting()else:self.weighting=weightingself.leafreaders=Noneself.subsearchers=Noneifnotself.ixreader.is_atomic():self.leafreaders=self.ixreader.leaf_readers()self.subsearchers=[(self._subsearcher(r),offset)forr,offsetinself.leafreaders]# Copy attributes/methods from wrapped readerfornamein("stored_fields","all_stored_fields","has_vector","vector","vector_as","lexicon","frequency","doc_frequency","term_info","doc_field_length","corrector"):setattr(self,name,getattr(self.ixreader,name))def__enter__(self):returnselfdef__exit__(self,*exc_info):self.close()def_subsearcher(self,reader):returnself.__class__(reader,fromindex=self._ix,weighting=self.weighting,parent=self)def_offset_for_subsearcher(self,subsearcher):forss,offsetinself.subsearchers:ifssissubsearcher:returnoffsetdefis_atomic(self):returnself.reader().is_atomic()defhas_parent(self):returnself.parentisnotNonedefget_parent(self):"""Returns the parent of this searcher (if has_parent() is True), or else self. """ifself.has_parent():returnself.parent()else:returnselfdefdoc_count(self):"""Returns the number of UNDELETED documents in the index. """returnself.ixreader.doc_count()defdoc_count_all(self):"""Returns the total number of documents, DELETED OR UNDELETED, in the index. """returnself._doccountdeffield_length(self,fieldname):ifself.parent:returnself.parent().field_length(fieldname)else:returnself.reader().field_length(fieldname)defmax_field_length(self,fieldname):ifself.parent:returnself.parent().max_field_length(fieldname)else:returnself.reader().max_field_length(fieldname)defup_to_date(self):"""Returns True if this Searcher represents the latest version of the index, for backends that support versioning. """ifnotself._ix:raiseException("No reference to index")returnself._ix.latest_generation()==self.ixreader.generation()defrefresh(self):"""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. """ifnotself._ix:raiseException("No reference to index")ifself._ix.latest_generation()==self.reader().generation():returnself# Get a new reader, re-using resources from the current reader if# possibleself.is_closed=Truenewreader=self._ix.reader(reuse=self.ixreader)returnself.__class__(newreader,fromindex=self._ix,weighting=self.weighting)defclose(self):ifself._closereader:self.ixreader.close()self.is_closed=Truedefavg_field_length(self,fieldname,default=None):ifnotself.schema[fieldname].scorable:returndefaultreturnself.field_length(fieldname)/(self._doccountor1)defreader(self):"""Returns the underlying :class:`~whoosh.reading.IndexReader`. """returnself.ixreaderdefset_caching_policy(self,*args,**kwargs):self.ixreader.set_caching_policy(*args,**kwargs)defpostings(self,fieldname,text,weighting=None,qf=1):"""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. """weighting=weightingorself.weightingscorer=weighting.scorer(self,fieldname,text,qf=qf)returnself.ixreader.postings(fieldname,text,scorer=scorer)defidf(self,fieldname,text):"""Calculates the Inverse Document Frequency of the current term (calls idf() on the searcher's Weighting object). """# This method just calls the Weighting object's idf() method, but# caches the result. So Weighting objects should call *this* method# which will then call *their own* idf() methods.cache=self._idf_cacheterm=(fieldname,text)iftermincache:returncache[term]idf=self.weighting.idf(self,fieldname,text)cache[term]=idfreturnidfdefdocument(self,**kw):"""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") """forpinself.documents(**kw):returnpdefdocuments(self,**kw):"""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']) """ixreader=self.ixreaderreturn(ixreader.stored_fields(docnum)fordocnuminself.document_numbers(**kw))def_kw_to_text(self,kw):fork,viniteritems(kw):field=self.schema[k]kw[k]=field.to_text(v)def_query_for_kw(self,kw):subqueries=[]forkey,valueiniteritems(kw):subqueries.append(query.Term(key,value))ifsubqueries:q=query.And(subqueries).normalize()else:q=query.Every()returnqdefdocument_number(self,**kw):"""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 """# In the common case where only one keyword was given, just use# first_id() instead of building a query.self._kw_to_text(kw)iflen(kw)==1:k,v=list(kw.items())[0]try:returnself.reader().first_id(k,v)exceptTermNotFound:returnNoneelse:m=self._query_for_kw(kw).matcher(self)ifm.is_active():returnm.id()defdocument_numbers(self,**kw):"""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")) """self._kw_to_text(kw)returnself.docs_for_query(self._query_for_kw(kw))def_find_unique(self,uniques):# uniques is a list of ("unique_field_name", "field_value") tuplesdelset=set()forname,valueinuniques:docnum=self.document_number(**{name:value})ifdocnumisnotNone:delset.add(docnum)returndelset@lru_cache(20)def_query_to_comb(self,fq):returnBitSet(self.docs_for_query(fq),size=self.doc_count_all())def_filter_to_comb(self,obj):ifobjisNone:returnNoneifisinstance(obj,(set,DocIdSet)):c=objelifisinstance(obj,Results):c=obj.docsetelifisinstance(obj,ResultsPage):c=obj.results.docsetelifisinstance(obj,query.Query):c=self._query_to_comb(obj)else:raiseException("Don't know what to do with filter object %r"%obj)returncdefsuggest(self,fieldname,text,limit=5,maxdist=2,prefix=0):"""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. """c=self.reader().corrector(fieldname)returnc.suggest(text,limit=limit,maxdist=maxdist,prefix=prefix)defkey_terms(self,docnums,fieldname,numterms=5,model=classify.Bo1Model,normalize=True):"""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. """expander=classify.Expander(self.ixreader,fieldname,model=model)fordocnumindocnums:expander.add_document(docnum)returnexpander.expanded_terms(numterms,normalize=normalize)defkey_terms_from_text(self,fieldname,text,numterms=5,model=classify.Bo1Model,normalize=True):"""Return 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. """expander=classify.Expander(self.ixreader,fieldname,model=model)expander.add_text(text)returnexpander.expanded_terms(numterms,normalize=normalize)defmore_like(self,docnum,fieldname,text=None,top=10,numterms=5,model=classify.Bo1Model,normalize=False,filter=None):"""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. """iftext:kts=self.key_terms_from_text(fieldname,text,numterms=numterms,model=model,normalize=normalize)else:kts=self.key_terms([docnum],fieldname,numterms=numterms,model=model,normalize=normalize)# Create an Or query from the key termsq=query.Or([query.Term(fieldname,word,boost=weight)forword,weightinkts])returnself.search(q,limit=top,filter=filter,mask=set([docnum]))defsearch_page(self,query,pagenum,pagelen=10,**kwargs):"""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` """ifpagenum<1:raiseValueError("pagenum must be >= 1")results=self.search(query,limit=pagenum*pagelen,**kwargs)returnResultsPage(results,pagenum,pagelen)deffind(self,defaultfield,querystring,**kwargs):fromwhoosh.qparserimportQueryParserqp=QueryParser(defaultfield,schema=self.ixreader.schema)q=qp.parse(querystring)returnself.search(q,**kwargs)defsorter(self,*args,**kwargs):"""This method is deprecated. Instead of using a Sorter, configure a :class:`whoosh.sorting.FieldFacet` or :class:`whoosh.sorting.MultiFacet` and pass it to the :meth:`Searcher.search` method's ``sortedby`` keyword argument. See :doc:`/facets`. """returnsorting.Sorter(self,*args,**kwargs)defadd_facet_field(self,name,facet,save=False):"""This is an experimental feature which may change in future versions. Adds a field cache for a computed field defined by a :class:`whoosh.sorting.FacetType` object, for example a :class:`~whoosh.sorting.QueryFacet` or :class:`~whoosh.sorting.RangeFacet`. This creates a field cache from the facet, so once you define the "facet field", sorting/grouping by it will be faster than using the original facet object. For example, sorting using a :class:`~whoosh.sorting.QueryFacet` recomputes the queries at sort time, which may be slow:: qfacet = sorting.QueryFacet({"a-z": TermRange(... results = searcher.search(myquery, sortedby=qfacet) You can cache the results of the query facet in a field cache:: searcher.define_facets("nameranges", qfacet, save=True) ..then use the pseudo-field for sorting:: results = searcher.search(myquery, sortedby="nameranges") See :doc:`/facets`. :param name: a name for the pseudo-field to cache the query results in. :param qs: a :class:`~whoosh.sorting.FacetType` object. :param save: if True, saves the field cache to disk so it is persistent across searchers. The default is False, which only creates the field cache in memory. """ifself.subsearchers:ss=self.subsearcherselse:ss=[(self,0)]fors,offsetinss:doclists=defaultdict(list)catter=facet.categorizer(self)catter.set_searcher(s,offset)fordocnuminxrange(s.doc_count_all()):key=catter.key_for_id(docnum)doclists[key].append(docnum)s.reader().define_facets(name,doclists,save=save)defdocs_for_query(self,q,for_deletion=False):"""Returns an iterator of document numbers for documents matching the given :class:`whoosh.query.Query` object. """# If we're getting the document numbers so we can delete them, use the# deletion_docs method instead of docs; this lets special queries# (e.g. nested queries) override what gets deletediffor_deletion:method=q.deletion_docselse:method=q.docsifself.subsearchers:fors,offsetinself.subsearchers:fordocnuminmethod(s):yielddocnum+offsetelse:fordocnuminmethod(self):yielddocnumdefsearch(self,q,limit=10,sortedby=None,reverse=False,groupedby=None,optimize=True,filter=None,mask=None,terms=False,maptype=None):"""Runs the query represented by the ``query`` object and returns a Results object. See :doc:`/facets` for information on using ``sortedby`` and/or ``groupedby``. :param query: a :class:`whoosh.query.Query` object. :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. :param sortedby: see :doc:`/facets`. :param reverse: Reverses the direction of the sort. :param groupedby: see :doc:`/facets`. :param optimize: use optimizations to get faster results when possible. :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 documents that are also in the mask object. :param terms: if True, record which terms were found in each matching document. You can use :meth:`Results.contains_term` or :meth:`Hit.contains_term` to check whether a hit contains a particular term. :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. :rtype: :class:`Results` """iflimitisnotNoneandlimit<1:raiseValueError("limit must be >= 1")collector=Collector(limit=limit,usequality=optimize,groupedby=groupedby,terms=terms,maptype=maptype)ifsortedby:returncollector.sort(self,q,sortedby,reverse=reverse,allow=filter,restrict=mask)else:returncollector.search(self,q,allow=filter,restrict=mask)defcorrect_query(self,q,qstring,correctors=None,allfields=False,terms=None,prefix=0,maxdist=2):"""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. * ONLY CORRECTS FIELDS THAT HAVE THE ``spelling`` ATTRIBUTE in the schema (or for which you pass a custom corrector). To automatically check all fields, use ``allfields=True``. Spell checking fields without ``spelling`` is slower. 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"' 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 allfields: if True, automatically spell check all fields, not just fields with the ``spelling`` attribute. :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 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 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. :rtype: :class:`whoosh.spelling.Correction` """ifcorrectorsisNone:correctors={}ifallfields:fieldnames=self.schema.names()else:fieldnames=[nameforname,fieldinself.schema.items()iffield.spelling]forfieldnameinfieldnames:iffieldnamenotincorrectors:correctors[fieldname]=self.corrector(fieldname)iftermsisNone:terms=[]fortokeninq.all_tokens():iftoken.fieldnameincorrectors:terms.append((token.fieldname,token.text))fromwhooshimportspellingsqc=spelling.SimpleQueryCorrector(correctors,terms)returnsqc.correct_query(q,qstring)classCollector(object):"""A Collector finds the matching documents, scores them, collects them into a list, and produces a Results object from them. Normally you do not need to instantiate an instance of the base Collector class, the :meth:`Searcher.search` method does that for you. If you create a custom Collector instance or subclass you can use its ``search()`` method instead of :meth:`Searcher.search`:: mycollector = MyCollector() results = mycollector.search(mysearcher, myquery) **Do not** re-use or share Collector instances between searches. You should create a new Collector instance for each search. To limit the amount of time a search can take, pass the number of seconds to the ``timelimit`` keyword argument:: # Limit the search to 4.5 seconds col = Collector(timelimit=4.5, greedy=False) # If this call takes more than 4.5 seconds, it will raise a # whoosh.searching.TimeLimit exception try: r = searcher.search(myquery, collector=col) except TimeLimit, tl: # You can still retrieve partial results from the collector r = col.results() If the ``greedy`` keyword is ``True``, the collector will finish adding the most recent hit before raising the ``TimeLimit`` exception. """def__init__(self,limit=10,usequality=True,groupedby=None,timelimit=None,greedy=False,terms=False,replace=10,maptype=None):""" :param limit: the maximum number of hits to collect. If this is None, collect all hits. :param usequality: whether to use block quality optimizations when available. This is mostly useful for debugging purposes. :param groupedby: see :doc:`/facets` for information. :param timelimit: the maximum amount of time (in possibly fractional seconds) to allow for searching. If the search takes longer than this, it will raise a ``TimeLimit`` exception. :param greedy: if ``True``, the collector will finish adding the most recent hit before raising the ``TimeLimit`` exception. :param terms: if ``True``, record which terms matched in each document. :param maptype: a :class:`whoosh.sorting.FacetMap` type to use for all facets that don't specify their own. """self.limit=limitself.usequality=usequalityself.replace=replaceself.timelimit=timelimitself.greedy=greedyself.maptype=maptypeself.termlists=defaultdict(set)iftermselseNoneself.facets=Noneifgroupedby:self.facets=sorting.Facets.from_groupedby(groupedby)self.replaced_times=0self.skipped_times=0defshould_add_all(self):"""Returns True if this collector needs to add all found documents (for example, if ``limit=None``), or False if this collector should only add the top N found documents. """limit=self.limitiflimit:limit=min(limit,self.searcher.doc_count_all())returnnotlimitdefuse_block_quality(self,searcher,matcher=None):"""Returns True if this collector can use block quality optimizations (usequality is True, the matcher supports block quality, the weighting does not use the final() method, etc.). """use=(self.usequalityandnotsearcher.weighting.use_finalandnotself.should_add_all())ifmatcher:use=useandmatcher.supports_block_quality()returnusedef_score_fn(self,searcher):w=searcher.weightingifw.use_final:defscorefn(matcher):score=matcher.score()returnw.final(searcher,matcher.id(),score)else:scorefn=Nonereturnscorefndef_set_categorizers(self,searcher,offset):ifself.facets:self.categorizers={}forname,facetinself.facets.items():catter=facet.categorizer(searcher)catter.set_searcher(searcher,offset)self.categorizers[name]=catterdef_set_filters(self,allow,restrict):ifallow:allow=self.searcher._filter_to_comb(allow)self.allow=allowifrestrict:restrict=self.searcher._filter_to_comb(restrict)self.restrict=restrictdef_set_timer(self):# If this collector is time limited, start the timer threadself.timer=Noneifself.timelimit:self.timer=threading.Timer(self.timelimit,self._timestop)self.timer.start()def_reset(self):self.facetmaps={}self.items=[]self.timedout=Falseself.runtime=-1self.minscore=Noneifself.facets:self.facetmaps=dict((facetname,facet.map(self.maptype))forfacetname,facetinself.facets.items())else:self.facetmaps={}def_timestop(self):# Called by the Timer when the time limit expires. Set an attribute on# the collector to indicate that the timer has expired and the# collector should raise a TimeLimit exception at the next consistent# state.self.timer=Noneself.timedout=Truedefcollect(self,docid,offsetid,sortkey):docset=self.docsetifdocsetisnotNone:docset.add(offsetid)ifself.facetsisnotNone:forname,catterinself.categorizers.items():add=self.facetmaps[name].addifcatter.allow_overlap:forkeyincatter.keys_for_id(docid):add(catter.key_to_name(key),offsetid,sortkey)else:key=catter.key_to_name(catter.key_for_id(docid))add(key,offsetid,sortkey)defsearch(self,searcher,q,allow=None,restrict=None):"""Top-level method call which uses the given :class:`Searcher` and :class:`whoosh.query.Query` objects to return a :class:`Results` object. >>> # This is the equivalent of calling searcher.search(q) >>> col = Collector() >>> results = col.search(searcher, q) This method takes care of calling :meth:`Collector.add_searcher` for each sub-searcher in a collective searcher. You should only call this method on a top-level searcher. """self.searcher=searcherself.q=qself._set_filters(allow,restrict)self._reset()self._set_timer()# If we're not using block quality, then we can add every document# number to a set as we see it, because we're not skipping low-quality# blocksself.docset=set()ifnotself.use_block_quality(searcher)elseNone# Perform the searcht=now()ifsearcher.is_atomic():searchers=[(searcher,0)]else:searchers=searcher.subsearchersfors,offsetinsearchers:scorefn=self._score_fn(s)self.subsearcher=sself._set_categorizers(s,offset)self.add_matches(q,offset,scorefn)# If we started a time limit timer thread, cancel itifself.timelimitandself.timer:self.timer.cancel()self.runtime=now()-treturnself.results()defadd_matches(self,q,offset,scorefn):items=self.itemslimit=self.limitaddall=self.should_add_all()forscore,offsetidinself.pull_matches(q,offset,scorefn):# Document numbers are negated before putting them in the heap so# that higher document numbers have lower "priority" in the queue.# Lower document numbers should always come before higher document# numbers with the same score to keep the order stable.negated_offsetid=0-offsetidifaddall:# We're just adding all matchesitems.append((score,negated_offsetid))eliflen(items)<limit:# The heap isn't full, so add this documentheappush(items,(score,negated_offsetid))else:# The heap is full, but if this document has a high enough# score to make the top N, add it to the heapifscore>items[0][0]:heapreplace(items,(score,negated_offsetid))self.minscore=items[0][0]defpull_matches(self,q,offset,scorefn):"""Low-level method yields (docid, score) pairs from the given matcher. Called by :meth:`Collector.add_matches`. """allow=self.allowrestrict=self.restrictreplace=self.replacecollect=self.collectminscore=self.minscorereplacecounter=0timelimited=bool(self.timelimit)matcher=q.matcher(self.subsearcher)usequality=self.use_block_quality(self.subsearcher,matcher)termlists=self.termlistsrecordterms=termlistsisnotNoneifrecordterms:termmatchers=list(matcher.term_matchers())else:termmatchers=None# A flag to indicate whether we should check block quality at the start# of the next loopcheckquality=Truewhilematcher.is_active():# If the replacement counter has reached 0, try replacing the# matcher with a more efficient versionifreplace:ifreplacecounter==0orself.minscore!=minscore:matcher=matcher.replace(minscoreor0)self.replaced_times+=1ifnotmatcher.is_active():breakusequality=self.use_block_quality(self.subsearcher,matcher)replacecounter=replaceminscore=self.minscoreifrecordterms:termmatchers=list(matcher.term_matchers())replacecounter-=1# Check whether the time limit expired since the last matchiftimelimitedandself.timedoutandnotself.greedy:raiseTimeLimit# If we're using block quality optimizations, and the checkquality# flag is true, try to skip ahead to the next block with the# minimum required qualityifusequalityandcheckqualityandminscoreisnotNone:self.skipped_times+=matcher.skip_to_quality(minscore)# Skipping ahead might have moved the matcher to the end of the# posting listifnotmatcher.is_active():break# The current document IDdocid=matcher.id()offsetid=docid+offset# Check whether the document is filteredif((notalloworoffsetidinallow)and(notrestrictoroffsetidnotinrestrict)):# Collect and yield this documentifscorefn:score=scorefn(matcher)collect(docid,offsetid,score)else:score=matcher.score()collect(docid,offsetid,0-score)yield(score,offsetid)# If recording terms, add the document to the termlistsifrecordterms:formintermmatchers:ifm.is_active()andm.id()==docid:termlists[m.term()].add(offsetid)# Check whether the time limit expirediftimelimitedandself.timedout:raiseTimeLimit# Move to the next document. This method returns True if the# matcher has entered a new block, so we should check block quality# again.checkquality=matcher.next()defsort(self,global_searcher,q,sortedby,reverse=False,allow=None,restrict=None):self.searcher=global_searcherself.q=qself.docset=set()self._set_filters(allow,restrict)self._reset()self._set_timer()items=self.itemslimit=self.limitheapfn=nlargestifreverseelsensmallestaddall=self.should_add_all()facet=sorting.MultiFacet.from_sortedby(sortedby)catter=facet.categorizer(global_searcher)t=now()ifglobal_searcher.is_atomic():searchers=[(global_searcher,0)]else:searchers=global_searcher.subsearchersforsegment_searcher,offsetinsearchers:self.subsearcher=segment_searcherself._set_categorizers(segment_searcher,offset)catter.set_searcher(segment_searcher,offset)ifcatter.requires_matcherorself.termlists:ls=list(self.pull_matches(q,offset,catter.key_for_matcher))else:kfi=catter.key_for_idls=list(self.pull_unscored_matches(q,offset,kfi))ifaddall:items.extend(ls)else:items=heapfn(limit,items+ls)self.items=itemsself.runtime=now()-treturnself.results(scores=False,reverse=reverse)defpull_unscored_matches(self,q,offset,keyfn):allow=self.allowrestrict=self.restrictcollect=self.collecttimelimited=bool(self.timelimit)matcher=q.matcher(self.subsearcher)fordocnuminmatcher.all_ids():# Check whether the time limit expired since the last matchiftimelimitedandself.timedoutandnotself.greedy:raiseTimeLimit# The current document IDoffsetid=docnum+offset# Check whether the document is filteredif((notalloworoffsetidinallow)and(notrestrictoroffsetidnotinrestrict)):# Collect and yield this documentkey=keyfn(docnum)collect(docnum,offsetid,key)yield(key,offsetid)# Check whether the time limit expirediftimelimitedandself.timedout:raiseTimeLimitdefresults(self,scores=True,reverse=False):"""Returns the current results from the collector. This is useful for getting the results out of a collector that was stopped by a time limit exception. """ifscores:# Docnums are stored as negative for reasons too obscure to go into# here, re-negate them before returningitems=[(x[0],0-x[1])forxinself.items]# Sort by negated scores so that higher scores go first, then by# document number to keep the order stable when documents have the# same scoreitems.sort(key=lambdax:(0-x[0],x[1]))else:items=sorted(self.items,reverse=reverse)returnResults(self.searcher,self.q,items,self.docset,facetmaps=self.facetmaps,runtime=self.runtime,filter=self.allow,mask=self.restrict,termlists=self.termlists)classResults(object):"""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. """def__init__(self,searcher,q,top_n,docset,facetmaps=None,runtime=-1,filter=None,mask=None,termlists=None,highlighter=None):""" :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. """self.searcher=searcherself.q=qself.top_n=top_nself.docset=docsetself._facetmaps=facetmapsor{}self.runtime=runtimeself._filter=filterself._mask=maskself._termlists=termlistsself.highlighter=highlighterorhighlight.Highlighter()self._char_cache={}def__repr__(self):return"<Top %s Results for %r runtime=%s>"%(len(self.top_n),self.q,self.runtime)def__len__(self):"""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. """ifself.docsetisNone:self._load_docs()returnlen(self.docset)def__getitem__(self,n):ifisinstance(n,slice):start,stop,step=n.indices(len(self.top_n))return[Hit(self,self.top_n[i][1],i,self.top_n[i][0])foriinxrange(start,stop,step)]else:ifn>=len(self.top_n):raiseIndexError("results[%r]: Results only has %s hits"%(n,len(self.top_n)))returnHit(self,self.top_n[n][1],n,self.top_n[n][0])def__iter__(self):"""Yields a :class:`Hit` object for each result in ranked order. """foriinxrange(len(self.top_n)):yieldHit(self,self.top_n[i][1],i,self.top_n[i][0])def__contains__(self,docnum):"""Returns True if the given document number matched the query. """ifself.docsetisNone:self._load_docs()returndocnuminself.docsetdef__nonzero__(self):returnnotself.is_empty()__bool__=__nonzero__defis_empty(self):"""Returns True if not documents matched the query. """returnself.scored_length()==0defitems(self):"""Returns an iterator of (docnum, score) pairs for the scored documents in the results. """return((docnum,score)forscore,docnuminself.top_n)deffields(self,n):"""Returns 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. """returnself.searcher.stored_fields(self.top_n[n][1])deffacet_names(self):"""Returns the available facet names, for use with the ``groups()`` method. """returnself._facetmaps.keys()defgroups(self,name=None):"""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} """if(nameisNoneorname=="facet")andlen(self._facetmaps)==1:# If there's only one facet, just use it; convert keys() to list# for Python 3name=list(self._facetmaps.keys())[0]elifnamenotinself._facetmaps:raiseKeyError("%r not in facet names %r"%(name,self.facet_names()))returnself._facetmaps[name].as_dict()def_load_docs(self):# If self.docset is None, that means this results object was created# block optimizations on, which means we didn't record the matching# documents because we might have skipped some blocks. SOOO, we have to# go back and use docs_for_query to get just the matching docnums. This# is much faster than getting the scored results, but might still be# noticeable for complex queries and/or a large index.docset=set(self.searcher.docs_for_query(self.q))# Apply the filter and mask, if any, from the original searchifself._filter:docset.intersection_update(self._filter)ifself._mask:docset.difference_update(self._mask)self.docset=docsetdefhas_exact_length(self):"""Returns True if this results object already knows the exact number of matching documents. """returnself.docsetisnotNonedefestimated_length(self):"""The estimated maximum number of matching documents, or the exact number of matching documents if it's known. """ifself.docsetisnotNone:returnlen(self.docset)returnself.q.estimate_size(self.searcher.reader())defestimated_min_length(self):"""The estimated minimum number of matching documents, or the exact number of matching documents if it's known. """ifself.docsetisnotNone:returnlen(self.docset)returnself.q.estimate_min_size(self.searcher.reader())defscored_length(self):"""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. """returnlen(self.top_n)defdocs(self):"""Returns a set-like object containing the document numbers that matched the query. """ifself.docsetisNone:self._load_docs()returnself.docsetdefcopy(self):"""Returns a copy of this results object. """topcopy=list(self.top_n)setcopy=copy.copy(self.docset)returnself.__class__(self.searcher,self.q,topcopy,setcopy,runtime=self.runtime,filter=self._filter,mask=self._mask)defscore(self,n):"""Returns 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. """returnself.top_n[n][0]defdocnum(self,n):"""Returns the document number of the result at position n in the list of ranked documents. """returnself.top_n[n][1]defquery_terms(self,expand=False):returnself.q.existing_terms(self.searcher.reader(),expand=expand)defhas_matched_terms(self):"""Returns True if the search recorded which terms matched in which documents. >>> r = searcher.search(myquery) >>> r.has_matched_terms() False >>> """returnself._termlistsisnotNonedefmatched_terms(self):"""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")]) """ifself._termlistsisNone:raiseNoTermsExceptionreturnset(self._termlists.keys())def_get_fragmenter(self):returnself.highlighter.fragmenterdef_set_fragmenter(self,f):self.highlighter.fragmenter=ffragmenter=property(_get_fragmenter,_set_fragmenter)def_get_formatter(self):returnself.highlighter.formatterdef_set_formatter(self,f):self.highlighter.formatter=fformatter=property(_get_formatter,_set_formatter)def_get_scorer(self):returnself.highlighter.scorerdef_set_scorer(self,s):self.highlighter.scorer=sscorer=property(_get_scorer,_set_scorer)def_get_order(self):returnself.highlighter.orderdef_set_order(self,o):self.highlighter.order=oorder=property(_get_order,_set_order)defkey_terms(self,fieldname,docs=10,numterms=5,model=classify.Bo1Model,normalize=True):"""Returns the 'numterms' most important terms from the top 'numdocs' 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 terms: Return this number of important terms. :param model: The classify.ExpansionModel to use. See the classify module. :returns: list of unicode strings. """ifnotlen(self):return[]docs=min(docs,len(self))reader=self.searcher.reader()expander=classify.Expander(reader,fieldname,model=model)for_,docnuminself.top_n[:docs]:expander.add_document(docnum)returnexpander.expanded_terms(numterms,normalize=normalize)defextend(self,results):"""Appends hits from 'results' (that are not already in this results object) to the end of these results. :param results: another results object. """docs=self.docs()foriteminresults.top_n:ifitem[1]notindocs:self.top_n.append(item)self.docset=docs|results.docs()deffilter(self,results):"""Removes any hits that are not also in the other results object. """ifnotlen(results):returnotherdocs=results.docs()items=[itemforiteminself.top_nifitem[1]inotherdocs]self.docset=self.docs()&otherdocsself.top_n=itemsdefupgrade(self,results,reverse=False):"""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. """ifnotlen(results):returnotherdocs=results.docs()arein=[itemforiteminself.top_nifitem[1]inotherdocs]notin=[itemforiteminself.top_nifitem[1]notinotherdocs]ifreverse:items=notin+areinelse:items=arein+notinself.top_n=itemsdefupgrade_and_extend(self,results):"""Combines the effects of extend() and increase(): 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. """ifnotlen(results):returndocs=self.docs()otherdocs=results.docs()arein=[itemforiteminself.top_nifitem[1]inotherdocs]notin=[itemforiteminself.top_nifitem[1]notinotherdocs]other=[itemforiteminresults.top_nifitem[1]notindocs]self.docset=docs|otherdocsself.top_n=arein+notin+otherclassHit(object):"""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"] """def__init__(self,results,docnum,pos=None,score=None):""" :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. """self.results=resultsself.searcher=results.searcherself.pos=self.rank=posself.docnum=docnumself.score=scoreself._fields=Nonedeffields(self):"""Returns a dictionary of the stored fields of the document this object represents. """ifself._fieldsisNone:self._fields=self.searcher.stored_fields(self.docnum)returnself._fieldsdefmatched_terms(self):"""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()) """termlists=self.results._termlistsiftermlistsisNone:raiseNoTermsException# termlists maps terms->set of docnums, so we have to check every term# to see if this document is in its lists=set()fortermintermlists.keys():ifself.docnumintermlists[term]:s.add(term)returnsdefhighlights(self,fieldname,text=None,top=3):"""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. """hliter=self.results.highlighterreturnhliter.highlight_hit(self,fieldname,text=text,top=top)defmore_like_this(self,fieldname,text=None,top=10,numterms=5,model=classify.Bo1Model,normalize=True,filter=None):"""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. """returnself.searcher.more_like(self.docnum,fieldname,text=text,top=top,numterms=numterms,model=model,normalize=normalize,filter=filter)defcontains_term(self,fieldname,text):"""Returns True if the given query term exists in this document. This only works for terms that were in the original query. """termlists=self.results._termlistsiftermlistsisnotNone:term=(fieldname,text)iftermintermlists:docset=termlists[term]returnself.docnumindocsetreturnFalsedef__repr__(self):return"<%s%r>"%(self.__class__.__name__,self.fields())def__eq__(self,other):ifisinstance(other,Hit):returnself.fields()==other.fields()elifisinstance(other,dict):returnself.fields()==otherelse:returnFalsedef__len__(self):returnlen(self.fields())def__iter__(self):returniterkeys(self.fields())def__getitem__(self,key):returnself.fields().__getitem__(key)def__contains__(self,key):returnkeyinself.fields()defitems(self):returnlist(self.fields().items())defkeys(self):returnlist(self.fields().keys())defvalues(self):returnlist(self.fields().values())defiteritems(self):returniteritems(self.fields())defiterkeys(self):returniterkeys(self.fields())defitervalues(self):returnitervalues(self.fields())defget(self,key,default=None):returnself.fields().get(key,default)def__setitem__(self,key,value):raiseNotImplementedError("You cannot modify a search result")def__delitem__(self,key,value):raiseNotImplementedError("You cannot modify a search result")defclear(self):raiseNotImplementedError("You cannot modify a search result")defupdate(self,dict=None,**kwargs):raiseNotImplementedError("You cannot modify a search result")classResultsPage(object):"""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() """def__init__(self,results,pagenum,pagelen=10):""" :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. """self.results=resultsself.total=len(results)ifpagenum<1:raiseValueError("pagenum must be >= 1")self.pagecount=int(ceil(self.total/pagelen))ifpagenum>1andpagenum>self.pagecount:raiseValueError("Asked for page %s of %s"%(pagenum,self.pagecount))self.pagenum=pagenumoffset=(pagenum-1)*pagelenif(offset+pagelen)>self.total:pagelen=self.total-offsetself.offset=offsetself.pagelen=pagelendef__getitem__(self,n):offset=self.offsetifisinstance(n,slice):start,stop,step=n.indices(self.pagelen)returnself.results.__getitem__(slice(start+offset,stop+offset,step))else:returnself.results.__getitem__(n+offset)def__iter__(self):returniter(self.results[self.offset:self.offset+self.pagelen])def__len__(self):returnself.totaldefscored_length(self):returnself.results.scored_length()defscore(self,n):"""Returns the score of the hit at the nth position on this page. """returnself.results.score(n+self.offset)defdocnum(self,n):"""Returns the document number of the hit at the nth position on this page. """returnself.results.docnum(n+self.offset)defis_last_page(self):"""Returns True if this object represents the last page of results. """returnself.pagecount==0orself.pagenum==self.pagecount