Full text search with Algolia for a Ghost membership site

How to full-text search posts that are restricted to members or paying subscribers, without making the full text available to unregistered visitors.

Full text search with Algolia for a Ghost membership site

My client wanted full text search, but many of her posts were for paid subscribers and/or free members only.  We wanted the text searchable, but not to have it be freely available.

This is not a full tutorial.  The link below has directions for how to set up Algolia and Netlify.  I'm just adding some refinements below.

algolia/packages/algolia at main · TryGhost/algolia
Contribute to TryGhost/algolia development by creating an account on GitHub.

Protecting full text content that's paywalled

I determined that the webhooks were going to Netlify give full text (regardless of post restrictions).  

To protect her content, I wanted to make sure that the full HTML wasn't available in the search result.  This is done in the Algolia console.  Here's what I settled on (under Configuration > Search Behavior > Retrieved Attributes):

I set highlighting to off, because it otherwise returns full fields.  But I set snippeting on, because I want to show a little extract that contains the search terms.  Here's what I settled on:

Great, so that protects her new content that comes in via Netlify.  Her users can find something and see a snippet even if it is paywalled, but they don't get the whole post.  

Getting existing paywalled full text into Algolia

My next problem was that the import tool was using the Content API, so it only had the publicly-visible part of her posts.  For some posts, that wasn't all that much, and it wasn't going to work.  I needed the Admin API.  This was just a matter of editing node_modules/@tryghost/algolia/bin/cli.js.  Here's what I needed to do:

Line 6:

// Line 6: Call the Admin API instead of content API 
// (Be sure to install the SDK first - npm install @tryghost/admin-api )

	const GhostContentAPI = require('@tryghost/admin-api');

// While you're there you could update the version to 'v5.0' (from 'canary'), but that's a warning not an error otherwise.

// Line 68:  Add filter, if you have draft posts that shouldn't get indexed:
const params = {limit: 'all', include: 'tags,authors', filter: "status:published"};

// line 80ish:  Add a parameter to make the API return HTML.
// This step is critical, and I spent way too long figuring it out.  Enjoy!

    params.formats = "html"
    context.posts = await ghost.posts.browse(params);

Then you also need to update your configuration file to use the Ghost Admin API key.  

That's it.  Run it and you'll get full text in Algolia, even with a paywall!