Skip to content

Commit

Permalink
Allow access to original ES response object (#35)
Browse files Browse the repository at this point in the history
The original HTTP response object from ES is now available as `SearchQuery.response` after the `execute` method is called. The full response is not stored, however - it is ephemeral.
  • Loading branch information
tim-mccurrach authored and hugorodgerbrown committed Jan 17, 2019
1 parent 9d663ee commit 681192a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ If the setting ``auto_sync`` is True, then on ``AppConfig.ready`` each model con

There is a **VERY IMPORTANT** caveat to the signal handling. It will **only** pick up on changes to the model itself, and not on related (``ForeignKey``, ``ManyToManyField``) model changes. If the search document is affected by such a change then you will need to implement additional signal handling yourself.

In addition to ``object.save()``, SeachDocumentMixin also provides the ``update_search_index(self, action, index='_all', update_fields=None, force=False)`` method. Action should be 'index', 'update' or 'delete'. The difference between 'index' and 'update' is that 'update' is a partial update that only changes the fields specified, rather than re-updating the entire document. If ``action`` is 'update' whilst ``update_fields`` is None, action will be changed to ``index``.
In addition to ``object.save()``, SeachDocumentMixin also provides the ``update_search_index(self, action, index='_all', update_fields=None, force=False)`` method. Action should be 'index', 'update' or 'delete'. The difference between 'index' and 'update' is that 'update' is a partial update that only changes the fields specified, rather than re-updating the entire document. If ``action`` is 'update' whilst ``update_fields`` is None, action will be changed to ``index``.

We now have documents in our search index, kept up to date with their Django counterparts. We are ready to start querying ES.

Expand Down Expand Up @@ -183,6 +183,9 @@ The ``elasticsearch_django.models.SearchQuery`` model wraps this functionality u
# run a default match_all query
search = Search(using=get_client(), index='blog')
sq = execute_search(search)
# the raw response is stored on the return object,
# but is not stored on the object in the database.
print(sq.response)
Calling the ``execute_search`` function will execute the underlying search, log the query JSON, the number of hits, and the list of hit meta information for future analysis. The ``execute`` method also includes these additional kwargs:

Expand Down
1 change: 1 addition & 0 deletions elasticsearch_django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,5 @@ def execute_search(search, search_terms='', user=None, reference=None, save=True
executed_at=tz_now(),
duration=duration
)
log.response = response
return log.save() if save else log
1 change: 1 addition & 0 deletions elasticsearch_django/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ def test_execute(self, mock_save, mock_execute, mock_now):
self.assertEqual(sq.reference, '')
self.assertTrue(sq.duration > 0)
self.assertEqual(sq.executed_at, mock_now.return_value)
self.assertEqual(sq.response, mock_execute.return_value)
mock_save.assert_called_once_with()

# try without saving
Expand Down

0 comments on commit 681192a

Please sign in to comment.