【问题标题】:Elasticsearch Indexing in Django Celery TaskDjango Celery 任务中的 Elasticsearch 索引
【发布时间】:2020-07-31 14:01:17
【问题描述】:

我正在构建一个 Django Web 应用程序来存储文档及其相关元数据。

大部分元数据将存储在底层 MySQL 数据库中,在 Elasticsearch 中索引 OCR 的文档文本以启用全文搜索。我已经合并了django-elasticsearch-dsl 来连接和同步我的数据模型,因为我还在索引(因此,双重存储)我模型中的其他一些字段。我曾考虑使用Haystack,但它缺乏对最新 Elasticsearch 版本的支持。

当通过应用程序的管理界面上传文档时,post_save 信号会自动触发 Celery 异步后台任务以执行 OCR,并最终将提取的文本索引到 Elasticsearch 中。

看到我的模型中没有定义全文字段(并希望避免这样做,因为我不想在数据库中存储或搜索 CLOB),我正在寻找最好的从我的 tasks.py 文件中更新我的 Elasticsearch 文档的练习。似乎没有办法使用 django-elasticseach-dsl 做到这一点(但也许我错了?)所以我想知道我是否应该这样做:

  1. 尝试使用姐妹django-elasticsearch-dsl-drf 包通过REST 与Elasticsearch 交互。

  2. 通过使用更普通的 elasticsearch-dsl-py 包(基于 elasticsearch-py)更松散地将我的应用程序与 Elasticsearch 集成。使用这种方法我会失去一些“奢侈”,因为我必须编写更多的集成代码,至少如果我想用信号连接我的模型。

有最佳实践吗?还是我没有考虑过的其他方法?

更新 1: 在尝试实现@Nielk 的答案时,我能够将 OCR 文本(下面的 tasks.py 中的结果 =“test”)持久化到 ElasticSearch 中,但它也保留在 MySQL 数据库中。我仍然对如何将 Submission.rawtext 配置为 ElasticSearch 的通路感到困惑。

models.py:

class Submission(models.Model):

  rawtext = models.TextField(null=True, blank=True)
  ...
  def type_to_string(self):
    return ""

documents.py:

@registry.register_document
class SubmissionDocument(Document)

  rawtext = fields.TextField(attr="type_to_string")

  def prepare_rawtext(self, instance):
    # self.rawtext = None
    # instance.rawtext = "test"

    return instance.rawtext

  ... 

tasks.py(在提交模型 post_save 信号上调用):

  @shared_task
  def process_ocr(my_uuid)

    result = "test" # will ultimately be OCR'd text

    instance = Submission.objects.get(my_uuid=my_uuid)
    instance.rawtext = result
    instance.save()

更新 2(工作解决方案):

models.py 类提交(models.Model):

   @property
   def rawtext(self):
      if getattr(self, '_rawtext_local_change', False):
         return self._rawtext
      if not self.pk:
         return None
      from .documents import SubmissionDocument
      try:
         return SubmissionDocument.get(id=self.pk)._rawtext
      except:
         return None

   @rawtext.setter
   def rawtext(self, value):
      self._rawtext_local_change = True
      self._rawtext = value

documents.py

   @registry.register_document
   class SubmissionDocument(Document):

      rawtext = fields.TextField()

      def prepare_rawtext(self, instance):
         return instance.rawtext

tasks.py

   @shared_task
   def process_ocr(my_uuid)

      result = "test" # will ultimately be OCR'd text

      # note that you must do a save on property fields, can't do an update
      instance = Submission.objects.get(my_uuid=my_uuid)
      instance.rawtext = result
      instance.save()

【问题讨论】:

    标签: django elasticsearch celery django-celery elasticsearch-dsl-py


    【解决方案1】:

    您可以在链接到您的模型的文档定义中添加额外字段(请参阅文档https://django-elasticsearch-dsl.readthedocs.io/en/latest/fields.html#using-different-attributes-for-model-fields 中的字段“type_to_field”,如果创建实例,则将其与“prepare_xxx”方法结合使用以初始化为空字符串, 并在更新的情况下为其当前值) 这能解决你的问题吗?

    编辑 1 - 这就是我的意思:

    models.py

    class Submission(models.Model):
        @property
        def rawtext(self):
            if getattr(self, '_rawtext_local_change ', False):
                return self._rawtext
            if not self.pk:
                return None
            from .documents import SubmissionDocument
            return SubmissionDocument.get(meta__id=self.pk).rawtext
    
        @property.setter
        def rawtext(self, value):
            self._rawtext_local_change = True
            self._rawtext = value
    

    编辑 2 - 修正代码拼写错误

    【讨论】:

    • 当您说“链接到您的模型”时,这是否意味着该字段值将保留在 Elasticsearch 中而不是 MySQL 数据库中?还是最终也会存储到数据库中?
    • 它只会在 elasticsearch 中持续存在,是的。 django elasticsearch dsl 旨在根据您的 django 模型具有文档结构,而不是相反。
    • 完美。最后一个问题:然后我可以将我的模型实例导入到我的 tasks.py 文件中并执行 instance.raw_text = “my ocr text”吗?
    • 如果 'raw_text' 也是索引字段的名称,那应该可以工作。我认为一个不错的解决方案是在模型类中添加一个“raw_text”属性,从弹性搜索中获取数据。根据您的数据访问模式,您可以使用单个 multiget 调用优化和初始化多个实例的字段。
    • 我尝试实施您的建议已经暴露了我的想法中的一些漏洞。请参阅上面的“更新 1”帖子(带有代码)。我能够从 tasks.py 中索引 OCR 文本,但它仍然存在于数据库中。如何确保不会发生这种情况,以及如何设置模型以从 Elasticsearch 获取最新值?
    猜你喜欢
    • 2019-11-08
    • 1970-01-01
    • 2011-06-19
    • 2012-09-06
    • 1970-01-01
    • 2011-07-18
    • 2011-07-17
    • 1970-01-01
    • 2013-09-30
    相关资源
    最近更新 更多