Skip to content

Algolia sync

Push records to an Algolia search index whenever they are created, updated, or deleted on the network.

Trigger: record.index:<nsid>. Needs: happyview-http. Script variables: ALGOLIA_APP_ID, ALGOLIA_API_KEY.

local json = require("internal.json")
local http = require("happyview.http")
-- Percent-encode everything but unreserved characters, so the AT URI fits in a path segment
local function enc(s)
return (s:gsub("[^%w%-_%.~]", function(c) return string.format("%%%02X", c:byte()) end))
end
function handle(input, ctx)
local url = "https://" .. ctx.env.ALGOLIA_APP_ID .. ".algolia.net/1/indexes/records/" .. enc(input.uri)
local headers = {
["X-Algolia-API-Key"] = ctx.env.ALGOLIA_API_KEY,
["X-Algolia-Application-Id"] = ctx.env.ALGOLIA_APP_ID,
["Content-Type"] = "application/json",
}
local res
if input.action == "delete" then
res = http.delete(url, { headers = headers })
else
res = http.put(url, {
headers = headers,
body = json.encode({
objectID = input.uri,
collection = input.collection,
did = input.did,
record = input.record,
}),
})
end
if res.status >= 400 then
error("algolia answered " .. res.status)
end
-- On delete `record` is null, which still lets the delete proceed.
return input.record or true
end
  1. On create or update: sends a PUT request to Algolia’s index API with the record data, using the AT URI as the objectID. Algolia replaces an object that already exists.
  2. On delete: sends a DELETE request to remove the object from the index by its AT URI.

The AT URI is percent-encoded in the request path, with enc in Lua and encodeURIComponent in JavaScript and TypeScript, because its / characters would otherwise be read as path separators.

input is the record event. happyview.http returns a 4xx or 5xx answer as an ordinary result, so the script checks status and raises for it. A failed record script is retried and then dead-lettered.

Set these as script variables from the dashboard or the admin API:

VariableValue
ALGOLIA_APP_IDYour Algolia Application ID
ALGOLIA_API_KEYAn Algolia API key with the addObject and deleteObject ACLs

The index name is the records segment of the URL. Change it to any name you like.

Combine this with a query script that searches Algolia instead of the local database for full-text search beyond the substring match of happyview.db’s search.