Skip to content

Commit

Permalink
Fix #145 -- Fix platform search (#144)
Browse files Browse the repository at this point in the history
Fix #145

After we ugpraded the Algolia python client to version 4
we had to change how to use it.

### How to test

- Run `sam run slack`
- Open AlphaSam and ask it to search something on the platform
- Should work
  • Loading branch information
herrbenesch authored Nov 20, 2024
1 parent d0c1c1a commit 539eccf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
33 changes: 21 additions & 12 deletions sam/contrib/algolia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import abc

import requests
from algoliasearch.search.client import SearchClient
from algoliasearch.search.client import SearchClientSync

from . import config

Expand Down Expand Up @@ -32,8 +32,8 @@ class AlgoliaSearch(AbstractAlgoliaSearch): # pragma: no cover
def __init__(self, application_id, api_key, index):
super().__init__()
self.api_key = api_key
client = SearchClient.create(application_id, api_key)
self.index = client.init_index(index)
self.client = SearchClientSync(application_id, api_key)
self.index_name = index
self.params = {}

def __enter__(self):
Expand All @@ -43,28 +43,37 @@ def __exit__(self, exc_type, exc_val, exc_tb):
pass

def search(self, query):
self.params.update(
{
"length": 5,
}
)
try:
return self.index.search(
query,
request_options={
**self.params,
"length": 5,
},
return self.client.search_single_index(
index_name=self.index_name,
search_params={"query": query, **self.params},
)
except requests.HTTPError as e:
raise AlgoliaSearchAPIError("The Algolia search API call failed.") from e


class AlgoliaSearchStub(AbstractAlgoliaSearch):
class SearchResponseStub:
def __init__(self):
self.headers = {}
self._objects = [
{
"title": "Deutschland",
"parent_object_title": "Ferienangebote",
"public_url": "https://www.schulferien.org/deutschland/ferien/",
},
]

def to_dict(self):
return {"hits": self._objects, "nbPages": 1}


class AlgoliaSearchStub(AbstractAlgoliaSearch):
def __init__(self):
self.headers = {}
self.params = {}

def __enter__(self):
Expand All @@ -74,7 +83,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
pass

def search(self, query):
return {"hits": self._objects, "nbPages": 1}
return SearchResponseStub()


def get_client(index=None) -> AbstractAlgoliaSearch:
Expand Down
2 changes: 1 addition & 1 deletion sam/contrib/algolia/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def search(query: str, _context=None) -> str:
}
)
try:
results = api.search(query)["hits"]
results = api.search(query).to_dict()["hits"]
except algolia.AlgoliaSearchAPIError:
logger.exception("Failed to search the platform for query: %s", query)
return "search failed"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def test_platform_search_with_error():

def test_platform_search_no_results():
with mock.patch(
"sam.contrib.algolia.AlgoliaSearchStub.search", return_value={"hits": []}
"sam.contrib.algolia.SearchResponseStub.to_dict", return_value={"hits": []}
):
assert sam.contrib.algolia.tools.search("something") == "no results found"

Expand Down

0 comments on commit 539eccf

Please sign in to comment.