prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Elasticsearch 1.7.0 breaks Pootle if ES not set up
  With `elasticsearch==1.7.0` doing `es.ping()` on a not-setup server raises `ConnectionError`.
  
  This breaks the default install here:
  
  https://github.com/translate/pootle/blob/master/pootle/core/search/backends/elasticsearch.py#L29
  
  Elasticsearch 1.7.0 breaks Pootle if ES not set up
  With `elasticsearch==1.7.0` doing `es.ping()` on a not-setup server raises `ConnectionError`.
  
  This breaks the default install here:
  
  https://github.com/translate/pootle/blob/master/pootle/core/search/backends/elasticsearch.py#L29
  
  
  </issue>
  <code>
  [start of pootle/core/search/backends/elasticsearch.py]
  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 #
  4 # Copyright (C) Pootle contributors.
  5 #
  6 # This file is a part of the Pootle project. It is distributed under the GPL3
  7 # or later license. See the LICENSE file for a copy of the license and the
  8 # AUTHORS file for copyright and authorship information.
  9 
  10 from __future__ import absolute_import
  11 
  12 __all__ = ('ElasticSearchBackend',)
  13 
  14 try:
  15     from elasticsearch import Elasticsearch
  16 except:
  17     Elasticsearch = None
  18 
  19 from ..base import SearchBackend
  20 
  21 
  22 class ElasticSearchBackend(SearchBackend):
  23     def __init__(self, config_name):
  24         super(ElasticSearchBackend, self).__init__(config_name)
  25         self._es = self._get_es_server()
  26         self._create_index_if_missing()
  27 
  28     def _server_setup_and_alive(self):
  29         return self._es is not None and self._es.ping()
  30 
  31     def _get_es_server(self):
  32         if self._settings is None or Elasticsearch is None:
  33             return None
  34         return Elasticsearch([
  35             {'host': self._settings['HOST'],
  36              'port': self._settings['PORT']},
  37         ])
  38 
  39     def _create_index_if_missing(self):
  40         if self._server_setup_and_alive():
  41             if not self._es.indices.exists(self._settings['INDEX_NAME']):
  42                 self._es.indices.create(self._settings['INDEX_NAME'])
  43 
  44     def _is_valuable_hit(self, unit, hit):
  45         return str(unit.id) != hit['_id']
  46 
  47     def search(self, unit):
  48         if not self._server_setup_and_alive():
  49             return []
  50 
  51         counter = {}
  52         res = []
  53         language = unit.store.translation_project.language.code
  54         es_res = self._es.search(
  55             index=self._settings['INDEX_NAME'],
  56             doc_type=language,
  57             body={
  58                 "query": {
  59                     "match": {
  60                         "source": {
  61                             "query": unit.source,
  62                             "fuzziness": self._settings['MIN_SCORE'],
  63                         }
  64                     }
  65                 }
  66             }
  67         )
  68 
  69         for hit in es_res['hits']['hits']:
  70             if self._is_valuable_hit(unit, hit):
  71                 translation_pair = hit['_source']['source'] + hit['_source']['target']
  72                 if translation_pair not in counter:
  73                     counter[translation_pair] = 1
  74                     res.append({
  75                         'unit_id': hit['_id'],
  76                         'source': hit['_source']['source'],
  77                         'target': hit['_source']['target'],
  78                         'project': hit['_source']['project'],
  79                         'path': hit['_source']['path'],
  80                         'username': hit['_source']['username'],
  81                         'fullname': hit['_source']['fullname'],
  82                         'email_md5': hit['_source']['email_md5'],
  83                     })
  84                 else:
  85                     counter[translation_pair] += 1
  86 
  87         for item in res:
  88             item['count'] = counter[item['source']+item['target']]
  89 
  90         return res
  91 
  92     def update(self, language, obj):
  93         if self._server_setup_and_alive():
  94             self._es.index(
  95                 index=self._settings['INDEX_NAME'],
  96                 doc_type=language,
  97                 body=obj,
  98                 id=obj['id']
  99             )
  100
  [end of pootle/core/search/backends/elasticsearch.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/pootle/core/search/backends/elasticsearch.py b/pootle/core/search/backends/elasticsearch.py
  --- a/pootle/core/search/backends/elasticsearch.py
  +++ b/pootle/core/search/backends/elasticsearch.py
  @@ -13,6 +13,7 @@
   
   try:
       from elasticsearch import Elasticsearch
  +    from elasticsearch.exceptions import ConnectionError
   except:
       Elasticsearch = None
   
  @@ -26,7 +27,12 @@
           self._create_index_if_missing()
   
       def _server_setup_and_alive(self):
  -        return self._es is not None and self._es.ping()
  +        if self._es is None:
  +            return False
  +        try:
  +            return self._es.ping()
  +        except ConnectionError:
  +            return False
   
       def _get_es_server(self):
           if self._settings is None or Elasticsearch is None:
  
verification_info: {"golden_diff": "diff --git a/pootle/core/search/backends/elasticsearch.py b/pootle/core/search/backends/elasticsearch.py\n--- a/pootle/core/search/backends/elasticsearch.py\n+++ b/pootle/core/search/backends/elasticsearch.py\n@@ -13,6 +13,7 @@\n \n try:\n     from elasticsearch import Elasticsearch\n+    from elasticsearch.exceptions import ConnectionError\n except:\n     Elasticsearch = None\n \n@@ -26,7 +27,12 @@\n         self._create_index_if_missing()\n \n     def _server_setup_and_alive(self):\n-        return self._es is not None and self._es.ping()\n+        if self._es is None:\n+            return False\n+        try:\n+            return self._es.ping()\n+        except ConnectionError:\n+            return False\n \n     def _get_es_server(self):\n         if self._settings is None or Elasticsearch is None:\n", "issue": "Elasticsearch 1.7.0 breaks Pootle if ES not set up\nWith `elasticsearch==1.7.0` doing `es.ping()` on a not-setup server raises `ConnectionError`.\n\nThis breaks the default install here:\n\nhttps://github.com/translate/pootle/blob/master/pootle/core/search/backends/elasticsearch.py#L29\n\nElasticsearch 1.7.0 breaks Pootle if ES not set up\nWith `elasticsearch==1.7.0` doing `es.ping()` on a not-setup server raises `ConnectionError`.\n\nThis breaks the default install here:\n\nhttps://github.com/translate/pootle/blob/master/pootle/core/search/backends/elasticsearch.py#L29\n\n", "before_files": [{"content": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n#\n# Copyright (C) Pootle contributors.\n#\n# This file is a part of the Pootle project. It is distributed under the GPL3\n# or later license. See the LICENSE file for a copy of the license and the\n# AUTHORS file for copyright and authorship information.\n\nfrom __future__ import absolute_import\n\n__all__ = ('ElasticSearchBackend',)\n\ntry:\n    from elasticsearch import Elasticsearch\nexcept:\n    Elasticsearch = None\n\nfrom ..base import SearchBackend\n\n\nclass ElasticSearchBackend(SearchBackend):\n    def __init__(self, config_name):\n        super(ElasticSearchBackend, self).__init__(config_name)\n        self._es = self._get_es_server()\n        self._create_index_if_missing()\n\n    def _server_setup_and_alive(self):\n        return self._es is not None and self._es.ping()\n\n    def _get_es_server(self):\n        if self._settings is None or Elasticsearch is None:\n            return None\n        return Elasticsearch([\n            {'host': self._settings['HOST'],\n             'port': self._settings['PORT']},\n        ])\n\n    def _create_index_if_missing(self):\n        if self._server_setup_and_alive():\n            if not self._es.indices.exists(self._settings['INDEX_NAME']):\n                self._es.indices.create(self._settings['INDEX_NAME'])\n\n    def _is_valuable_hit(self, unit, hit):\n        return str(unit.id) != hit['_id']\n\n    def search(self, unit):\n        if not self._server_setup_and_alive():\n            return []\n\n        counter = {}\n        res = []\n        language = unit.store.translation_project.language.code\n        es_res = self._es.search(\n            index=self._settings['INDEX_NAME'],\n            doc_type=language,\n            body={\n                \"query\": {\n                    \"match\": {\n                        \"source\": {\n                            \"query\": unit.source,\n                            \"fuzziness\": self._settings['MIN_SCORE'],\n                        }\n                    }\n                }\n            }\n        )\n\n        for hit in es_res['hits']['hits']:\n            if self._is_valuable_hit(unit, hit):\n                translation_pair = hit['_source']['source'] + hit['_source']['target']\n                if translation_pair not in counter:\n                    counter[translation_pair] = 1\n                    res.append({\n                        'unit_id': hit['_id'],\n                        'source': hit['_source']['source'],\n                        'target': hit['_source']['target'],\n                        'project': hit['_source']['project'],\n                        'path': hit['_source']['path'],\n                        'username': hit['_source']['username'],\n                        'fullname': hit['_source']['fullname'],\n                        'email_md5': hit['_source']['email_md5'],\n                    })\n                else:\n                    counter[translation_pair] += 1\n\n        for item in res:\n            item['count'] = counter[item['source']+item['target']]\n\n        return res\n\n    def update(self, language, obj):\n        if self._server_setup_and_alive():\n            self._es.index(\n                index=self._settings['INDEX_NAME'],\n                doc_type=language,\n                body=obj,\n                id=obj['id']\n            )\n", "path": "pootle/core/search/backends/elasticsearch.py"}]}