Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Celery task for indexing.

  • Loading branch information...
commit f3dbb8905adab108ddf4fd468a183afd76538279 1 parent 28a09aa
@davedash davedash authored
View
0  docs/celeryconfig.py
No changes.
View
2  docs/conf.py
@@ -4,6 +4,8 @@
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.insert(0, os.path.abspath('..'))
+sys.path.insert(0, os.path.abspath('.'))
+os.environ['DJANGO_SETTINGS_MODULE'] = 'fake_settings'
# -- General configuration -----------------------------------------------------
View
6 docs/django.rst
@@ -7,6 +7,12 @@ It would be terribly useful if a Django Model knew how to add and remove itself
from ElasticSearch.
This is where the :class:`elasticutils.models.SearchMixin` comes in.
+You can then utilize things such as :func:`~elasticutils.tasks.index_objects` to
+automatically index all new items.
+
.. autoclass:: elasticutils.models.SearchMixin
:members:
+.. automodule:: elasticutils.tasks
+
+ .. autofunction:: index_objects(model, ids=[...])
View
0  docs/fake_settings.py
No changes.
View
26 elasticutils/tasks.py
@@ -0,0 +1,26 @@
+import logging
+
+import elasticutils
+from celeryutils import task
+
+log = logging.getLogger('elasticutils')
+
+
+@task
+def index_objects(model, ids, **kw):
+ """Models can asynchronously update their ES index.
+
+ If a model extends SearchMixin, it can add a post_save hook like so::
+
+ @receiver(dbsignals.post_save, sender=MyModel)
+ def update_search_index(sender, instance, **kw):
+ from elasticutils import tasks
+ tasks.index_objects.delay(sender, [instance.id])
+
+ """
+ es = elasticutils.get_es()
+ log.info('Indexing objects %s-%s. [%s]' % (ids[0], ids[-1], len(ids)))
+ qs = model.filter(id__in=ids)
+ for item in qs:
+ model.index(item.fields(), bulk=True, id=item.id)
+ es.flush_bulk(forced=True)
View
6 requirements-extra.txt
@@ -1,4 +1,10 @@
# This includes extra items that you may not require.
# These are necessary, however, to build the documentation
+# needed if you use this with Django
django
+
+# needed if you want to use the async task
+-e git://github.com/mozilla/nuggets.git#egg=nuggets
+celery
+

0 comments on commit f3dbb89

Please sign in to comment.
Something went wrong with that request. Please try again.