feat: added index hooks to website generator

This commit is contained in:
Shivam Mishra 2020-06-17 23:10:31 +05:30
parent 702a6f103a
commit f0547ce0d0
2 changed files with 21 additions and 3 deletions

View file

@ -100,8 +100,12 @@ def search(index_name, text, scope=None, limit=20):
)
return out
def update_index_for_path(index_name, path):
document = get_document_to_index(path)
update_index(index_name, document)
def reindex(index_name, document):
def update_index(index_name, document):
# open index
index_dir = get_index_path(index_name)
ix = open_dir(index_dir)
@ -120,7 +124,7 @@ def reindex(index_name, document):
writer.commit(optimize=True)
def remove_from_index(index_name, path):
def remove_document_from_index(index_name, path):
# open index
index_dir = get_index_path(index_name)
ix = open_dir(index_dir)

View file

@ -7,6 +7,7 @@ from frappe.model.document import Document
from frappe.website.utils import cleanup_page_name
from frappe.website.render import clear_cache
from frappe.modules import get_module_name
from frappe.modules.full_text_search import update_index_for_path, remove_document_from_index
class WebsiteGenerator(Document):
website = frappe._dict()
@ -82,11 +83,18 @@ class WebsiteGenerator(Document):
pass
def on_update(self):
old_doc = self.get_doc_before_save()
if old_doc.route != self.route:
remove_document_from_index("web_routes", old_doc.route)
self.send_indexing_request()
def on_change(self):
self.update_website_search_index()
def on_trash(self):
self.clear_cache()
self.send_indexing_request('URL_DELETED')
remove_document_from_index("web_routes", self.route)
def is_website_published(self):
"""Return true if published in website"""
@ -129,4 +137,10 @@ class WebsiteGenerator(Document):
url = frappe.utils.get_url(self.route)
frappe.enqueue('frappe.website.doctype.website_settings.google_indexing.publish_site', \
url=url, operation_type=operation_type)
url=url, operation_type=operation_type)
def update_website_search_index(self):
if not self.is_website_published():
remove_document_from_index("web_routes", self.route)
else:
frappe.enqueue(update_index_for_path, index_name="web_routes", path=self.route)