diff --git a/algoliasearch_django/models.py b/algoliasearch_django/models.py index 5e55822..97819cc 100644 --- a/algoliasearch_django/models.py +++ b/algoliasearch_django/models.py @@ -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.""" @@ -493,7 +499,8 @@ 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) @@ -501,7 +508,8 @@ def reindex_all(self, batch_size=1000): 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) diff --git a/runtests.py b/runtests.py index 8d97daa..ff520a7 100755 --- a/runtests.py +++ b/runtests.py @@ -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"]) diff --git a/tests/test_index.py b/tests/test_index.py index 33fbad3..62ecdd8 100644 --- a/tests/test_index.py +++ b/tests/test_index.py @@ -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 @@ -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(