Core module¶
Search API core module
Contains the main functions to perform a search.
-
class
search.core.SearchEngine(attributes, limit=-1, threshold=0.75, weights=None)[source]¶ Creates callable objects to perform lazy search in iterables of objects. The engine is customizable upon creation, and when the function is called.
Search can be performed calling directly the object
>>> search = SearchEngine(['attr_name'], limit=10) >>> result = search('john doe', [people])
or through the search method
>>> search_engine = SearchEngine(['attr_name'], limit=10) >>> result = search_engine.search('john doe')
For actual documentation on the search functionality and parameters refer to the
SearchEngine.searchmethod documentation.-
search(query, dataset, attributes=None, limit=None, threshold=None, weights=None)[source]¶ Main function of the package, allows to do a fuzzy full-text search on the rows of the given table model, looking up the value on the given attributes list against the passed query string.
Extra arguments allows customization on the search results (see below)
Note
Since this function implements the core functionality, it has the shortcut import
>>> from search.core import search >>> from search import search
Will have the same effect
Parameters: - query (str) – String to search for
- attributes (list) – The names of thetable columns to search into.
- dataset (iterable) – iterable of objects to lookup. All objects in the dataset must have the specified attribute(s)
- limit (int) – max number of results to return. if
-1will return everything. - threshold (float) – paragon for validating match results.
- weights (list) – matching attributes argument, describes the attributes weights. if not provided or if different length the weight will generated automatically, considering the index of the attribute name, reversed.
Returns: A list containing
[0:limit]resources from the given dataset, sorted by relevance.Return type: list
Raises: AttributeError– if one of the object does not have one of the given attribute(s).Example
Assuming a random number of items in the Item table, that defines name, category, description, availability, one can do:
>>> from models import Item >>> from search import search >>> results = search('aweso', ['name', 'category'], Item.select()) [ <Item name: 'awesome item' cat: 'generic'>, <Item name: 'normal item', cat: 'awesome'>, ]
Note that even though the category is a perfect match, it’s ranked lower priority, so it comes after.
-