From 634d484c500b90ffcf45f8874fec4f22bb0e7717 Mon Sep 17 00:00:00 2001 From: pvfneqs26 <3331425936@qq.com> Date: Sun, 19 Oct 2025 23:41:26 +0800 Subject: [PATCH 1/2] ADD file via upload --- search_indexes.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 search_indexes.py diff --git a/search_indexes.py b/search_indexes.py new file mode 100644 index 0000000..c69c292 --- /dev/null +++ b/search_indexes.py @@ -0,0 +1,24 @@ +from haystack import indexes +# 导入Haystack搜索索引模块 +# 导入博客文章模型 +from blog.models import Article + + +class ArticleIndex(indexes.SearchIndex, indexes.Indexable): + """文章搜索索引类,用于定义Elasticsearch/Solr等搜索引擎的索引结构""" + + # 定义主搜索字段,document=True表示这是主要的搜索文档字段 + # use_template=True表示使用模板文件来定义字段内容 + text = indexes.CharField(document=True, use_template=True) + + def get_model(self): + """返回该索引对应的Django模型类""" + return Article + + def index_queryset(self, using=None): + """ + 返回需要被索引的查询集 + using参数指定使用的搜索引擎后端 + """ + # 只索引状态为已发布('p')的文章 + return self.get_model().objects.filter(status='p') -- 2.34.1 From f88c9018b8aa159460d603efd32a8ea94d558113 Mon Sep 17 00:00:00 2001 From: pvfneqs26 <3331425936@qq.com> Date: Sun, 19 Oct 2025 23:47:23 +0800 Subject: [PATCH 2/2] ADD file via upload --- src/search_indexes.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/search_indexes.py diff --git a/src/search_indexes.py b/src/search_indexes.py new file mode 100644 index 0000000..c69c292 --- /dev/null +++ b/src/search_indexes.py @@ -0,0 +1,24 @@ +from haystack import indexes +# 导入Haystack搜索索引模块 +# 导入博客文章模型 +from blog.models import Article + + +class ArticleIndex(indexes.SearchIndex, indexes.Indexable): + """文章搜索索引类,用于定义Elasticsearch/Solr等搜索引擎的索引结构""" + + # 定义主搜索字段,document=True表示这是主要的搜索文档字段 + # use_template=True表示使用模板文件来定义字段内容 + text = indexes.CharField(document=True, use_template=True) + + def get_model(self): + """返回该索引对应的Django模型类""" + return Article + + def index_queryset(self, using=None): + """ + 返回需要被索引的查询集 + using参数指定使用的搜索引擎后端 + """ + # 只索引状态为已发布('p')的文章 + return self.get_model().objects.filter(status='p') -- 2.34.1