1.先安装需要的包
pip install django-haystack
pip install whoosh
pip install jieba
2.配置需要的setting.py
添加haystack
INSTALLED_APPS = (
...
'haystack',
...
)
3.设置settings.py
# Whoosh示例
#需要设置PATH到你的Whoosh索引的文件系统位置
import os
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'pc.whoosh_cn_backend.WhooshEngine',
'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
},
}
# 自动更新索引
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
HAYSTACK_SEARCH_RESULTS_PER_PAGE = 10
分页 10
4.配置url 路径
path('search/', include('haystack.urls')),
5.设置为那个class建立索引pc/search_indexes 每个索引里面必须有且只能有一个字段为document=True
from haystack import indexes
from pc.models import articlet
class ArticleIndex(indexes.SearchIndex, indexes.Indexable):
#类名必须为需要检索的Model_name+Index,这里需要检索Article,所以创建ArticleIndex
text = indexes.CharField(document=True, use_template=True)#创建一个text字段
#其它字段
name = indexes.CharField(model_attr='name')
content = indexes.CharField(model_attr='content')
def get_model(self):#重载get_model方法,必须要有!
return articlet
def index_queryset(self, using=None):
return self.get_model().objects.all()
6.在templates/search/indexes/pc/ 新建 article_text.txt
{{ object.eye }}
{{ object.name }}
{{ object.content }}
7.在pc 下新建 whoosh_cn_backend.py
该文件 是由
\PY_ENV\dailyfresh_env\Lib\site-packages\haystack\backends
复制该目录下的whoosh_backend.py文件并重命名为whoosh_cn_backend.py
from jieba.analyse import ChineseAnalyzer
找到
analyzer=StemmingAnalyzer()
改为
analyzer=ChineseAnalyzer()
8.新建base.html 是搜索的页面
<form class="navbar-form navbar-left" action="{% url 'haystack_search' %}" method="get">
{% csrf_token %}
<div class="form-group">
<input type="text" class="form-control" name="q" placeholder="请输入搜索的内容" value=""/>
</div>
<input type="submit" value="搜索">
</form>
9.templates/search/indexes/pc/新建serarch.html
{#{% extends 'index.html' %}#}
{% block style %}
{% load static %}
<style>
span.highlighted{
color: red;
}
</style>
{% endblock %}
{% block jumbotron %}
{% endblock %}
{% block container %}
<div class="container" id="content" style="margin-top: 60px;">
<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>
{% load highlight %}
{% for result in page.object_list %}
<!--{% highlight result.object.content with query %} 代码高亮这样写 否则可以 这样写 -->
<td>
<p>
吸睛内容: {% highlight result.object.eye with query %}
{# <a href="{% url 'article:detail' article_id=result.object.id %}">#}
{# {% highlight result.object.title with query %}</a>#}
</p>
<p class="article_content">内容{% highlight result.object.content with query %}</p>
<p class="article_content">name{% highlight result.object.name with query %}</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 }}">{% endif %}« 上一页{% if page.has_previous %}</a>{% endif %}
|
{% if page.has_next %}<a href="?q={{ query }}&page={{ page.next_page_number }}">{% 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 %}
{% block script %}
<script type="text/javascript">
</script>
{% endblock %}
10.新建路径 跳转到搜索页面
path('bases/', bases),
def bases(request):
return render(request,'bases.html')
11.设置完成,重新生成索引
python manage.py rebuild_index
12.运行项目,访问/base
python manage.py runserver 0.0.0.0:9000