prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Publisher search endpoint solr performance
  <!-- What problem are we solving? What does the experience look like today? What are the symptoms? -->
  
  Our /search/publishers endpoint is doing a strange roll-up and submitting many solr select queries causing performance issues. Solution presumably is to not make more than 1 solr query on /search/publishers.
  
  ### Proposal
  
  Change the backend call for /search/publishers to make a single query to solr `publisher:(...)` query.
  
  ### Evidence / Screenshot (if possible)
  ![image](https://user-images.githubusercontent.com/978325/227324438-3fe82d6b-c8b3-4f11-bb51-855898bb62a5.png)
  <img width="775" alt="Screenshot 2023-03-23 at 12 18 55 PM" src="https://user-images.githubusercontent.com/978325/227324919-d19b91c5-d19b-4746-9908-43e0f7cf1cbd.png">
  
  ### Relevant url?
  <!-- `https://openlibrary.org/...` -->
  
  http://testing.openlibrary.org/search/publishers?q=Black%20Dolls%20And%20White%20Dolls%20From%201940%20Through%201970%3A%20Their%20Impact%20Then%20On%20Black%20And%20White%20Children%27s%20Development%20
  
  ### Related files
  <!-- Files related to this issue; this is super useful for new contributors who might want to help! If you're not sure, leave this blank; a maintainer will add them. -->
  
  https://github.com/internetarchive/openlibrary/blob/b897c8c51a79308e38f9825fac82864a5cc7d3ae/openlibrary/plugins/worksearch/publishers.py#L82
  
  ### Stakeholders
  <!-- @ tag stakeholders of this bug -->
  
  
  </issue>
  <code>
  [start of openlibrary/plugins/worksearch/publishers.py]
  1 """Publisher pages
  2 """
  3 from infogami.utils import delegate, stats
  4 from infogami.utils.view import render_template, safeint
  5 import web
  6 import logging
  7 
  8 from . import subjects
  9 from . import search
  10 
  11 logger = logging.getLogger("openlibrary.worksearch")
  12 
  13 
  14 class publishers(subjects.subjects):
  15     path = '(/publishers/[^/]+)'
  16 
  17     def GET(self, key):
  18         key = key.replace("_", " ")
  19         page = subjects.get_subject(key, details=True)
  20 
  21         if not page or page.work_count == 0:
  22             web.ctx.status = "404 Not Found"
  23             return render_template('publishers/notfound.tmpl', key)
  24 
  25         return render_template("publishers/view", page)
  26 
  27     def is_enabled(self):
  28         return "publishers" in web.ctx.features
  29 
  30 
  31 class publishers_json(subjects.subjects_json):
  32     path = '(/publishers/[^/]+)'
  33     encoding = "json"
  34 
  35     def is_enabled(self):
  36         return "publishers" in web.ctx.features
  37 
  38     def normalize_key(self, key):
  39         return key
  40 
  41     def process_key(self, key):
  42         return key.replace("_", " ")
  43 
  44 
  45 class index(delegate.page):
  46     path = "/publishers"
  47 
  48     def GET(self):
  49         return render_template("publishers/index")
  50 
  51     def is_enabled(self):
  52         return "publishers" in web.ctx.features
  53 
  54 
  55 class publisher_search(delegate.page):
  56     path = '/search/publishers'
  57 
  58     def GET(self):
  59         i = web.input(q="")
  60         solr = search.get_solr()
  61         q = {"publisher": i.q}
  62 
  63         result = solr.select(
  64             q,
  65             facets=["publisher_facet"],
  66             fields=["publisher", "publisher_facet"],
  67             rows=0,
  68         )
  69         result = self.process_result(result)
  70         return render_template('search/publishers', i.q, result)
  71 
  72     def process_result(self, result):
  73         solr = search.get_solr()
  74 
  75         def process(p):
  76             return web.storage(
  77                 name=p.value,
  78                 key="/publishers/" + p.value.replace(" ", "_"),
  79                 count=solr.select({"publisher_facet": p.value}, rows=0)['num_found'],
  80             )
  81 
  82         publisher_facets = result['facets']['publisher_facet'][:25]
  83         return [process(p) for p in publisher_facets]
  84 
  85 
  86 class PublisherEngine(subjects.SubjectEngine):
  87     def normalize_key(self, key):
  88         return key
  89 
  90     def get_ebook_count(self, name, value, publish_year):
  91         # Query solr for this publish_year and publish_year combination and read the has_fulltext=true facet
  92         solr = search.get_solr()
  93         q = {"publisher_facet": value}
  94 
  95         if isinstance(publish_year, list):
  96             q['publish_year'] = tuple(publish_year)  # range
  97         elif publish_year:
  98             q['publish_year'] = publish_year
  99 
  100         result = solr.select(q, facets=["has_fulltext"], rows=0)
  101         counts = {v.value: v.count for v in result["facets"]["has_fulltext"]}
  102         return counts.get('true')
  103 
  104 
  105 def setup():
  106     subjects.SUBJECTS.append(
  107         subjects.SubjectMeta(
  108             name="publisher",
  109             key="publishers",
  110             prefix="/publishers/",
  111             facet="publisher_facet",
  112             facet_key="publisher_facet",
  113             Engine=PublisherEngine,
  114         )
  115     )
  116
  [end of openlibrary/plugins/worksearch/publishers.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/openlibrary/plugins/worksearch/publishers.py b/openlibrary/plugins/worksearch/publishers.py
  --- a/openlibrary/plugins/worksearch/publishers.py
  +++ b/openlibrary/plugins/worksearch/publishers.py
  @@ -57,30 +57,28 @@
   
       def GET(self):
           i = web.input(q="")
  -        solr = search.get_solr()
  -        q = {"publisher": i.q}
  -
  -        result = solr.select(
  -            q,
  +        result = search.get_solr().select(
  +            {"publisher": i.q, "type": "work"},
               facets=["publisher_facet"],
  -            fields=["publisher", "publisher_facet"],
  +            facet_mincount=1,
  +            facet_limit=25,
  +            facet_contains=i.q,
  +            facet_contains_ignoreCase='true',
               rows=0,
           )
           result = self.process_result(result)
           return render_template('search/publishers', i.q, result)
   
       def process_result(self, result):
  -        solr = search.get_solr()
  -
  -        def process(p):
  -            return web.storage(
  +        publisher_facets = result['facets']['publisher_facet']
  +        return [
  +            web.storage(
                   name=p.value,
                   key="/publishers/" + p.value.replace(" ", "_"),
  -                count=solr.select({"publisher_facet": p.value}, rows=0)['num_found'],
  +                count=p.count,
               )
  -
  -        publisher_facets = result['facets']['publisher_facet'][:25]
  -        return [process(p) for p in publisher_facets]
  +            for p in publisher_facets
  +        ]
   
   
   class PublisherEngine(subjects.SubjectEngine):
  
verification_info: {"golden_diff": "diff --git a/openlibrary/plugins/worksearch/publishers.py b/openlibrary/plugins/worksearch/publishers.py\n--- a/openlibrary/plugins/worksearch/publishers.py\n+++ b/openlibrary/plugins/worksearch/publishers.py\n@@ -57,30 +57,28 @@\n \n     def GET(self):\n         i = web.input(q=\"\")\n-        solr = search.get_solr()\n-        q = {\"publisher\": i.q}\n-\n-        result = solr.select(\n-            q,\n+        result = search.get_solr().select(\n+            {\"publisher\": i.q, \"type\": \"work\"},\n             facets=[\"publisher_facet\"],\n-            fields=[\"publisher\", \"publisher_facet\"],\n+            facet_mincount=1,\n+            facet_limit=25,\n+            facet_contains=i.q,\n+            facet_contains_ignoreCase='true',\n             rows=0,\n         )\n         result = self.process_result(result)\n         return render_template('search/publishers', i.q, result)\n \n     def process_result(self, result):\n-        solr = search.get_solr()\n-\n-        def process(p):\n-            return web.storage(\n+        publisher_facets = result['facets']['publisher_facet']\n+        return [\n+            web.storage(\n                 name=p.value,\n                 key=\"/publishers/\" + p.value.replace(\" \", \"_\"),\n-                count=solr.select({\"publisher_facet\": p.value}, rows=0)['num_found'],\n+                count=p.count,\n             )\n-\n-        publisher_facets = result['facets']['publisher_facet'][:25]\n-        return [process(p) for p in publisher_facets]\n+            for p in publisher_facets\n+        ]\n \n \n class PublisherEngine(subjects.SubjectEngine):\n", "issue": "Publisher search endpoint solr performance\n<!-- What problem are we solving? What does the experience look like today? What are the symptoms? -->\r\n\r\nOur /search/publishers endpoint is doing a strange roll-up and submitting many solr select queries causing performance issues. Solution presumably is to not make more than 1 solr query on /search/publishers.\r\n\r\n### Proposal\r\n\r\nChange the backend call for /search/publishers to make a single query to solr `publisher:(...)` query.\r\n\r\n### Evidence / Screenshot (if possible)\r\n![image](https://user-images.githubusercontent.com/978325/227324438-3fe82d6b-c8b3-4f11-bb51-855898bb62a5.png)\r\n<img width=\"775\" alt=\"Screenshot 2023-03-23 at 12 18 55 PM\" src=\"https://user-images.githubusercontent.com/978325/227324919-d19b91c5-d19b-4746-9908-43e0f7cf1cbd.png\">\r\n\r\n### Relevant url?\r\n<!-- `https://openlibrary.org/...` -->\r\n\r\nhttp://testing.openlibrary.org/search/publishers?q=Black%20Dolls%20And%20White%20Dolls%20From%201940%20Through%201970%3A%20Their%20Impact%20Then%20On%20Black%20And%20White%20Children%27s%20Development%20\r\n\r\n### Related files\r\n<!-- Files related to this issue; this is super useful for new contributors who might want to help! If you're not sure, leave this blank; a maintainer will add them. -->\r\n\r\nhttps://github.com/internetarchive/openlibrary/blob/b897c8c51a79308e38f9825fac82864a5cc7d3ae/openlibrary/plugins/worksearch/publishers.py#L82\r\n\r\n### Stakeholders\r\n<!-- @ tag stakeholders of this bug -->\r\n\n", "before_files": [{"content": "\"\"\"Publisher pages\n\"\"\"\nfrom infogami.utils import delegate, stats\nfrom infogami.utils.view import render_template, safeint\nimport web\nimport logging\n\nfrom . import subjects\nfrom . import search\n\nlogger = logging.getLogger(\"openlibrary.worksearch\")\n\n\nclass publishers(subjects.subjects):\n    path = '(/publishers/[^/]+)'\n\n    def GET(self, key):\n        key = key.replace(\"_\", \" \")\n        page = subjects.get_subject(key, details=True)\n\n        if not page or page.work_count == 0:\n            web.ctx.status = \"404 Not Found\"\n            return render_template('publishers/notfound.tmpl', key)\n\n        return render_template(\"publishers/view\", page)\n\n    def is_enabled(self):\n        return \"publishers\" in web.ctx.features\n\n\nclass publishers_json(subjects.subjects_json):\n    path = '(/publishers/[^/]+)'\n    encoding = \"json\"\n\n    def is_enabled(self):\n        return \"publishers\" in web.ctx.features\n\n    def normalize_key(self, key):\n        return key\n\n    def process_key(self, key):\n        return key.replace(\"_\", \" \")\n\n\nclass index(delegate.page):\n    path = \"/publishers\"\n\n    def GET(self):\n        return render_template(\"publishers/index\")\n\n    def is_enabled(self):\n        return \"publishers\" in web.ctx.features\n\n\nclass publisher_search(delegate.page):\n    path = '/search/publishers'\n\n    def GET(self):\n        i = web.input(q=\"\")\n        solr = search.get_solr()\n        q = {\"publisher\": i.q}\n\n        result = solr.select(\n            q,\n            facets=[\"publisher_facet\"],\n            fields=[\"publisher\", \"publisher_facet\"],\n            rows=0,\n        )\n        result = self.process_result(result)\n        return render_template('search/publishers', i.q, result)\n\n    def process_result(self, result):\n        solr = search.get_solr()\n\n        def process(p):\n            return web.storage(\n                name=p.value,\n                key=\"/publishers/\" + p.value.replace(\" \", \"_\"),\n                count=solr.select({\"publisher_facet\": p.value}, rows=0)['num_found'],\n            )\n\n        publisher_facets = result['facets']['publisher_facet'][:25]\n        return [process(p) for p in publisher_facets]\n\n\nclass PublisherEngine(subjects.SubjectEngine):\n    def normalize_key(self, key):\n        return key\n\n    def get_ebook_count(self, name, value, publish_year):\n        # Query solr for this publish_year and publish_year combination and read the has_fulltext=true facet\n        solr = search.get_solr()\n        q = {\"publisher_facet\": value}\n\n        if isinstance(publish_year, list):\n            q['publish_year'] = tuple(publish_year)  # range\n        elif publish_year:\n            q['publish_year'] = publish_year\n\n        result = solr.select(q, facets=[\"has_fulltext\"], rows=0)\n        counts = {v.value: v.count for v in result[\"facets\"][\"has_fulltext\"]}\n        return counts.get('true')\n\n\ndef setup():\n    subjects.SUBJECTS.append(\n        subjects.SubjectMeta(\n            name=\"publisher\",\n            key=\"publishers\",\n            prefix=\"/publishers/\",\n            facet=\"publisher_facet\",\n            facet_key=\"publisher_facet\",\n            Engine=PublisherEngine,\n        )\n    )\n", "path": "openlibrary/plugins/worksearch/publishers.py"}]}