From a053f209619429706f33baeeb2844a48756acce3 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 31 May 2022 08:20:59 -0700 Subject: [PATCH] Re-implements return first option Since we get all the results quickly now, this aggregates all the results that came back and sorts them by confidence, and returns the highest confidence result. The confidences aren't great on free text search, but conceptually that's how it should work at least. It may make sense to aggregate the search results in all contexts, but I'll propose that in a separate PR. --- bookwyrm/book_search.py | 4 ++-- bookwyrm/connectors/connector_manager.py | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/bookwyrm/book_search.py b/bookwyrm/book_search.py index e42a6d8c3..4b0a6eab9 100644 --- a/bookwyrm/book_search.py +++ b/bookwyrm/book_search.py @@ -148,8 +148,8 @@ class SearchResult: def __repr__(self): # pylint: disable=consider-using-f-string - return "".format( - self.key, self.title, self.author + return "".format( + self.key, self.title, self.author, self.confidence ) def json(self): diff --git a/bookwyrm/connectors/connector_manager.py b/bookwyrm/connectors/connector_manager.py index fc7849249..5c8b43fdb 100644 --- a/bookwyrm/connectors/connector_manager.py +++ b/bookwyrm/connectors/connector_manager.py @@ -91,13 +91,16 @@ def search(query, min_confidence=0.1, return_first=False): # load as many results as we can results = asyncio.run(async_connector_search(query, items, min_confidence)) + results = [r for r in results if r] if return_first: # find the best result from all the responses and return that - raise Exception("Not implemented yet") # TODO + all_results = [r for con in results for r in con["results"]] + all_results = sorted(all_results, key=lambda r: r.confidence, reverse=True) + return all_results[0] # failed requests will return None, so filter those out - return [r for r in results if r] + return results def first_search_result(query, min_confidence=0.1):