yxy添加 search_indexes.py 注释

yxy_branch
严欣怡 4 months ago
parent 5fc3f6db58
commit f9d877179c

@ -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')
Loading…
Cancel
Save