Simple full text search module for Python - backed by Redis
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

README.rst

pyreds

https://travis-ci.org/7anshuai/pyreds.svg?branch=master

reds is a light-weight Redis Search for Node.js.

pyreds is a Python port of reds.

Installation

pyreds requires a running Redis server. See Redis's quickstart for installation instructions.

To install pyreds, simply:

$ pip install pyreds

You may need install NLTK Data:

>>> import nltk
>>> nltk.download('stopwords')

Getting Started

The first thing you'll want to do is create a Search instance, which allow you to pass a key, used for namespacing within Redis so that you may have several searches in the same db.

>>> import pyreds
>>> search = pyreds.create_search('pets')

pyreds acts against arbitrary numeric or string based ids, so you could utilize this library with essentially anything you wish, even combining data stores. The following example just uses a list for our "database", containing some strings, which we add to pyreds by calling Search#index() padding the body of text and an id of some kind, in this case the index.

>>> strs = []
>>> strs.append('Tobi wants four dollars')
>>> strs.append('Tobi only wants $4')
>>> strs.append('Loki is really fat')
>>> strs.append('Loki, Jane, and Tobi are ferrets')
>>> strs.append('Manny is a cat')
>>> strs.append('Luna is a cat')
>>> strs.append('Mustachio is a cat')
>>> for i, v in enumerate(strs):
...     search.index(v, i)

To perform a query against pyreds simply invoke Search#query() with a string, which return a Query instance. Then invoke Query#end(), which return a list of ids when present, or an empty list otherwise.

>>> ids = search.query('Tobi dollars').end()
>>> print('Search results for "Tobi dollars"'))
>>> for id in ids:
...     print('  - {}'.format(strs[id]))

By default pyreds performs an intersection of the search words. The previous example would yield the following output since only one string contains both "Tobi" and "dollars":

Search results for "Tobi dollars":
    - Tobi wants four dollars

We can tweak pyreds to perform a union by passing either "union" or "or" to Search#type() between Search#query() and Query#end(), indicating that any of the constants computed may be present for the id to match.

>>> ids = search.query('tobi dollars').type('or').end()
>>> print('Search results for "Tobi dollars"'))
>>> for id in ids:
...     print('  - {}'.format(strs[id]))

The union search would yield the following since three strings contain either "Tobi" or "dollars":

Search results for "tobi dollars":
    - Tobi wants four dollars
    - Tobi only wants $4
    - Loki, Jane, and Tobi are ferrets

API

>>> search = pyreds.create_search(key)
>>> search.index(text, id)
>>> search.remove(id)
>>> query = search.query(text[, type]) # will return a `Query` instance
>>>
>>> query.between(start, stop)
>>> query.type(type)
>>> query.end()

LICENSE

The MIT License