Django的PGRONGA实用程序。
django-pgroonga的Python项目详细描述
django-pgroonga Django是用于利用PGroonga的实用程序
设置PGroonga
为了在PGroonga中进行全文检索,进行以下设定
- PGroonga Extension注册
- 创建搜索索引
在制作数据库后只进行一次Extension的注册。每个要搜索的列分别创建搜索索引。
这个设定可以手动执行SQL,但是可以作为Django的微分进行登录和执行。
创建空麦格
$ python3 manage.py makemigration testapp --empty Migrations for'testapp': testapp/migrations/0002_auto_20161210_1544.py:
编辑我的
创建的微调(在上面的例子中testapp/migrations/0002_auto_20161210_1544.py)编辑如下:在此示例中,在表格中^{tt1}的text1列中创建全文搜索索引。
from__future__importunicode_literalsfromdjango.dbimportmigrationsclassMigration(migrations.Migration):dependencies=[('testapp','0001_initial'),]operations=[# pgroonga を登録する(一回のみ)migrations.RunSQL('CREATE EXTENSION pgroonga','DROP EXTENSION pgroonga',),# 全文検索用インデックスを作成する(検索対象のカラムごとに作成)migrations.RunSQL('CREATE INDEX idx1 ON testapp_testmodel USING pgroonga (id, text1)','DROP INDEX idx1',)]
查找
您可以使用Django的contains搜索所有索引来创建PGroonga索引。
TestModel(text1='あいうえお').save()# 日本語のテキストを登録TestModel.objects.filter(text1__contains='あいうえお')
此搜索将被执行为like使用运算符搜索指定单词的SQL。
@@使用运算符,在Groonga的查询语法中查找时,指定^{tt6}作为查找。
TestModel.objects.filter(text1__groonga='あいうえお OR かきくけこ')
搜索得分
使用函数获取搜索匹配度^{tt7}
fromdjango_pgroongaimportScoreforrinTestModel.objects.annotate(score=Score(TestModel)).order_by('-score').filter(text1__contains='あいうえお'):print(r.score,r.text1)
django_pgroonga.Score()调用pgroonga.score函数来获得得分。得分只能在PGroonga索引中包含表格的主关键字时获得,如果不包含则总是返回^{tt10}美元。
LICENSE
Copyright(c)2016 Atsuo Ishimoto
Permission is hereby granted,free of charge,to any person obtaing a copy of this software and associated documentation files(the“Software”),to deal in tocumentation files(the“Software”),to deal in todeal in toal in tal in ttion tiles in the he Software without restriction,including without limititition the rights to use,copy,modify,merge,publish,distribute,sublicense,and/or sell copies of tf tof tof tof tlis triblicense,se,sublicense,and/or selllll copielll cophe Software,and to permit persons to whom the Software is furnished to do so,subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED“AS IS”,WITHOUT WARRANTY OF ANY KIND,EXPRESS OR IMPLIED,INCLUDING BUT NOT LIMIMITED TO THE WARRANTIES OF MERCHANTABILITY,THE WARRRANTIES OF MERCHANTANTABILITABITY,ILITY,TY,ITY FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER LIABILILIIIABILILIILIIILIIILIIILI FORLLAIM,DAMAGES OR OR OTHER OTHER OTHTY,WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF OR IN CONECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.