pull/29/head
djq 4 months ago
parent e07788b54c
commit 5d4dee14be

@ -2,11 +2,7 @@
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="jdk" jdkName="Python 3.12" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PyDocumentationSettings">
<option name="format" value="PLAIN" />
<option name="myDocStringFormat" value="Plain" />
</component>
</module>

@ -1,4 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.13" project-jdk-type="Python SDK" />
<component name="Black">
<option name="sdkName" value="Python 3.12" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12" project-jdk-type="Python SDK" />
</project>

@ -1,13 +1,26 @@
# 从haystack框架导入索引相关的模块用于实现全文搜索功能
from haystack import indexes
# 导入当前项目中blog应用的Article模型该模型对应需要被索引的文章数据
from blog.models import Article
# 定义ArticleIndex类继承自SearchIndex和Indexable用于配置Article模型的搜索索引
# SearchIndex提供索引的核心功能定义了如何从模型中提取数据构建索引
# Indexable标识该类可被索引要求实现get_model方法来指定关联的模型
class ArticleIndex(indexes.SearchIndex, indexes.Indexable):
# 定义一个text字段作为文档的主要索引字段document=True表示这是主要搜索字段
# use_template=True表示使用模板来定义该字段需要索引的内容通常在templates/search/indexes/blog/article_text.txt中配置
# 该字段会聚合模型中需要被搜索的字段(如标题、正文等),作为全文搜索的基础
text = indexes.CharField(document=True, use_template=True)
# 实现Indexable接口的方法返回当前索引关联的模型类
# 作用告诉haystack该索引对应的数据来自哪个模型
def get_model(self):
return Article
# 定义需要被索引的查询集(即哪些数据会被纳入搜索范围)
# using参数用于指定搜索引擎多引擎场景下使用默认None表示使用默认引擎
# 这里返回状态为'p'(假设表示"已发布")的文章,确保只有已发布的内容可被搜索
def index_queryset(self, using=None):
return self.get_model().objects.filter(status='p')
return self.get_model().objects.filter(status='p')
Loading…
Cancel
Save