|
|
|
|
@ -1,13 +1,32 @@
|
|
|
|
|
# 导入Haystack的索引模块,用于定义搜索索引
|
|
|
|
|
from haystack import indexes
|
|
|
|
|
|
|
|
|
|
# 导入博客文章模型,作为搜索索引的数据源
|
|
|
|
|
from blog.models import Article
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ArticleIndex(indexes.SearchIndex, indexes.Indexable):
|
|
|
|
|
"""
|
|
|
|
|
文章搜索索引类,用于配置Haystack搜索的索引规则
|
|
|
|
|
继承自Haystack的SearchIndex(搜索索引基类)和Indexable(可索引接口)
|
|
|
|
|
"""
|
|
|
|
|
# 定义主搜索字段:
|
|
|
|
|
# - document=True:标记为主要搜索字段(Haystack默认以此字段作为全文检索的基础)
|
|
|
|
|
# - use_template=True:指定使用模板来构建索引内容(模板通常存放于templates/search/indexes/[app名]/[模型名]_text.txt)
|
|
|
|
|
text = indexes.CharField(document=True, use_template=True)
|
|
|
|
|
|
|
|
|
|
def get_model(self):
|
|
|
|
|
"""
|
|
|
|
|
必须实现的方法:指定该索引对应的模型
|
|
|
|
|
返回值为需要被索引的Django模型类
|
|
|
|
|
"""
|
|
|
|
|
return Article
|
|
|
|
|
|
|
|
|
|
def index_queryset(self, using=None):
|
|
|
|
|
return self.get_model().objects.filter(status='p')
|
|
|
|
|
"""
|
|
|
|
|
定义需要被索引的数据集
|
|
|
|
|
筛选出状态为"已发布"(status='p')的文章,仅对这些文章建立搜索索引
|
|
|
|
|
:param using: 可选参数,指定搜索引擎(多引擎场景下使用)
|
|
|
|
|
:return: 查询集(QuerySet),包含需要被索引的模型实例
|
|
|
|
|
"""
|
|
|
|
|
return self.get_model().objects.filter(status='p')
|