prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Elasticsearch backend indexes draft updates of existing pages
  ### Issue Summary
  
  When saving a draft version of an existing live page the Elasticsearch backend reindexes the page with the draft content. The reindexed content will potentially cause frontend search results to include the page if the search query matches the draft content.
  
  I'm using the following search query in my view:
  
     search_results = Page.objects.live().search(search_query)
  
  New content that is saved as draft is not an issue since the live() filter excludes it.
  
  
  ### Steps to Reproduce
  
  1. Edit an indexed field of an existing published page
  2. Insert a unique term in the indexed field
  3. Click 'Save Draft'
  4. On the fontend search for the unique term.
  5. The editted page will be returned in the results
  
  I can see the value of having the draft content indexed on the Wagtail backend but since the frontend shares the same index, that can be a problem.
  
  ### Technical details
  
  * Python version: 3.5.2.
  * Django version: 1.10.4.
  * Wagtail version: 1.8.
  * Elasticsearch: 5
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `wagtail/wagtailsearch/signal_handlers.py`
  Content:
  ```
  1 from __future__ import absolute_import, unicode_literals
  2 
  3 from django.db.models.signals import post_delete, post_save
  4 
  5 from wagtail.wagtailsearch import index
  6 
  7 
  8 def post_save_signal_handler(instance, **kwargs):
  9     index.insert_or_update_object(instance)
  10 
  11 
  12 def post_delete_signal_handler(instance, **kwargs):
  13     index.remove_object(instance)
  14 
  15 
  16 def register_signal_handlers():
  17     # Loop through list and register signal handlers for each one
  18     for model in index.get_indexed_models():
  19         post_save.connect(post_save_signal_handler, sender=model)
  20         post_delete.connect(post_delete_signal_handler, sender=model)
  21
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/wagtail/wagtailsearch/signal_handlers.py b/wagtail/wagtailsearch/signal_handlers.py
  --- a/wagtail/wagtailsearch/signal_handlers.py
  +++ b/wagtail/wagtailsearch/signal_handlers.py
  @@ -5,7 +5,13 @@
   from wagtail.wagtailsearch import index
   
   
  -def post_save_signal_handler(instance, **kwargs):
  +def post_save_signal_handler(instance, update_fields=None, **kwargs):
  +    if update_fields is not None:
  +        # fetch a fresh copy of instance from the database to ensure
  +        # that we're not indexing any of the unsaved data contained in
  +        # the fields that were not passed in update_fields
  +        instance = type(instance).objects.get(pk=instance.pk)
  +
       index.insert_or_update_object(instance)
  
verification_info: {"golden_diff": "diff --git a/wagtail/wagtailsearch/signal_handlers.py b/wagtail/wagtailsearch/signal_handlers.py\n--- a/wagtail/wagtailsearch/signal_handlers.py\n+++ b/wagtail/wagtailsearch/signal_handlers.py\n@@ -5,7 +5,13 @@\n from wagtail.wagtailsearch import index\n \n \n-def post_save_signal_handler(instance, **kwargs):\n+def post_save_signal_handler(instance, update_fields=None, **kwargs):\n+    if update_fields is not None:\n+        # fetch a fresh copy of instance from the database to ensure\n+        # that we're not indexing any of the unsaved data contained in\n+        # the fields that were not passed in update_fields\n+        instance = type(instance).objects.get(pk=instance.pk)\n+\n     index.insert_or_update_object(instance)\n", "issue": "Elasticsearch backend indexes draft updates of existing pages\n### Issue Summary\r\n\r\nWhen saving a draft version of an existing live page the Elasticsearch backend reindexes the page with the draft content. The reindexed content will potentially cause frontend search results to include the page if the search query matches the draft content.\r\n\r\nI'm using the following search query in my view:\r\n\r\n   search_results = Page.objects.live().search(search_query)\r\n\r\nNew content that is saved as draft is not an issue since the live() filter excludes it.\r\n\r\n\r\n### Steps to Reproduce\r\n\r\n1. Edit an indexed field of an existing published page\r\n2. Insert a unique term in the indexed field\r\n3. Click 'Save Draft'\r\n4. On the fontend search for the unique term.\r\n5. The editted page will be returned in the results\r\n\r\nI can see the value of having the draft content indexed on the Wagtail backend but since the frontend shares the same index, that can be a problem.\r\n\r\n### Technical details\r\n\r\n* Python version: 3.5.2.\r\n* Django version: 1.10.4.\r\n* Wagtail version: 1.8.\r\n* Elasticsearch: 5\r\n\n", "before_files": [{"content": "from __future__ import absolute_import, unicode_literals\n\nfrom django.db.models.signals import post_delete, post_save\n\nfrom wagtail.wagtailsearch import index\n\n\ndef post_save_signal_handler(instance, **kwargs):\n    index.insert_or_update_object(instance)\n\n\ndef post_delete_signal_handler(instance, **kwargs):\n    index.remove_object(instance)\n\n\ndef register_signal_handlers():\n    # Loop through list and register signal handlers for each one\n    for model in index.get_indexed_models():\n        post_save.connect(post_save_signal_handler, sender=model)\n        post_delete.connect(post_delete_signal_handler, sender=model)\n", "path": "wagtail/wagtailsearch/signal_handlers.py"}], "after_files": [{"content": "from __future__ import absolute_import, unicode_literals\n\nfrom django.db.models.signals import post_delete, post_save\n\nfrom wagtail.wagtailsearch import index\n\n\ndef post_save_signal_handler(instance, update_fields=None, **kwargs):\n    if update_fields is not None:\n        # fetch a fresh copy of instance from the database to ensure\n        # that we're not indexing any of the unsaved data contained in\n        # the fields that were not passed in update_fields\n        instance = type(instance).objects.get(pk=instance.pk)\n\n    index.insert_or_update_object(instance)\n\n\ndef post_delete_signal_handler(instance, **kwargs):\n    index.remove_object(instance)\n\n\ndef register_signal_handlers():\n    # Loop through list and register signal handlers for each one\n    for model in index.get_indexed_models():\n        post_save.connect(post_save_signal_handler, sender=model)\n        post_delete.connect(post_delete_signal_handler, sender=model)\n", "path": "wagtail/wagtailsearch/signal_handlers.py"}]}