Repository: internetarchive/openlibrary

Issue:
User language only influences results if an edition query present
eg `subject:horror` not preferring user lang!


Discussion:

Resolving patch:
diff --git a/openlibrary/plugins/worksearch/code.py b/openlibrary/plugins/worksearch/code.py
--- a/openlibrary/plugins/worksearch/code.py
+++ b/openlibrary/plugins/worksearch/code.py
@@ -1,4 +1,5 @@
 from dataclasses import dataclass
+import time
 import copy
 import json
 import logging
@@ -230,10 +231,12 @@ def run_solr_query(
         params.append(('sort', scheme.process_user_sort(sort)))
 
     url = f'{solr_select_url}?{urlencode(params)}'
-
+    start_time = time.time()
     response = execute_solr_query(solr_select_url, params)
     solr_result = response.json() if response else None
-    return SearchResponse.from_solr_result(solr_result, sort, url)
+    end_time = time.time()
+    duration = end_time - start_time
+    return SearchResponse.from_solr_result(solr_result, sort, url, time=duration)
 
 
 @dataclass
@@ -245,12 +248,15 @@ class SearchResponse:
     solr_select: str
     raw_resp: dict = None
     error: str = None
+    time: float = None
+    """Seconds to execute the query"""
 
     @staticmethod
     def from_solr_result(
         solr_result: dict | None,
         sort: str,
         solr_select: str,
+        time: float,
     ) -> 'SearchResponse':
         if not solr_result or 'error' in solr_result:
             return SearchResponse(
@@ -260,6 +266,7 @@ def from_solr_result(
                 num_found=None,
                 solr_select=solr_select,
                 error=(solr_result.get('error') if solr_result else None),
+                time=time,
             )
         else:
             return SearchResponse(
@@ -277,6 +284,7 @@ def from_solr_result(
                 docs=solr_result['response']['docs'],
                 num_found=solr_result['response']['numFound'],
                 solr_select=solr_select,
+                time=time,
             )
 
 
@@ -449,13 +457,51 @@ def GET(self):
         for k in ('title', 'author', 'isbn', 'subject', 'place', 'person', 'publisher'):
             if k in i:
                 q_list.append(f'{k}:{fully_escape_query(i[k].strip())}')
+
+        web_input = i
+        param = {}
+        for p in {
+            'q',
+            'title',
+            'author',
+            'page',
+            'sort',
+            'isbn',
+            'oclc',
+            'contributor',
+            'publish_place',
+            'lccn',
+            'ia',
+            'first_sentence',
+            'publisher',
+            'author_key',
+            'debug',
+            'subject',
+            'place',
+            'person',
+            'time',
+            'editions.sort',
+        } | WorkSearchScheme.facet_fields:
+            if p in web_input and web_input[p]:
+                param[p] = web_input[p]
+        if list(param) == ['has_fulltext']:
+            param = {}
+
+        page = int(param.get('page', 1))
+        sort = param.get('sort', None)
+        rows = 20
+        search_response: SearchResponse | None = None
+        if param:
+            search_response = do_search(
+                param, sort, page, rows=rows, spellcheck_count=3
+            )
         return render.work_search(
-            i,
             ' '.join(q_list),
-            do_search,
+            search_response,
             get_doc,
-            fulltext_search,
-            WorkSearchScheme.facet_fields,
+            param,
+            page,
+            rows,
         )
 
 
diff --git a/openlibrary/plugins/worksearch/schemes/works.py b/openlibrary/plugins/worksearch/schemes/works.py
--- a/openlibrary/plugins/worksearch/schemes/works.py
+++ b/openlibrary/plugins/worksearch/schemes/works.py
@@ -2,7 +2,7 @@
 import logging
 import re
 import sys
-from typing import Any
+from typing import Any, cast
 from collections.abc import Callable
 
 import luqum.tree
@@ -298,6 +298,7 @@ def q_to_solr_params(
         )
 
         ed_q = None
+        full_ed_query = None
         editions_fq = []
         if has_solr_editions_enabled() and 'editions:[subquery]' in solr_fields:
             WORK_FIELD_TO_ED_FIELD: dict[str, str | Callable[[str], str]] = {
@@ -454,7 +455,7 @@ def convert_work_query_to_edition_query(work_query: str) -> str:
         if ed_q or len(editions_fq) > 1:
             # The elements in _this_ edition query should cause works not to
             # match _at all_ if matching editions are not found
-            new_params.append(('edQuery', full_ed_query if ed_q else '*:*'))
+            new_params.append(('edQuery', cast(str, full_ed_query) if ed_q else '*:*'))
             q = (
                 f'+{full_work_query} '
                 # This is using the special parent query syntax to, on top of
@@ -467,6 +468,10 @@ def convert_work_query_to_edition_query(work_query: str) -> str:
                 ')'
             )
             new_params.append(('q', q))
+        else:
+            new_params.append(('q', full_work_query))
+
+        if full_ed_query:
             edition_fields = {
                 f.split('.', 1)[1] for f in solr_fields if f.startswith('editions.')
             }
@@ -484,8 +489,6 @@ def convert_work_query_to_edition_query(work_query: str) -> str:
             )
             new_params.append(('editions.rows', '1'))
             new_params.append(('editions.fl', ','.join(edition_fields)))
-        else:
-            new_params.append(('q', full_work_query))
 
         return new_params
 


Test patch: