Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove highlightResult from response #337

Merged
merged 4 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading