lass whoosh.searching.Results(searcher, q, top_n, docset=None, facetmaps=None, runtime=0, highlighter=None)
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.
| Parameters: |
|
|---|
- copy()
-
Returns a deep copy of this results object.
- docnum(n)
-
Returns the document number of the result at position n in the list of ranked documents.
- docs()
-
Returns a set-like object containing the document numbers that matched the query.
- estimated_length()
-
The estimated maximum number of matching documents, or the exact number of matching documents if it’s known.
- estimated_min_length()
-
The estimated minimum number of matching documents, or the exact number of matching documents if it’s known.
- extend(results)
-
Appends hits from ‘results’ (that are not already in this results object) to the end of these results.
Parameters: results – another results object.
- facet_names()
-
Returns the available facet names, for use with the groups() method.
- fields(n)
-
Returns the stored fields for the document at the n th position in the results. Use Results.docnum() if you want the raw document number instead of the stored fields.
- filter(results)
-
Removes any hits that are not also in the other results object.
- groups(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 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 thewhoosh.sorting.FacetMap.
>>> myfacet = sorting.FieldFacet("tag", maptype=sorting.Count) >>> results = mysearcher.search(myquery, groupedby=myfacet) >>> counts = results.groups() {"new": 3, "apple": 4, "search": 1}
- has_exact_length()
-
Returns True if this results object already knows the exact number of matching documents.
- has_matched_terms()
-
Returns True if the search recorded which terms matched in which documents.
>>> r = searcher.search(myquery) >>> r.has_matched_terms() False >>>
- is_empty()
-
Returns True if not documents matched the query.
- items()
-
Returns an iterator of (docnum, score) pairs for the scored documents in the results.
- key_terms(fieldname, docs=10, numterms=5, model=<class 'whoosh.classify.Bo1Model'>, normalize=True)
-
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.
Parameters: - fieldname – Look at the terms in this field. This field must store vectors.
- docs – Look at this many of the top documents of the results.
- numterms – Return this number of important terms.
- model – The classify.ExpansionModel to use. See the classify module.
Returns: list of unicode strings.
- matched_terms()
-
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")])
- score(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.
- scored_length()
-
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() 20This may be fewer than the total number of documents that match the query, which is what len(Results) returns.
- upgrade(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.
Parameters: - results – another results object.
- reverse – if True, lower the position of hits in the other results object instead of raising them.
- upgrade_and_extend(results)
-
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.
Parameters: results – another results object.
浙公网安备 33010602011771号