Create a Rebuild Search Index button for the Frappe Blog Posts doctype
Searching blog posts using the Frappe + redisearch back-end relies on an up to date index and ways to trigger these updates.
Custom App Backend
The first thing we need to do is create a whitelisted python function to build the search index with redisearch. I placed a file in the following directory: frappe-directory/apps/my-custom-app/my-custom-app/api/blog_post_search.py
and created the buildblogposts_index() function inside this file. This function can be called by anyone who is logged into the frappe back-end or by using the bench --site mysite.local execute
command from the frappe directory.
Example output of running bench --site mysite.local execute my-custom-site.api.blog_post_search.build_blog_posts_index
INFO:root:Connecting to Redis at 127.0.0.1:13000
INFO:root:Redis connection successful
INFO:root:Fetched 2 blog posts
INFO:root:Deleting Index
INFO:root:Blog Posts Index created successfully with JSON support
INFO:root:Stored JSON document blog:1: {'title': '121tech Web Site - Version 2', 'blog_intro': 'It seems that all ...truncated for brevity...'}
INFO:root:Stored JSON document blog:2: {'title': 'Create a Rebuild Search Index button ...truncated for brevity...'}
INFO:root:Index contains 2 documents
See the following snippet for the full python and javascript files: https://gitlab.com/-/snippets/4884850
Frappe Desk
We then need a button at the top of the Blog Post form in the frappe desk view that will call this custom method. We do this by creating a javascript file to extend the javascript Blog Post class by adding a custom button with a frappe.call() function to our python method mentioned above. We place this javascript file in the public/js/
directory of our custom frappe app.
To attach this javascript override to the core frappe Blog Post doctype we add an entry into our hooks.py file as such:
doctype_js = {"Blog Post": "public/js/blog_post.js"}
Clicking this button in the Blog Post desk view will initiate the building of the blog post search index for our use from a front end website.
Here is what the button looks like in the frappe desk view for this blog post doc:
Front-end Framework
I am using a Vue/Nuxt frontend framework to create this web site and have created a simple search component to do a fetch request to the backend method search.posts()
included in the the above blog_post_search.py
file. This component then renders the results in a modal dialog for easy navigation. But I am not going to share this now as I have run out of time so it will have to wait for another day.
Hopefully this code will provide a solution to someone. Enjoy!

Christopher Robert Nuss
Father, web developer, farmer, genX
No comments yet. Login to start a new discussion Start a new discussion