gpt4 book ai didi

python - 将提取的 PDF 内容与 django-haystack 集成

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:51 24 4
gpt4 key购买 nike

我已经使用 Solr 提取了 PDF/DOCX 内容,并且我已经成功地使用以下专用于此的 Solr URL 建立了一些搜索查询:

http://localhost:8983/solr/select?q=Lycee

我想用 django-haystack 建立这样的查询。我发现这个链接正在谈论这个问题:

https://github.com/toastdriven/django-haystack/blob/master/docs/rich_content_extraction.rst

但是 django-haystack (2.0.0-beta) 没有“FileIndex”类。我如何在 django-haystack 中集成这样的搜索?

最佳答案

文档中引用的“FileIndex”是 haystack.indexes.SearchIndex 的假设子类。这是一个例子:

from haystack import indexes
from myapp.models import MyFile

class FileIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
title = indexes.CharField(model_attr='title')
owner = indexes.CharField(model_attr='owner__name')


def get_model(self):
return MyFile

def index_queryset(self, using=None):
return self.get_model().objects.all()

def prepare(self, obj):
data = super(FileIndex, self).prepare(obj)

# This could also be a regular Python open() call, a StringIO instance
# or the result of opening a URL. Note that due to a library limitation
# file_obj must have a .name attribute even if you need to set one
# manually before calling extract_file_contents:
file_obj = obj.the_file.open()

extracted_data = self.backend.extract_file_contents(file_obj)

# Now we'll finally perform the template processing to render the
# text field with *all* of our metadata visible for templating:
t = loader.select_template(('search/indexes/myapp/myfile_text.txt', ))
data['text'] = t.render(Context({'object': obj,
'extracted': extracted_data}))

return data

因此 extracted_data 将替换为您提出的用于提取 PDF/DOCX 内容的任何过程。然后,您将更新您的模板以包含该数据。

关于python - 将提取的 PDF 内容与 django-haystack 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14036704/

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