CloudCannon / pagefind

Static low-bandwidth search at scale
https://pagefind.app
MIT License
3.42k stars 108 forks source link

Node.js Search Example #703

Open BeebBenjamin opened 1 week ago

BeebBenjamin commented 1 week ago

After wrestling with a React component approach to running a search on a Pagefind index, I came up with an alternative method for interfacing with the Search API whilst we wait for someone to tackle a full Node.js implementation of the Search API.

You can run the Search API as an embedded script on a small HTML page and use a simple Node.js express API to pass in the query params via a headerless browser i.e. puppeteer and parse the JSON.

This approach also allows you to catch the JSON response from Pagefind and format it to something else e.g. IIIF content search. I've also been making use of the Node.js indexing API to create custom content from linked data files e.g. json. I'm really impressed with it's power to provide full text searching to a static IIIF implementation.

Just wanted to document it here.

I would be interested to know what other people have done with Node.js.

Shbiro commented 5 days ago

Great approach! It’s always interesting to see creative workarounds while waiting for full implementations. Using Puppeteer to interface with the Search API is a clever solution, especially with how it allows parsing JSON responses from Pagefind into customizable formats. It sounds like you’ve built a solid bridge between the current limitations and the flexibility you need, particularly with IIIF content search.

In terms of other solutions with Node.js, I’ve seen a few approaches depending on the use case:

Direct API Integration: Some developers implement search functionalities using libraries like elasticsearch or lunr.js in Node.js, which provide more control over the indexing process. This can also be used to build real-time search capabilities, especially when working with dynamic data.

Static Site Search with Headless CMS: Using next.js or gatsby.js with a headless CMS is another common use case, where Node.js servers handle search queries and dynamic content rendering. This way, search is pre-built during the static site generation and still serves fast search results.

Middleware for Search Enhancements: Node.js can also act as middleware to augment search results with additional metadata from external sources (e.g., linking structured data like RDF, JSON-LD).

I’m curious about the specifics of how you are using the Node.js indexing API for custom content. Are there any unique challenges or optimizations you've run into with linked data files?