groonga的Python接口
项目描述
这是什么?
为groonga全文搜索引擎提供的Python接口。
需求
Python 2.6或3.x及更高版本
groonga
安装
从pypi
% pip install pyroonga
从源
% python setup.py install
用法
首先,请以服务器模式或守护进程模式运行groonga。见以下
# server mode % groonga -s DB_PATH_NAME # daemon mode % groonga -d DB_PATH_NAME
使用groonga --help获取更多选项。
创建表
from pyroonga import tablebase, Column, Groonga # create the base class for table definition. Table = tablebase() # define the table class Site(Table): title = Column() name = Column() class Blog(Table): entry = Column() # create and bind the groonga connection object grn = Groonga() Table.bind(grn) # create the all table on groonga's database Table.create_all()
数据加载
data = [Site(_key='key1', title='foo', name='hoge'), Site(_key='key2', title='bar', name='fuga'), Site(_key='key3', title='baz', name='piyo'), Site(_key='key4', title='qux', name='xyzzy')] Site.load(data)
上面的示例是立即将数据加载到groonga中。还支持伪提交和回滚
data1 = [Site(_key='key5', title='Constellation', name='Sagittarius'), Site(_key='key6', title='Constellation', name='Pisces')] # first load, but not load to groonga actually data = Site.load(data1, immediate=False) data2 = [Site(_key='key7', title='Constellation', name='Aquarius')] data.load(data2) # same as previous # load data to groonga actually data.commit()
或重置加载的数据
data.rollback()
请注意,只有当设置为immediate=False时,才能重置加载的数据。
查询并获取映射对象的数据
从Site表中获取所有数据
data = Site.select().all()
并打印数据
for row in data: print(row._id, row._key, row.title)
全文搜索查询
Site.select().match_columns(Site.title).query('foo').all() Site.select().match_columns(Site.title, Site.name).query('bar').all()
上面的示例与以下查询相同
select --table Site --match_columns 'title' --query "foo" select --table Site --match_columns 'title OR name' --query "bar"
使用pyroonga.odm.GE进行更复杂的查询
from pyroonga.odm import GE Site.select().match_columns(Site.title).query(GE('foo') | GE('bar')).all()
上面的示例与以下查询相同
select --table Site --match_columns 'title' --query "(foo OR bar)"
并且不使用match_columns
Site.select(title='foo').all() Site.select(title='foo', name='bar').all() # "or" search
上面的示例与以下查询相同
select --table Site --query "(title:@\"foo\")" select --table Site --query "(title:@\"foo\" OR name:@\"bar\")"
条件搜索查询
Site.select(Site.title == 'bar').all()
条件的组合
Site.select((Site._id > 3) & (Site.title == 'baz')).all()
限制和偏移量
Site.select().limit(3).offset(2).all()
排序
Site.select().sortby(Site._id).all() # asc Site.select().sortby(-Site._id).all() # desc
选择输出列
# get the title and name columns Site.select().output_columns(Site.title, Site.name).all() # get the all columns Site.select().output_columns(Site.ALL).all()
钻取
在从select()方法链调用drilldown()之后切换到钻取查询
data = Site.select().sortby(Site._key).drilldown(Site.title).all()
钻取的结果将被存储到 all() 方法的返回值的 drilldown 属性中。
for drilldown in data.drilldown: print(drilldown._key, drilldown._nsubrecs)
如上例中的 sortby() 方法,它是 --sortby 的查询选项。对于钻取的排序,请在调用 drilldown() 方法后调用 sortby() 方法。
Site.select().drilldown(Site.title).sortby(Site._key).all()
如上例中的 sortby() 方法,它是 --drilldown_sortby 的查询选项。当然,还包括 limit()、offset() 和 output_columns() 方法。
建议
注意。 Groonga 的 suggest 功能仍在草案阶段。
首先,如果尚未创建,请创建表
from pyroonga import SuggestTable grn = Groonga() SuggestTable.bind(grn) SuggestTable.create_all()
其次,数据加载
import time from pyroonga import event_query data = [event_query(time=time.time(), sequence=1, item='e'), event_query(time=time.time(), sequence=1, item='en'), event_query(time=time.time(), sequence=1, item='eng'), event_query(time=time.time(), sequence=1, item='engi'), event_query(time=time.time(), sequence=1, item='engin'), event_query(time=time.time(), sequence=1, item='engine', type='submit')] event_query.load(data)
最后,查询
from pyroonga import item_query, SuggestType query = 'en' result = item_query.suggest(query).types(SuggestType.complete). \ frequency_threshold(1).all() for r in result.complete: print("key is '%s', score is %s" % (r._key, r._score))
更多信息
尚未编写。
另请参阅
许可证
pyroonga 采用 MIT 许可证。
变更日志
v0.5.2 (2013-09-17)
多字节支持
添加了 'filter' API
一些更改
v0.5.1 (2013-08-17)
更多支持 Groonga 的查询
添加了 GroongaRecord 并用它来映射,而不是使用 Table 子类
现在可以通过 pyroonga.Groonga 的构造函数指定连接到 Groonga 的主机和端口
v0.5 (2013-07-30)
将包名从 pyroonga.orm 更改为 pyroonga.odm (注意与旧版本不兼容)
添加了 match_columns 和 query API
将许可证更改为 MIT
修复了 issues#2
v0.4 (2012-03-28)
添加 suggest
v0.3 (2012-02-17)
添加将数据加载到 groonga 的功能
v0.2 (2012-02-17)
添加 ORM
添加基本用法文档
v0.1 (2012-02-05)
第一个版本
项目详情
pyroonga-0.5.2.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | e68c69cc2d7566862600578c3329641e94dc1270aeb4778613cc67d88eaf5acb |
|
MD5 | 31d285c816da0c48ea6dce174c052f3a |
|
BLAKE2b-256 | 28abf6aca0a3826853f4a6f9bdd5b011662fde0cfac637ee94209dac612bef88 |