From 4cc69a849b31dd2a2eb776dd3376f94b30c1957d Mon Sep 17 00:00:00 2001 From: Stefan Pratter Date: Mon, 22 May 2023 16:42:30 +0300 Subject: [PATCH] Fix v2 search indexing deleted items (#1386) * dont index deleted objects #1384 * linting * instance to thing --- peeringdb_server/documents.py | 41 ++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/peeringdb_server/documents.py b/peeringdb_server/documents.py index e4e878b4..67b7321f 100644 --- a/peeringdb_server/documents.py +++ b/peeringdb_server/documents.py @@ -1,5 +1,7 @@ import re +from types import GeneratorType +import elasticsearch.helpers.errors as errors from django_elasticsearch_dsl import Document, fields from django_elasticsearch_dsl.registries import registry @@ -21,7 +23,44 @@ def is_valid_longitude(long): ) -class GeocodeMixin: +class StatusMixin: + + """ + Ensures only objects with status=ok are indexed + and deleted from the index if status is no longer ok + """ + + def get_queryset(self): + return super().get_queryset().filter(status="ok") + + def should_index_object(self, obj): + return obj.status == "ok" + + def update(self, thing, **kwargs): + """ + Updates the document with the given kwargs. + """ + + # if is iterable then we are bulk indexing and can just proceed normally + if isinstance(thing, GeneratorType): + return super().update(thing, **kwargs) + + attempt_delete = False + + # otherwise we are updating a single object + if thing.status != "ok": + kwargs["action"] = "delete" + attempt_delete = True + try: + return super().update(thing, **kwargs) + except errors.BulkIndexError as e: + if attempt_delete: + pass + else: + raise e + + +class GeocodeMixin(StatusMixin): """ Cleans up invalid lat/lng values beforee passing