PS: 我的检索是在文章模块下 forum/article
第一步:先安装需要的包:
1
2
3
|
pip install django - haystack
pip install whoosh
pip install jieba
|
第二步: 配置需要的文件 settings.py
添加haystack应用模块
1
2
3
4
5
|
INSTALLED_APPS = (
...
'haystack',
...
)
|
在settings.py 末尾添加
1
2
3
4
5
6
7
|
HAYSTACK_CONNECTIONS = {
'default' : {
'ENGINE' : 'article.whoosh_cn_backend.WhooshEngine' ,
'PATH' : os.path.join(BASE_DIR, 'whoosh_index' ),
},
}
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor' # 索引自动更新
|
第三步: 配置url路径
在 forum/forum/urls.py中包含search路径
1
|
url(r '^search/' , include( 'haystack.urls' )),
|
第四步:建立模型
forum/article/models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class Article(models.Model):
'''
文章数据模型
'''
id = models.AutoField(primary_key = True ,verbose_name = '文章编号' )
title = models.CharField(max_length = 50 ,verbose_name = '文章标题' )
content = MDTextField()
publish_time = models.DateTimeField(auto_now_add = True ,verbose_name = '发表时间' )
last_update_time = models.DateTimeField(auto_now = True ,verbose_name = '最后一次修改时间' )
status = models.IntegerField(u '状态' , default = 1 )
read_count = models.IntegerField(default = 0 ,verbose_name = '阅读次数' )
comment_count = models.IntegerField(default = 0 ,verbose_name = '评论次数' )
#关联用户
user = models.ForeignKey(User,on_delete = models.CASCADE,verbose_name = '用户' )
subject = models.ForeignKey(Subject, null = True , blank = True , on_delete = models.CASCADE,
verbose_name = '属于哪一个专题' )
|
第五步:设置为那个class建立索引
如果你想针对某个app例如mainapp做全文检索,则必须在mainapp的目录下面建立search_indexes.py文件,文件名不能修改
例:forum/article/search_indexes.py模块
1
2
3
4
5
6
7
8
9
10
11
12
13
|
from article.models import Article
from haystack import indexes
class ArticleIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document = True , use_template = True )
def get_model( self ):
return Article
def index_queryset( self , using = None ):
return self .get_model().objects. filter (status = 1 )
|
每个索引里面必须有且只能有一个字段为document=True
第六步: 确定我们需要的属性:
新建yourapp/templates/search/indexes/yourapp/article_text.txt来指明需要属性
例: article/templates/search/indexes/yourapp/article_text.txt
1
2
3
|
{{ object.title }} # 文章标题
{{ object.user.username }} # 文章作者
{{ object.content }} # 文章内容
|
第七步: 把搜索引擎放入项目下
例: forum/article/whoosh_cn_backend.py
将文件whoosh_backend.py(例: 我的python路径E:\python\Lib\site-packages\haystack\backends\whoosh_backend.py
)放到article下,并重命名为whoosh_cn_backend.py,例如blog/whoosh_cn_backend.py。修改如下:
导入
1
|
from jieba.analyse import ChineseAnalyzer
|
找到
然后将其修改为
成功引入jieba分词
第八步: 前端页面配置
templates/base.html (搜索页面)
1
2
3
4
5
6
|
< form class = "navbar-form navbar-left" action = "/search/" method = "get" >
< div class = "form-group" >
< input type = "text" class = "form-control" name = "q" placeholder = "请输入搜索的内容" value = "" />
</ div >
< input type = "submit" value = "搜索" >
</ form >
|
结果展示页面(forum/article/templates/search/search.html)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
{% extends 'base.html' %}
{% block title %}搜索结果{% endblock %}
{% block content %}
< div class = "container" id = "content" >
< div class = "search-body" >
< form method = "get" action = "" >
< table >
< tr >
< td >
< label for = "id_q" ></ label >
</ td >
< td >
< input class = "form-control" id = "id_q" name = "q" type = "search" value = "{{ query }}" />
</ td >
< td > </ td >
< td >
< button class = "btn btn-defaul" type = "submit" >< span class = "glyphicon glyphicon-search" >搜索</ span ></ button >
</ td >
</ tr >
</ table >
</ form >
{% if query %}
< h3 >搜索结果</ h3 >
< table class = "table" >
< tr >
{% for result in page.object_list %}
< td >
< p >
< a href = "{% url 'article:article_detail' article_id=result.object.id %}" rel = "external nofollow" >
{{ result.object.title }}</ a >
</ p > 作者: < a href = "{% url 'user:user_index' result.object.user.id %}" rel = "external nofollow" >{{ result.object.user.username }}</ a >
< p >{{ result.object.content | safe | truncatechars_html:40 }}</ p >
</ td >
</ tr >
{% empty %}
< p >没有得到想要的结果哦.</ p >
{% endfor %}
</ table >
{% if page.has_previous or page.has_next %}
< div >
{% if page.has_previous %}< a href = "?q={{ query }}&page={{ page.previous_page_number }}" rel = "external nofollow" >{% endif %}« 上一页{% if page.has_previous %}</ a >{% endif %}
|
{% if page.has_next %}< a href = "?q={{ query }}&page={{ page.next_page_number }}" rel = "external nofollow" >{% endif %}下一页 »{% if page.has_next %}</ a >{% endif %}
</ div >
{% endif %}
{% else %}
{# Show some example queries to run, maybe query syntax, something else? #}
{% endif %}
</ div >
</ div >
{% endblock %}
|
第九步: 建立索引
1
|
python manage.py rebuild_index
|
建立成功会自行生成一个forum/whoosh_index的文件夹
第十步: 运行程序
1
|
python manage.py runserver
|
PS: 模板样式可以自行调整
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.jianshu.com/p/4f88d5f09ce3