首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将提取的PDF内容与django-haystack集成

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

Stack Overflow用户
提问于 2012-12-26 14:20:53
回答 1查看 896关注 0票数 4

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

代码语言:javascript
复制
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中集成这样的搜索?

EN

回答 1

Stack Overflow用户

发布于 2014-07-22 06:59:56

文档中引用的"FileIndex“是haystack.indexes.SearchIndex的假设子类。下面是一个示例:

代码语言:javascript
复制
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内容的任何过程所取代。然后,您将更新模板以包含该数据。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14036704

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档