You’ll usually want to open the searcher using a with statement so the
searcher is automatically closed when you’re done with it (searcher objects
represent a number of open files, so if you don’t explicitly close them and the
system is slow to collect them, you can run out of file handles):

withix.searcher()assearcher:...

This is of course equivalent to:

try:searcher=ix.searcher()...finally:searcher.close()

The Searcher object is the main high-level interface for reading the index. It
has lots of useful methods for getting information about the index, such as
lexicon(fieldname).

By default, Searcher.search(myquery) limits the number of hits to 20, So the
number of scored hits in the Results object may be less than the number of
matching documents in the index.

>>> # How many documents in the entire index would have matched?>>> len(results)27>>> # How many scored and sorted documents in this Results object?>>> # This will often be less than len() if the number of hits was limited>>> # (the default).>>> results.scored_length()10

Calling len(Results) runs a fast (unscored) version of the query again to
figure out the total number of matching documents. This is usually very fast
but for large indexes it can cause a noticeable delay. If you want to avoid
this delay on very large indexes, you can use the
has_exact_length(),
estimated_length(), and
estimated_min_length() methods to estimate the
number of matching documents without calling len():

You can use the filter keyword argument to search() to specify a set of
documents to permit in the results. The argument can be a
whoosh.query.Query object, a whoosh.searching.Results object,
or a set-like object containing document numbers. The searcher caches filters
so if for example you use the same query filter with a searcher multiple times,
the additional searches will be faster because the searcher will cache the
results of running the filter query

You can also specify a mask keyword argument to specify a set of documents
that are not permitted in the results.

withmyindex.searcher()ass:qp=qparser.QueryParser("content",myindex.schema)user_q=qp.parse(query_string)# Only show documents in the "rendering" chapterallow_q=query.Term("chapter","rendering")# Don't show any documents where the "tag" field contains "todo"restrict_q=query.Term("tag","todo")results=s.search(user_q,filter=allow_q,mask=restrict_q)

(If you specify both a filter and a mask, and a matching document
appears in both, the mask “wins” and the document is not permitted.)

To find out how many results were filtered out of the results, use
results.filtered_count (or resultspage.results.filtered_count):

# Was this results object created with terms=True?ifresults.has_matched_terms():# What terms matched in the results?print(results.matched_terms())# What terms matched in each hit?forhitinresults:print(hit.matched_terms())

Whoosh lets you eliminate all but the top N documents with the same facet key
from the results. This can be useful in a few situations:

Eliminating duplicates at search time.

Restricting the number of matches per source. For example, in a web search
application, you might want to show at most three matches from any website.

Whether a document should be collapsed is determined by the value of a “collapse
facet”. If a document has an empty collapse key, it will never be collapsed,
but otherwise only the top N documents with the same collapse key will appear
in the results.

withmyindex.searcher()ass:# Set the facet to collapse on and the maximum number of documents per# facet value (default is 1)results=s.collector(collapse="hostname",collapse_limit=3)# Dictionary mapping collapse keys to the number of documents that# were filtered out by collapsing on that keyprint(results.collapsed_counts)

Collapsing works with both scored and sorted results. You can use any of the
facet types available in the whoosh.sorting module.

By default, Whoosh uses the results order (score or sort key) to determine the
documents to collapse. For example, in scored results, the best scoring
documents would be kept. You can optionally specify a collapse_order facet
to control which documents to keep when collapsing.

For example, in a product search you could display results sorted by decreasing
price, and eliminate all but the highest rated item of each product type:

The collapsing happens during the search, so it is usually more efficient than
finding everything and post-processing the results. However, if the collapsing
eliminates a large number of documents, collapsed search can take longer
because the search has to consider more documents and remove many
already-collected documents.

Since this collector must sometimes go back and remove already-collected
documents, if you use it in combination with
TermsCollector and/or
FacetCollector, those collectors may contain
information about documents that were filtered out of the final results by
collapsing.

fromwhoosh.collectorsimportTimeLimitCollector,TimeLimitwithmyindex.searcher()ass:# Get a collector objectc=s.collector(limit=None,sortedby="title_exact")# Wrap it in a TimeLimitedCollector and set the time limit to 10 secondstlc=TimeLimitedCollector(c,timelimit=10.0)# Try searchingtry:s.search_with_collector(myquery,tlc)exceptTimeLimit:print("Search took too long, aborting!")# You can still get partial results from the collectorresults=tlc.results()

For example, you might have a “best bet” field. This field contains hand-picked
keywords for documents. When the user searches for those keywords, you want
those documents to be placed at the top of the results list. You could try to
do this by boosting the “bestbet” field tremendously, but that can have
unpredictable effects on scoring. It’s much easier to simply run the query
twice and combine the results:

# Parse the user queryuserquery=queryparser.parse(querystring)# Get the terms searched fortermset=set()userquery.existing_terms(termset)# Formulate a "best bet" query for the terms the user# searched for in the "content" fieldbbq=Or([Term("bestbet",text)forfieldname,textintermsetiffieldname=="content"])# Find documents matching the searched for termsresults=s.search(bbq,limit=5)# Find documents that match the original queryallresults=s.search(userquery,limit=10)# Add the user query results on to the end of the "best bet"# results. If documents appear in both result sets, push them# to the top of the combined results.results.upgrade_and_extend(allresults)

The Results object supports the following methods:

Results.extend(results)

Adds the documents in ‘results’ on to the end of the list of result
documents.

Results.filter(results)

Removes the documents in ‘results’ from the list of result documents.

Results.upgrade(results)

Any result documents that also appear in ‘results’ are moved to the top
of the list of result documents.

Results.upgrade_and_extend(results)

Any result documents that also appear in ‘results’ are moved to the top
of the list of result documents. Then any other documents in ‘results’ are
added on to the list of result documents.