Skip to content

Commit

Permalink
fix: remove highlightResult from response (#337)
Browse files Browse the repository at this point in the history
## 🧭 What and Why

🎟 Related Issue:

### Changes included:

there is a regression in the new api clients, we don't remove the
`highlightResult` anymore of the returned objects from the browse
response.

in the meantime of a fix there, we can fix it in the integration to
prevent different behaviors, as the API doesn't allow saving rules with
`highlightResult`
  • Loading branch information
shortcuts authored Dec 24, 2024
1 parent b71984f commit e869b15
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
12 changes: 10 additions & 2 deletions algoliasearch_django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def get_model_attr(name):
return partial(_getattr, name=name)


def sanitize(hit):
if "_highlightResult" in hit:
hit.pop("_highlightResult")
return hit


class AlgoliaIndexError(Exception):
"""Something went wrong with an Algolia Index."""

Expand Down Expand Up @@ -493,15 +499,17 @@ def reindex_all(self, batch_size=1000):

rules = []
self.__client.browse_rules(
self.index_name, lambda _resp: rules.extend(_resp.hits)
self.index_name,
lambda _resp: rules.extend([sanitize(_hit.to_dict()) for _hit in _resp.hits]),
)
if len(rules):
logger.debug("Got rules for index %s: %s", self.index_name, rules)
should_keep_rules = True

synonyms = []
self.__client.browse_synonyms(
self.index_name, lambda _resp: synonyms.extend(_resp.hits)
self.index_name,
lambda _resp: synonyms.extend([sanitize(_hit.to_dict()) for _hit in _resp.hits]),
)
if len(synonyms):
logger.debug("Got synonyms for index %s: %s", self.index_name, rules)
Expand Down
2 changes: 1 addition & 1 deletion runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def main():
# kept here to run a single test
# failures = test_runner.run_tests(
# [
# "tests.test_index.IndexTestCase.test_reindex_with_rules"
# "tests.test_index.IndexTestCase"
# ]
# )
failures = test_runner.run_tests(["tests"])
Expand Down
8 changes: 7 additions & 1 deletion tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
from .models import User, Website, Example


def sanitize(hit):
if "_highlightResult" in hit:
hit.pop("_highlightResult")
return hit


class IndexTestCase(TestCase):
def setUp(self):
self.client = algolia_engine.client
Expand Down Expand Up @@ -320,7 +326,7 @@ class WebsiteIndex(AlgoliaIndex):
synonyms = []
self.client.browse_synonyms(
self.index.index_name,
lambda _resp: synonyms.extend([_hit.to_dict() for _hit in _resp.hits]),
lambda _resp: synonyms.extend([sanitize(_hit.to_dict()) for _hit in _resp.hits]),
)
self.assertEqual(len(synonyms), 1, "There should only be one synonym")
self.assertIn(
Expand Down

0 comments on commit e869b15

Please sign in to comment.