先奉上官方网址:"https://django-haystack.readthedocs.io/en/master/tutorial.html"
ok 跟着我的节奏 一步一步来 步步都很关键
①:安装 django-haystack 模块
pip install django-haystack
②:想settings内将haystack注册一下
# 注册应用
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'news', # 注册news应用
'django_ckeditor_5', # 富文本
'user', # 注册user应用
'haystack', # 配置全文检索
'operate', # 操作类
]
③:在settings中配置whoosh的全文检索
# whoosh的全文检索配置
HAYSTACK_CONNECTIONS = {
'default': {
# 等会要=需要该为自己的目录文件 先不着急 接着往下走
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': BASE_DIR / 'whoosh_index',
},
}
④:你需要在让那个内容有全文检索的功能 就需要在那个app下创建 search_indexes.py文件
例如我这里需要让新闻news有全文检索的功能 我就在news文件夹下创建文件
文件内容:
from haystack import indexes
# 导入需要全文检索的模块
from .models import News
# 类名改为自己的模块名+Index
class NewsIndex(indexes.SearchIndex, indexes.Indexable):
# 搜索的主体 官方文档有作者什么的没必要 直留一个text就行 而且命名只能为text
text = indexes.CharField(document=True, use_template=True)
def get_model(self):
# 返回自己的模块名
return News
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
# 返回查询集所有内容
return self.get_model().objects.all()
⑤:需要在你的mysite/templates/创建一个search/创建一个indexes/创建一个news/创建一个文件news_text.txt 其中news改为你的app名字即可
注意:这步操作需要你的settings中
TEMPLATES的DIRS配置为全局使用
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates'], # 这里面默认为空
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
news_text.txt 的内容为你的搜索内容 这看你的app/models的字段名 以及你想要匹配的对象
以新闻为例
# 以新闻的标题匹配
{{ object.title }}
# 以新闻的正文匹配
{{ object.content }}
⑥:在你的mysite/urls.py中配置一个路由
path('search/', include('haystack.urls'))
⑦:配置搜索模板 mysite/templates/search/search.html
search.html的内容为 根据自己不同配置不同:
{% extends 'base.html' %}
{% block title %}
<title>新华网-搜索结果</title>
{% endblock %}
{% block main %}
<div class="container">
<div class="row">
<div class="clo-md-8 col-md-offset-2">
{% if query %}
<h3>搜索结果</h3>
<ul class="list-group">
{% for result in page.object_list %}
<li class="list-group-item">
<a href="{% url 'news:detail' result.object.id %}">
{{ result.object.title }}</a></li>
{% endfor %}
</ul>
{% endif %}
</div>
</div>
</div>
{% endblock %}
⑧:下面这一步就是官方文档中没有的步骤了 跟我来
讲返回的接种以中文的形式返回
pip install jieba
在环境变量中 haystack/backends/whoosh_backend.py 复制出来(如果有权限无法复制,去文件夹中复制) 复制到你要在哪一个应用中出现全文检索的目录下并且可以改一下文件名为:whoosh_cn_backend.py
有三处需要修改:直接复制去查找就好!
1.# from whoosh.analysis import StemmingAnalyzer
from jieba.analyse import ChineseAnalyzer
2.analyzer=field_class.analyzer or ChineseAnalyzer(),
3.sa = ChineseAnalyzer()
⑨:我们回到settings中对第三步的内容进行更改,更改为我们自己的目录
# whoosh的全文检索配置
HAYSTACK_CONNECTIONS = {
'default': {
# 我需要我的news app下拥有全文检索功能
'ENGINE': 'news.whoosh_cn_backend.WhooshEngine',
'PATH': BASE_DIR / 'whoosh_index',
},
}
⑩:在终端中输入:
python ./manage.py rebuild_index