2012-12-11 31 views
12

Tôi đã sử dụng Django Haystack một thời gian và thật tuyệt vời! Tôi có một trang web khá nặng với dữ liệu cần được cập nhật theo thời gian (15 đến 30 phút).Chỉ số cập nhật Django Haystack nhanh hơn

Khi sử dụng python manage.py update_index phải mất rất nhiều thời gian để cập nhật dữ liệu. Có cách nào để tăng tốc độ này không? Hoặc có thể chỉ cập nhật dữ liệu đã thay đổi nếu có thể ..

Tôi hiện đang sử dụng Django Haystack 1.2.7 với Solr làm phụ trợ và Django 1.4.

Xin cảm ơn !!!


EDIT:

Có, tôi đã cố gắng đọc một phần của tài liệu nhưng những gì tôi thực sự cần là một cách để tăng tốc việc lập chỉ mục lên. Có thể chỉ cập nhật dữ liệu gần đây thay vì cập nhật tất cả. Tôi đã tìm thấy get_updated_field nhưng không biết cách sử dụng nó. Trong tài liệu, nó chỉ được đề cập tại sao nó được sử dụng nhưng không có ví dụ thực tế nào được hiển thị.


EDIT 2:

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

EDIT 3:

Ok tôi đã thực hiện các giải pháp dưới đây nhưng khi tôi đã cố gắng rebuild_index (với 45000 dữ liệu) nó gần như bị rơi máy tính của tôi . Sau 10 phút chờ đợi một lỗi xuất hiện:

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 
+0

Bạn đã thử một số điều trong tài liệu Phương pháp hay nhất chưa? http://django-haystack.readthedocs.org/en/latest/best_practices.html#ref-best-practices – Spacedman

+0

Tôi đã không sử dụng phần phụ trợ solr nên tôi không thể giúp bạn ở đó, xin lỗi. –

Trả lời

19

get_updated_field nên trả về một chuỗi chứa tên của thuộc tính trên mô hình có chứa ngày mà các mô hình đã được cập nhật (haystack docs). Một DateField với auto_now = True sẽ là lý tưởng cho điều đó (Django docs).

Ví dụ, mô hình UserProfile tôi có một trường tên là cập nhật

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" 

Sau đó, khi tôi chạy ./manage.py update_index --age=10 nó chỉ lập chỉ mục hồ sơ người dùng được cập nhật trong 10 giờ qua.

+0

Tôi nên thêm auto_now = True trong search_indexes.py ở đâu? Tôi đã làm một ví dụ trong câu hỏi của tôi ở trên. Ngoài ra, nơi chính xác tôi nên thực hiện get_updated_field. Cảm ơn câu trả lời của bạn!! – dark4p

+0

Auto_now sẽ đi trên Model trong models.py, hàm get_updated_field sẽ đi trong lớp SearchIndex. –

+0

Tôi đã thêm một ví dụ. –

Các vấn đề liên quan