From 9185708224afd250154e69e7a972d3937a621a66 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 15 Nov 2022 20:05:41 -0800 Subject: [PATCH 1/3] Simplify how default edition is checked This logic is still totally bonkers, but this change puts a hard limit on how many iterations the loop can go through and makes the query that selects which edition to display a little simpler. --- bookwyrm/book_search.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/bookwyrm/book_search.py b/bookwyrm/book_search.py index 227d8211f..d0856029f 100644 --- a/bookwyrm/book_search.py +++ b/bookwyrm/book_search.py @@ -96,19 +96,14 @@ def search_title_author(query, min_confidence, *filters, return_first=False): ) # when there are multiple editions of the same work, pick the closest - editions_of_work = results.values("parent_work__id").values_list("parent_work__id") + editions_of_work = results.values_list("parent_work__id", flat=True).distinct() # filter out multiple editions of the same work list_results = [] - for work_id in set(editions_of_work): - editions = results.filter(parent_work=work_id) - default = editions.order_by("-edition_rank").first() - default_rank = default.rank if default else 0 - # if mutliple books have the top rank, pick the default edition - if default_rank == editions.first().rank: - result = default - else: - result = editions.first() + for work_id in set(editions_of_work[:30]): + result = results.filter(parent_work=work_id).order_by( + "-rank", "-edition_rank" + ).first() if return_first: return result From f0f65b8b73faec2cb338d5db191596f77db1ecd0 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 15 Nov 2022 20:08:17 -0800 Subject: [PATCH 2/3] Python formatting --- bookwyrm/book_search.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bookwyrm/book_search.py b/bookwyrm/book_search.py index d0856029f..6f1618029 100644 --- a/bookwyrm/book_search.py +++ b/bookwyrm/book_search.py @@ -101,9 +101,11 @@ def search_title_author(query, min_confidence, *filters, return_first=False): # filter out multiple editions of the same work list_results = [] for work_id in set(editions_of_work[:30]): - result = results.filter(parent_work=work_id).order_by( - "-rank", "-edition_rank" - ).first() + result = ( + results.filter(parent_work=work_id) + .order_by("-rank", "-edition_rank") + .first() + ) if return_first: return result From 47f1865c590e9184e64a6033257a736d83473244 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 15 Nov 2022 20:09:00 -0800 Subject: [PATCH 3/3] Remove trailing whitespace from queries --- bookwyrm/book_search.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bookwyrm/book_search.py b/bookwyrm/book_search.py index 6f1618029..f9bb57dfc 100644 --- a/bookwyrm/book_search.py +++ b/bookwyrm/book_search.py @@ -17,6 +17,8 @@ def search(query, min_confidence=0, filters=None, return_first=False): filters = filters or [] if not query: return [] + query = query.strip() + results = None # first, try searching unqiue identifiers # unique identifiers never have spaces, title/author usually do