haystack+whoosh
麻雀虽小,搜索引擎还是不能少的,要不然每次都得去数据库匹配,django通过haystack搜索框架可以很容易实现一个搜索功能的,whoosh采用的python编写,性能一般但足够用,这里就用这个,开始部署
先安装
pip install Whoosh django-haystack jieba
# Whoosh==2.7.4 django-haystack==2.8.1 jieba==0.39 这是我安装的版本,jieba是提供中文分词功能的,whoosh默认没有对中文适配
用jieba自己制作一个中文分词引擎
# 从你haystack安装目录的backends下拷贝一份whoosh_backend.py 到你项目目录下,这里我放入utils文件夹下并改了个名字,
# 不在原目录下直接修改是为了迁移时又要再一次修改
cp ~/.pyenv/versions/Dweb/lib/python3.6/site-packages/haystack/backends/whoosh_backend.py utils/whoosh_cn_backend.py
并修改如下内容
# 导入中文分析模块
from jieba.analyse import ChineseAnalyzer
# 修改默认的analyzer,在160-170行附近
schema_fields[field_class.index_fieldname] = TEXT(stored=True, analyzer=ChineseAnalyzer(), field_boost=field_class.boost, sortable=True)
别忘了urls里配置url路径
path('search/', include('haystack.urls')),
可以开始写索引了
在你需要搜索的应用目录下,新建search_index.py,注意名字不可更改,内容为
from haystack import indexes
from .models import Articles # 你需要搜索的表
class ArticlesIndex(indexes.SearchIndex, indexes.Indexable):
'''# 类名没要求'''
# use_templates指定根据那些字段建里索引文件,把说明放在一个文件中
text = indexes.CharField(document=True, use_template=True)
def get_model(self):
# 返回模型类
return Articles
def index_queryset(self, using=None):
return self.get_model().objects.filter(is_secret=False)
在模板目录下新建search/index/blog/articles_text.txt和search/search.html文件,
blog为应用名,articles为模型类名,*.txt文件定义你需要根据哪些字段建立索引,内容如下
# 指定根据表中的哪些数据建里索引
{{ object.title }}
{{ object.body }}
{{ object.author_name.username }}
{{ objects.description }}
search.html为你搜索结果展示页面
{# 引入highlight可以高亮显示,分页类似paginotor #}
{% extends 'base.html' %}
{% load staticfiles %}
{% load highlight %}
{% block mainbody %}
<main role="main" class="container">
<div class="my-3 p-3 bg-white rounded shadow-sm" >
<style>
span.highlighted {
color: red;
}
</style>
<div class="d-flex align-items-center p-3 my-3 text-white-50 bg-purple rounded shadow-sm">
<div class="lh-100">
<h6 class="mb-0 text-white lh-100">{{ query }}的搜索结果:{{ page.paginator.count }}</h6>
</div>
</div>
{% for blog in page %}
<div class="list-group-item d-flex justify-content-between align-items-center">
<li class="blogtitle"><a href="{% url 'blog:blog' blog.object.blog_id %}" target="_blank">{% highlight blog.object.title with query %}</a></li>
<ul class="list-group">
<li>{{ blog.object.create_time }}by <a href="{% url 'blog:other_home' blog.object.author_name.user_id %}">{% highlight blog.object.author_name with query %}</a>
<span class="badge badge-primary badge-pill">{{ blog.object.look_times }}已阅读</span><span class="badge badge-primary badge-pill">{{ blog.object.love }}喜欢</span><span class="badge badge-primary badge-pill">{{ blog.comments_set.all|length }}评论</span></li>
</ul>
</div>
{% empty %}
<h4>还没有内容哟</h4>
{% endfor %}
</div>
<div class="pagelist">
{% if page.has_other_pages %}
<p>当前页码:{{ page.number }}/{{ page.paginator.num_pages }}
{% if page.has_previous %}
{% if page.previous_page_number > 1 %}
<a href="{{ request.path }}?q={{ query }}&page=1">首页</a>
{% endif %}
<a href="{{ request.path }}?q={{ query }}&page={{ page.previous_page_number }}">上一页:{{ page.previous_page_number }}</a>
{% endif %}
{% if page.has_next %}
<a href="{{ request.path }}?q={{ query }}&page={{ page.next_page_number }}">下一页:{{ page.next_page_number }}</a>
{% if page.next_page_number > 1 %}
<a href="{{ request.path }}?q={{ query }}&page={{ page.paginator.num_pages }}">尾页</a>
{% endif %}
{% endif %}
</p>
{% endif %}
</div>
</div>
</main>
{% endblock %}
目录结构
原博客