FlexSearch Plugin for Nikola: New Version 0.2 Released

TLDR

Version 0.2 of the FlexSearch Plugin for Nikola is now available! This update adds page indexing, improved search relevance, better UI, and more configuration options. You can now search both posts and pages, with results showing content type badges and better formatting.

What's New in Version 0.2?

It's been a while since I first released the FlexSearch plugin, and I've been using it a lot and working on improvements (I developed the plugin because I use this site as a personal repository).

Version 0.2 brings several new features and improvements (IMHO) that make the search functionality more powerful and flexible.

Major New Features

1. Page Indexing

The original version only indexed posts (and I did not care until I wanted to find a page and couldn't find it), but now you can also index pages! This means you can search through all your content, not just blog posts. This is particularly useful for me as I published a lot of additional resources for my book "The Digital Marketer's Playbook," and they could not be found using the search. It took me a while but it now works 😁

You can control this with 3 new configuration options:

  • FLEXSEARCH_INDEX_POSTS (default: True) - Whether to index posts
  • FLEXSEARCH_INDEX_PAGES (default: False) - Whether to index pages
  • FLEXSEARCH_INDEX_DRAFTS (default: False) - Whether to index draft content

2. Improve the search index

The search index now includes titles, content, and tags (which you can change in the source code), making search results much more relevant. The plugin uses a multi-field search approach that combines all these elements when searching:

searchIndex.add(key, data[key].title + " " + data[key].content + data[key].tags + " " + data[key].content);

The data[key].content part is commented out by default as I was getting just too many results.

This means that searching for a tag will find all posts with that tag, and searching for words in titles will give those results higher priority. This is thanks to the flexsearch library.

3. Content Type Display

Search results now show whether an item is a post or a page, with styled badges. This makes it easier to distinguish between different types of content in your search results. (this is at least important for me)

Just search something on this sile, for example nikola to see how it behaves.

4. Keyboard Shortcuts

I've added some convenient keyboard shortcuts. I don't really remember why I did not add this before:

  • Press ESC to close the search overlay
  • Press Enter to submit your search

5. Better URL Handling

The plugin now uses absolute paths (/search_index.json) instead of relative ones, which fixes an issue with path resolution on different pages. It also adds ?utm_source=internal_search to result URLs, making it easier to track internal search usage in analytics, if you use any.

Technical Improvements

For those who like to tinker with their plugins, I've also made some technical improvements:

  • Added console logging for easier debugging
  • Updated to the latest FlexSearch v0.8.0 library, released 4 days ago by pure coincidence
  • Added better error handling throughout the code
  • Improved code organization and structure (or at least I hope so)

How to Update

IMPORTANT: Remember to update the javascript snippets with the new versions. Make sure you read the README and make a backup of your plugin version before updating.

If you're already using or if you want to install the FlexSearch plugin, you can use:

nikola plugin -i flexsearch_plugin

After updating, rebuild your site with nikola build -a to generate the updated search index.

Configuration

To enable page indexing, add these lines to your conf.py:

# FlexSearch Plugin Configuration
FLEXSEARCH_INDEX_POSTS = True  # Default is True
FLEXSEARCH_INDEX_PAGES = True  # Default is False
FLEXSEARCH_INDEX_DRAFTS = False  # Default is False

What's Next?

As I'm using the plugin myself, and I don't plan to migrate my site from Nikola anytime soon, I will at least keep maintaining the plugin to meet my needs.

That may include:

  • Better mobile support, although the snippets and css are already responsive and work for me)
  • More customization options for search results (maybe some filters)
  • Anything I find useful while I use this.

If you have suggestions or encounter any issues, feel free to open an issue with a pull request on the Nikola Plugins repository.

References