蟒蛇接口
pyroonga的Python项目详细描述
这是什么?
用于groonga全文搜索引擎的python接口。
要求
- python 2.6或3.x及更高版本
- 格隆加
安装
来自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"
对于更复杂的查询,请使用pyronga.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)"
也不要使用匹配列:
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()后切换到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()方法。
建议
n.b.groonga的建议功能仍在草稿中。
首先,如果尚未创建,请创建表:
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))
更多信息
还是没写。
许可证
Pironga是根据麻省理工学院的许可证授权的。
更改日志
V0.5.2(2013-09-17)
- 多字节支持
- 添加“filter”api
- 一些更改
V0.5.1(2013-08-17)
- 更多支持groonga的查询
- 添加GroongaRecord并用于映射它而不是表子类
- 连接到groonga的主机和端口现在可以由pyroonga.Groonga 的构造函数指定
V0.5(2013-07-30)
- 将包名称从pyronga.orm更改为pyronga.odm(注意,与旧版本不兼容)
- 添加匹配列和查询api
- 将许可证更改为MIT
- 修复issues#2
v0.4(2012-03-28)
- 添加建议
v0.3(2012-02-17)
- 将数据添加到groonga
V0.2(2012-02-17)
- 添加orm
- 添加基本用法文档
v0.1(2012-02-05)
- 首次发布