使用django和postgresql的简易fts
django-searchable的Python项目详细描述
使用django和postgresql轻松进行全文搜索。理智的默认值+向量字段、索引和数据库触发器的自动创建。
示例
使用SearchableModel
类扩展模型,并使用SearchableTextField
类自动设置全文搜索:
fromdjango.db.modelsimportTextFieldfromdjango_searchable.modelsimportSearchableModel,SearchableTextFieldclassBlog(SearchableModel):author_name=TextField()# will NOT have FTS setup automaticallytitle=SearchableTextField()# will have FTS setup automaticallytext=SearchableTextField()# will have FTS setup automatically
然后通过博客管理器进行搜索:
# takes a string of space separated termsresults=Blog.objects.search('spiderman suits')# or a list of termsresults=Blog.objects.search(['water','baskets','leaking'])# or a SearchQuery objectfromdjango.contrib.postgres.searchimportSearchQueryquery=~SearchQuery('superman')&SearchQuery('batman')results=Blog.objects.search(query)
.search
添加一个rank
注释,并自动过滤和排序生成的queryset。
默认情况下,.search
将搜索模型上的所有SearchableTextField
字段,但您可以指定任何子集:
results=Blog.objects.search('who is venom',fields=['title'])
贡献
pipenv install --dev createuser -s -P test_user # use 'password' pipenv run ./manage.py test