1.先安裝需要的包
pip install django-haystack
pip install whoosh
pip install jieba
2.配置需要的setting.py
添加haystack
INSTALLED_APPS = (
...
'haystack',
...
)
3.設(shè)置settings.py
# Whoosh示例
#需要設(shè)置PATH到你的Whoosh索引的文件系統(tǒng)位置
import os
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'pc.whoosh_cn_backend.WhooshEngine',
'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
},
}
# 自動(dòng)更新索引
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
HAYSTACK_SEARCH_RESULTS_PER_PAGE = 10
分頁 10
4.配置url 路徑
path('search/', include('haystack.urls')),
5.設(shè)置為那個(gè)class建立索引pc/search_indexes 每個(gè)索引里面必須有且只能有一個(gè)字段為document=True
from haystack import indexes
from pc.models import articlet
class ArticleIndex(indexes.SearchIndex, indexes.Indexable):
#類名必須為需要檢索的Model_name+Index,這里需要檢索Article诫舅,所以創(chuàng)建ArticleIndex
text = indexes.CharField(document=True, use_template=True)#創(chuàng)建一個(gè)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
復(fù)制該目錄下的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="請(qǐng)輸入搜索的內(nèi)容" 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>搜索結(jié)果</h3>
<table class="table">
<tr>
{% load highlight %}
{% for result in page.object_list %}
<!--{% highlight result.object.content with query %} 代碼高亮這樣寫 否則可以 這樣寫 -->
<td>
<p>
吸睛內(nèi)容: {% 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">內(nèi)容{% highlight result.object.content with query %}</p>
<p class="article_content">name{% highlight result.object.name with query %}</p>
</td>
</tr>
{% empty %}
<p>沒有得到想要的結(jié)果哦.</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.新建路徑 跳轉(zhuǎn)到搜索頁面
path('bases/', bases),
def bases(request):
return render(request,'bases.html')
11.設(shè)置完成,重新生成索引
python manage.py rebuild_index
12.運(yùn)行項(xiàng)目邑闲,訪問/base
python manage.py runserver 0.0.0.0:9000