pyreds
0.1.4
紅色是對Node.js的輕巧重量搜索。
Pyreds是紅色的Python港口。
Pyreds需要運行的Redis服務器。有關安裝說明,請參見Redis的快速入門。
要安裝pyreds,簡單地:
$ pip install pyreds您可能需要安裝NLTK數據:
>>> import nltk
>>> nltk.download( ' stopwords ' )您要做的第一件事是創建一個搜索實例,該實例允許您傳遞REDIS內命名領域的鍵,以便您可以在同一DB中進行多個搜索。
>>> import pyreds
>>> search = pyreds.create_search( ' pets ' )Pyreds針對基於任意數字或字符串的ID進行操作,因此您可以使用此庫,甚至可以使用您想要的任何內容,甚至結合數據存儲。下面的示例只使用了我們的“數據庫”列表,其中包含一些字符串,我們通過調用搜索#index()填充文本主體和某種ID,在這種情況下,在這種情況下,在這種情況下,它們添加到了pyreds中,在這種情況下為索引。
>>> 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)要針對Pyreds執行查詢,只需用一個返回查詢實例的字符串調用搜索#query()。然後調用查詢#end(),否則返回ID列表,或者否則返回一個空列表。
>>> ids = search.query( ' Tobi dollars ' ).end()
>>> print ( ' Search results for "Tobi dollars" ' ))
>>> for id in ids:
... print ( ' - {} ' .format(strs[ id ]))默認情況下,pyreds執行搜索詞的交集。上一個示例將產生以下輸出,因為只有一個字符串包含“ Tobi”和“美元”:
Search results for "Tobi dollars":
- Tobi wants four dollars我們可以通過在搜索#query(Query()和Query#end()之間傳遞“ Union”或“或“ search”(),我們可以調整pyreds以執行聯合,以表明計算的任何常數都可能存在以供ID匹配。
>>> ids = search.query( ' tobi dollars ' ).type( ' or ' ).end()
>>> print ( ' Search results for "Tobi dollars" ' ))
>>> for id in ids:
... print ( ' - {} ' .format(strs[ id ]))工會搜索將產生以下內容,因為三個字符串包含“ Tobi”或“美元”:
Search results for "tobi dollars":
- Tobi wants four dollars
- Tobi only wants $4
- Loki, Jane, and Tobi are ferrets>>> 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()麻省理工學院許可證