Skip to content

Commit e869b15

Browse files
authored
fix: remove highlightResult from response (#337)
## 🧭 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`
1 parent b71984f commit e869b15

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

algoliasearch_django/models.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ def get_model_attr(name):
3636
return partial(_getattr, name=name)
3737

3838

39+
def sanitize(hit):
40+
if "_highlightResult" in hit:
41+
hit.pop("_highlightResult")
42+
return hit
43+
44+
3945
class AlgoliaIndexError(Exception):
4046
"""Something went wrong with an Algolia Index."""
4147

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

494500
rules = []
495501
self.__client.browse_rules(
496-
self.index_name, lambda _resp: rules.extend(_resp.hits)
502+
self.index_name,
503+
lambda _resp: rules.extend([sanitize(_hit.to_dict()) for _hit in _resp.hits]),
497504
)
498505
if len(rules):
499506
logger.debug("Got rules for index %s: %s", self.index_name, rules)
500507
should_keep_rules = True
501508

502509
synonyms = []
503510
self.__client.browse_synonyms(
504-
self.index_name, lambda _resp: synonyms.extend(_resp.hits)
511+
self.index_name,
512+
lambda _resp: synonyms.extend([sanitize(_hit.to_dict()) for _hit in _resp.hits]),
505513
)
506514
if len(synonyms):
507515
logger.debug("Got synonyms for index %s: %s", self.index_name, rules)

runtests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def main():
1616
# kept here to run a single test
1717
# failures = test_runner.run_tests(
1818
# [
19-
# "tests.test_index.IndexTestCase.test_reindex_with_rules"
19+
# "tests.test_index.IndexTestCase"
2020
# ]
2121
# )
2222
failures = test_runner.run_tests(["tests"])

tests/test_index.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
from .models import User, Website, Example
1111

1212

13+
def sanitize(hit):
14+
if "_highlightResult" in hit:
15+
hit.pop("_highlightResult")
16+
return hit
17+
18+
1319
class IndexTestCase(TestCase):
1420
def setUp(self):
1521
self.client = algolia_engine.client
@@ -320,7 +326,7 @@ class WebsiteIndex(AlgoliaIndex):
320326
synonyms = []
321327
self.client.browse_synonyms(
322328
self.index.index_name,
323-
lambda _resp: synonyms.extend([_hit.to_dict() for _hit in _resp.hits]),
329+
lambda _resp: synonyms.extend([sanitize(_hit.to_dict()) for _hit in _resp.hits]),
324330
)
325331
self.assertEqual(len(synonyms), 1, "There should only be one synonym")
326332
self.assertIn(

0 commit comments

Comments
 (0)