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

4
我使用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中集成这样的搜索?
1个回答

1
文档中提到的“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内容的任何流程。然后,您需要更新您的模板以包含该数据。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接