Being a content site, it was pretty important that whichElement.com have search–which was pretty hard considering that I didn’t want to have any server-side components involved.
At first I thought I would just let Google index the site, and hook up a Google search box on the site to solve the problem. That was certainly an option. I thought I would have to do some SEO magic to make it happen correctly, but it was doable. In fact, Ray had solved this problem already.
But then I got to thinking, wouldn’t be cooler to rise to the challenge of a search without a server? Why, yes, yes it would. I broke up my needs into two parts:
- An index of the site’s content
- A mechanism for searching the index and displaying the results.
I kicked around a few ideas, but finally settled on the idea of creating a JSON file that had an array of objects with title, url, summary, and condensed content info. If I had such a file, all I would have to do is search through the JSON to find results. So the second part of my search was a snap. All I had to do was:
- Pull down the JSON file
- Run searches against that JSON file
- Present the results
All of this was pretty easy to do, and not revolutionary.
What it actually does:
- Reads in all HTML files in the site
- Filters ones that I don’t want in search results.
- Grabs the title, url, and content from each
- Writes out this content to JSON on disk
It’s not perfect. Search is pretty primitive – I don’t know how far it will scale. But for now, I have a pretty cool solution to my problem.
Here’s the indexer code: