mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-26 11:31:08 +00:00
Merge pull request #2385 from bookwyrm-social/search-improvements
Simplify title/author search logic
This commit is contained in:
commit
1a8580cdbc
1 changed files with 9 additions and 10 deletions
|
@ -17,6 +17,8 @@ def search(query, min_confidence=0, filters=None, return_first=False):
|
||||||
filters = filters or []
|
filters = filters or []
|
||||||
if not query:
|
if not query:
|
||||||
return []
|
return []
|
||||||
|
query = query.strip()
|
||||||
|
|
||||||
results = None
|
results = None
|
||||||
# first, try searching unqiue identifiers
|
# first, try searching unqiue identifiers
|
||||||
# unique identifiers never have spaces, title/author usually do
|
# unique identifiers never have spaces, title/author usually do
|
||||||
|
@ -96,19 +98,16 @@ def search_title_author(query, min_confidence, *filters, return_first=False):
|
||||||
)
|
)
|
||||||
|
|
||||||
# when there are multiple editions of the same work, pick the closest
|
# 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
|
# filter out multiple editions of the same work
|
||||||
list_results = []
|
list_results = []
|
||||||
for work_id in set(editions_of_work):
|
for work_id in set(editions_of_work[:30]):
|
||||||
editions = results.filter(parent_work=work_id)
|
result = (
|
||||||
default = editions.order_by("-edition_rank").first()
|
results.filter(parent_work=work_id)
|
||||||
default_rank = default.rank if default else 0
|
.order_by("-rank", "-edition_rank")
|
||||||
# if mutliple books have the top rank, pick the default edition
|
.first()
|
||||||
if default_rank == editions.first().rank:
|
)
|
||||||
result = default
|
|
||||||
else:
|
|
||||||
result = editions.first()
|
|
||||||
|
|
||||||
if return_first:
|
if return_first:
|
||||||
return result
|
return result
|
||||||
|
|
Loading…
Reference in a new issue