diff --git a/realms/modules/search/hooks.py b/realms/modules/search/hooks.py index 503beb0..b31acf2 100644 --- a/realms/modules/search/hooks.py +++ b/realms/modules/search/hooks.py @@ -2,7 +2,6 @@ from realms.modules.wiki.models import Wiki from realms import search - @Wiki.after('write_page') def wiki_write_page(name, content, message=None, username=None, email=None, **kwargs): @@ -19,5 +18,9 @@ def wiki_write_page(name, content, message=None, username=None, email=None, **kw @Wiki.after('rename_page') -def wiki_rename_page(*args, **kwargs): - pass +def wiki_rename_page(old_name, *args, **kwargs): + + if not hasattr(search, 'index_wiki'): + return + + return search.delete_wiki(old_name) diff --git a/realms/modules/search/models.py b/realms/modules/search/models.py index 267c5c5..26bdc71 100644 --- a/realms/modules/search/models.py +++ b/realms/modules/search/models.py @@ -92,9 +92,19 @@ class WhooshSearch(BaseSearch): writer.update_document(path=id_.decode("utf-8"), body=body["content"].decode("utf-8")) writer.commit() + def delete(self, id_): + with self.search_index.searcher() as s: + doc_num = s.document_number(path=id_.decode("utf-8")) + writer = self.search_index.writer() + writer.delete_document(doc_num) + writer.commit() + def index_wiki(self, name, body): self.index('wiki', 'page', id_=name, body=body) + def delete_wiki(self, name): + self.delete(id_=name) + def delete_index(self, index): from whoosh import index as whoosh_index self.search_index.close() @@ -133,9 +143,15 @@ class ElasticSearch(BaseSearch): def index(self, index, doc_type, id_=None, body=None): return self.elastic.index(index=index, doc_type=doc_type, id=id_, body=body) + def delete(self, index, doc_type, id_): + return self.elastic.delete(index=index, doc_type=doc_type, id=id_) + def index_wiki(self, name, body): self.index('wiki', 'page', id_=name, body=body) + def delete_wiki(self, name): + self.delete('wiki', 'page', id_=name) + def delete_index(self, index): return self.elastic.indices.delete(index=index, ignore=[400, 404])