API

class RedisEngine(min_length=2, prefix='ac', stop_words=None, cache_timeout=300, **conn_kwargs)
Parameters:
  • min_length (integer) – the minimum length a phrase has to be to return meaningful search results
  • prefix (string) – a prefix used for all keys stored in Redis to allow multiple “indexes” to exist and to make deletion easier.
  • stop_words (set) – a set of stop words to remove from index/search data
  • cache_timeout (integer) – how long to keep around search results
  • conn_kwargs – any named parameters that should be used when connecting to Redis, e.g. host='localhost', port=6379

RedisEngine is responsible for storing and searching data suitable for autocompletion. There are many different options you can use to configure how autocomplete behaves, but the defaults are intended to provide good general performance for searching things like blog post titles and the like.

The underlying data structure used to provide autocompletion is a sorted set, details are described in this post.

Usage:

from redis_completion import RedisEngine
engine = RedisEngine()
store(obj_id[, title=None[, data=None]])
Parameters:
  • obj_id – a unique identifier for the object
  • title – a string to store in the index and allow autocompletion on, which, if not provided defaults to the given obj_id
  • data – any data you wish to store and return when a given title is searched for. If not provided, defaults to the given title (or obj_id)

Store an object in the index and allow it to be searched for.

Examples:

engine.store('some phrase')
engine.store('some other phrase')

In the following example a list of blog entries is being stored in the index. Note that arbitrary data can be stored in the index. When a search is performed this data will be returned.

for entry in Entry.select():
    engine.store(entry.id, entry.title, json.dumps({
        'id': entry.id,
        'published': entry.published,
        'title': entry.title,
        'url': entry.url,
    })
store_json(obj_id, title, data)

Like store() except data is automatically serialized as JSON before being stored in the index. Best when used in conjunction with search_json().

remove(obj_id)
Parameters:obj_id – a unique identifier for the object

Removes the given object from the index.

search(phrase[, limit=None[, filters=None[, mappers=None]]])
Parameters:
  • phrase – search the index for the given phrase
  • limit – an integer indicating the number of results to limit the search to.
  • filters – a list of callables which will be used to filter the search results before returning to the user. Filters should take a single parameter, the data associated with a given object and should return either True or False. A False value returned by any of the filters will prevent a result from being returned.
  • mappers – a list of callables which will be used to transform the raw data returned from the index.
Return type:

A list containing data returned by the index

Note

Mappers act upon data before it is passed to the filters

Assume we have stored some interesting blog posts, encoding some metadata using JSON:

>>> engine.search('python', mappers=[json.loads])
[{'published': True, 'title': 'an entry about python', 'url': '/blog/1/'},
 {'published': False, 'title': 'using redis with python', 'url': '/blog/3/'}]
search_json(phrase[, limit=None[, filters=None[, mappers=None]]])

Like search() except json.loads is inserted as the very first mapper. Best when used in conjunction with store_json().

Previous topic

Schema

This Page