gpt4 book ai didi

python - Haystack 使用反向关系索引对象

转载 作者:太空宇宙 更新时间:2023-11-03 16:17:53 25 4
gpt4 key购买 nike

我有两个模型:

class Question(models.Model):
question_text = models.TextField()

class Response(models.Model):
response_text = models.TextField()
question = models.ForeignKey(Question, related_name='responses', on_delete=models.CASCADE)

我有一个干草堆搜索索引来索引所有问题question_text:

class QuestionIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
question_text = indexes.CharField(model_attr='question_text')
def get_model(self):
return Question

如何为所有 response_text 建立索引,以便在搜索 Question 时获得与 question_text 匹配的所有问题以及所有具有与 response_text 匹配的答案的问题?我想要这样的东西:

class QuestionIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
question_text = indexes.CharField(model_attr='question_text')
response_text = indexes.CharField(model_attr='responses__response_text')
def get_model(self):
return Question

终极问题:如何使用此 QuestionIndex 类对所有 response_text 建立索引?

最佳答案

您可以提供 prepare_ method让该字段指定索引哪些数据:

class QuestionIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
question_text = indexes.CharField(model_attr='question_text')
response_text = indexes.CharField()

def prepare_response_text(self, obj):
return ', '.join([r.response_text for r in obj.responses.all()])

关于python - Haystack 使用反向关系索引对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38758617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com