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.
This commit is contained in:
Mouse Reeve 2022-05-31 08:20:59 -07:00
parent 98ed03b6b4
commit a053f20961
2 changed files with 7 additions and 4 deletions

View file

@ -148,8 +148,8 @@ class SearchResult:
def __repr__(self):
# pylint: disable=consider-using-f-string
return "<SearchResult key={!r} title={!r} author={!r}>".format(
self.key, self.title, self.author
return "<SearchResult key={!r} title={!r} author={!r} confidence={!r}>".format(
self.key, self.title, self.author, self.confidence
)
def json(self):

View file

@ -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):