gpt4 book ai didi

python - 如何制作一个抽象的 Haystack SearchIndex 类

转载 作者:太空狗 更新时间:2023-10-29 21:20:03 25 4
gpt4 key购买 nike

您如何制作一个抽象的 SearchIndex 类,类似于 Django 让您制作抽象基础模型的方式?

我有几个 SearchIndexes,我想提供相同的基本字段(object_id、时间戳、重要性等)。目前,我正在复制所有这些代码,所以我正在尝试创建一个“BaseIndex”,并让所有真正的索引类都继承自它。

我试过了:

class BaseIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
object_id = indexes.IntegerField()
timestamp = indexes.DateTimeField()

class Meta:
abstract = True

class PersonIndex(BaseIndex):
...other fields...

但这给了我错误:

NotImplementedError: You must provide a 'model' method for the '<myapp.search_indexes.BaseIndex object at 0x18a7328>' index.

然后我尝试了:

class BaseIndex(object):
text = indexes.CharField(document=True, use_template=True)
object_id = indexes.IntegerField()
timestamp = indexes.DateTimeField()

class PersonIndex(BaseIndex, indexes.SearchIndex, indexes.Indexable):
first_name = indexes.CharField()
middle_name = indexes.CharField()
last_name = indexes.CharField()

但是这些给我错误:

SearchFieldError: The index 'PersonIndex' must have one (and only one) SearchField with document=True.

如何从自定义 SearchIndex 子类继承?

最佳答案

只是不要将 indexes.Indexable 作为您不想编制索引的任何内容的父元素。

所以修改你的第一个例子。

class BaseIndex(indexes.SearchIndex):
text = indexes.CharField(document=True, use_template=True)
object_id = indexes.IntegerField()
timestamp = indexes.DateTimeField()

class Meta:
abstract = True

class PersonIndex(BaseIndex, indexes.Indexable):
...other fields...

关于python - 如何制作一个抽象的 Haystack SearchIndex 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16900067/

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