12

私は Django Haystack をしばらく使用していますが、これは素晴らしいです! 時々 (15 ~ 30 分) 更新する必要があるデータを含むかなり重いサイトがあります。

を使用するpython manage.py update_indexと、データの更新に時間がかかります。これをスピードアップする方法はありますか?または、可能であれば変更されたデータのみを更新することもできます..

現在、Solr をバックエンドとして Django Haystack 1.2.7 と Django 1.4 を使用しています。

ありがとう!!!


編集:

はい、ドキュメントのその部分を読んでみましたが、本当に必要なのはインデックス作成を高速化する方法です。すべてを更新するのではなく、最近のデータのみを更新する場合があります。見つけget_updated_fieldたけど使い方がわからない。ドキュメントでは、それが使用される理由のみが言及されていますが、実際の例は示されていません。


編集2:

start = DateTimeField(model_attr='start', null=True, faceted=True, --HERE?--)

編集3:

OK、以下のソリューションを実装しましたが、rebuild_index (45000 データ) を試したところ、コンピューターがほとんどクラッシュしました。10 分待った後、エラーが表示されました。

 File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/haystack/management/commands/rebuild_index.py", line 16, in handle
    call_command('update_index', **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 150, in call_command
    return klass.execute(*args, **defaults)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/haystack/management/commands/update_index.py", line 193, in handle
    return super(Command, self).handle(*apps, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 304, in handle
    app_output = self.handle_app(app, **options)
  File "/usr/local/lib/python2.7/dist-packages/haystack/management/commands/update_index.py", line 229, in handle_app
    do_update(index, qs, start, end, total, self.verbosity)
  File "/usr/local/lib/python2.7/dist-packages/haystack/management/commands/update_index.py", line 109, in do_update
    index.backend.update(index, current_qs)
  File "/usr/local/lib/python2.7/dist-packages/haystack/backends/solr_backend.py", line 73, in update
    self.conn.add(docs, commit=commit, boost=index.get_field_weights())
  File "/usr/local/lib/python2.7/dist-packages/pysolr.py", line 686, in add
    m = ET.tostring(message, encoding='utf-8')
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1127, in tostring
    ElementTree(element).write(file, encoding, method=method)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 821, in write
    serialize(write, self._root, encoding, qnames, namespaces)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 940, in _serialize_xml
    _serialize_xml(write, e, encoding, qnames, None)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 940, in _serialize_xml
    _serialize_xml(write, e, encoding, qnames, None)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 915, in _serialize_xml
    write("<" + tag)
MemoryError
4

1 に答える 1

21

get_updated_fieldモデルが更新された日付を含むモデルの属性の名前を含む文字列を返す必要があります ( haystack docs )。auto_now=True の DateField が理想的です ( Django docs )。

たとえば、私の UserProfile モデルには updated という名前のフィールドがあります

models.py

class UserProfile(models.Model):
    user = models.ForeignKey(User)
    # lots of other fields snipped
    updated = models.DateTimeField(auto_now=True)

search_indexes.py

class UserProfileIndex(SearchIndex):
    text = CharField(document=True, use_template=True)
    user = CharField(model_attr='user')
    user_fullname = CharField(model_attr='user__get_full_name')

    def get_model(self):
        return UserProfile

    def get_updated_field(self):
        return "updated"

次に、実行する./manage.py update_index --age=10と、過去 10 時間に更新されたユーザー プロファイルのみがインデックスに登録されます。

于 2012-12-11T18:55:25.310 に答える