Code Monkey home page Code Monkey logo

integration-example-algolia's Introduction

Core integration

Last modified Issues Contributors Forks MIT License

Stack Overflow Discord

Kontent.ai logo for dark/light scheme. algolia logo

DemoPrerequisitiesGet startedSearch data structureDevelopingContributorsLicenseResources

This repository contains an example implementation of integration between Algolia search engine and Kontent.ai. It produces an optional custom element for indexing your content in Algolia with a search preview functionality, as well as two Netlify functions for the initial setup and subsequent processing of content changes via a webhook.

This implementation also supports tracking changes in linked content, as well as searching in multiple languages.

Demo

algolia-custom-element

Prerequisities

  1. To run this integration, you'll need a Kontent.ai project + Algolia account.
  2. The content you want to be returned upon searching has to contain some kind of universal slug property (of any type). (Kontent.ai supports a SEO-friendly way to work with URLs through URL slugs).

Get Started

  1. Quick Deploy

    Netlify has made this easy. If you click the deploy button below, it will guide you through the process of deploying it to Netlify and leave you with a copy of the repository in your account as well.

    Deploy to Netlify

    After you deploy the project into Netlify, you'll find two functions there: netlify-functions

    1. algolia-init-function is the function that indexes or refreshes all of your content from Kontent.ai into Algolia.
    2. algolia-update-webhook is the function you want to call via a webhook from Kontent.ai to notify Algolia there's been a change to your content and that it has to be updated and how.
  2. Create a new Webhook in Kontent.ai

    The next step is creating a new webhook in Kontent.ai.

    Fill out the following into the webhook's URL address field:

    {algolia-update-webhook endpoint URL}?appId={algolia application id}&index={algolia search index name}&slug={codename of content's slug property}

    Subsequently, set the Kontent.ai Delivery API triggers to watch for changes in published data on content item events.

    At the end, this is an example of how your webhook might look like:

    webhook-setup

    Save the webhook and copy the generated secret as it would be required as a parameter in the following step.

  3. Configure your Netlify functions

    To setup both function, all you need to do is set the following environment variables for your Netlify site.

    Variable Value
    ALGOLIA_API_KEY your Algolia Admin key
    KONTENT_SECRET your Kontent.ai webhook secret

    The Algolia admin key is used by the function to create/recreate your search index, as well as for indexing your content.

    The Kontent.ai webhook secret is used to ensure integrity of the webhook payload. It has been created in the previous step.

    After your function has been redeployed (or your environment variables has been propagated), the functions are ready to be called.

    You can observe the real-time Function log on the same screen you'd find your function's endpoint (Functions -> your function).

  4. Run the index initialization

    The initialization of your Algolia index with your content is done through the algolia-init-function. Simply make a POST request towards the function's endpoint URL with the following payload:

    export const exampleInitRequestBody: InitRequestBody = {
    language: "{Kontent.ai language codename}",
    projectId: "{Kontent.ai project ID}",
    algoliaAppId: "{Algolia app id}",
    algoliaIndexName: "{Algolia index name}",
    slugCodename: "{Kontent.ai slug codename}",
    };

    The function processes all published content from your project (based on the given ID) and creates or updates the search index in Algolia (again, based on the given parameter).

    Alternatively, you can use the custom element that is a part of this repository as well.

    Simply add a new custom element into your content model (it might be a good idea to create some kind of meta content type to store some of the project's settings, so that would be a good place to put this element as well).

    Use your netlify's URL for the base page as the Hosted code URL and a following settings to setup the custom element:

    export const exampleCustomElementConfig: Config = {
    language: "{Kontent.ai language codename}",
    projectId: "{Kontent.ai project ID}",
    algoliaAppId: "{Algolia app id}",
    algoliaSearchKey: "{Algolia search-only api key}",
    algoliaIndexName: "{Algolia index name}",
    slugCodename: "{Kontent.ai slug codename}",
    };

    The Algolia's search-only api key is used to preview the search functionality from within the custom element.

    The custom element allows you to (re)initialize your Algolia index with all of your content + offers a way to preview your search results. Check out the demo gif above!

    If you are working with content in multiple language variants, adding additional variants to your search index is just as easy as just simply switching to the desired variant and activating the custom element to sync all content of the selected variant.

Search data structure

When building your search UI, you'll need to understand how the data is being stored inside of your Algolia search index. You can see an example of a content item with slug (i.e. separate "page"), that is linked to a Homepage page and contains its own content, as well as two linked items.

algolia-data

You can explore your own generated index to figure out how to implement your search, as well as the code of this custom element that is already implementing a preview of the search functionality here.

Developing

Netlify Dev is highly recommended for local development.

# Initial project setup
$ npm ci

# Build the project
$ npm run build

The integration is created with Vite.

Contributors

We have collected notes on how to contribute to this project in CONTRIBUTING.md.

License

MIT

Additional Resources

integration-example-algolia's People

Contributors

jirilojda avatar kontent-ai-bot avatar pokornyd avatar simply007 avatar strizr avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

integration-example-algolia's Issues

Invalid Config

Brief bug description

Have set up the config in Kontent.ai as specified in the readme, but the custom element throws " Invalid element config, the following properties are missing or invalid 0, 1, 2, 3, 4, 5."

Repro steps

  1. Set up Custom Element via Quick Deploy
  2. Set up custom element configuration in Kontent.ai
  3. Open the Item with the custom Element in Kontent.ai

Expected behavior

Validate element.config if correct, or point out specifically what properties of the config are invalid.

Additional context

config used {
"language": "es-us",
"projectId": "f9b63f57-16fd-0094-e757-2f72d2321c5f",
"algoliaAppId": "VG5LKCBKWQ",
"algoliaSearchKey": "ae3750d4df514f9bf3410909ed4d81c2",
"algoliaIndexName": "Final_Test",
"slugCodename": "url_slug"
}

Screenshots

image

Error: Cannot find module 'esbuild'

Brief bug description

Failing to install esbuild and to find package "@netlify/esbuild-windows-64" on the file system. Deployments to Netlify via Quick Deploy are failing for similar reason.

Repro steps

  1. Download Local Clone
  2. Run npm Install

Alternatively

  1. Use "Deploy to Netlify" Button

Expected behavior

Deploy to Netlify via quick deploy.

Additional context

Deploy Log

9:45:02 AM: build-image version: 5ad27658ebdf9c1e4379d77be303c2a3e3daa7bf (focal)
9:45:02 AM: buildbot version: 5ad27658ebdf9c1e4379d77be303c2a3e3daa7bf
9:45:02 AM: Fetching cached dependencies
9:45:02 AM: Failed to fetch cache, continuing with build
9:45:02 AM: Starting to prepare the repo for build
9:45:02 AM: No cached dependencies found. Cloning fresh repo
9:45:02 AM: git clone --filter=blob:none https://github.com/kontent-ai/integration-example-algolia
9:45:03 AM: Preparing Git Reference refs/heads/main
9:45:04 AM: Parsing package.json dependencies
9:45:05 AM: Different publish path detected, going to use the one specified in the Netlify configuration file: 'build' versus '/build' in the Netlify UI
9:45:05 AM: Starting build script
9:45:05 AM: Installing dependencies
9:45:05 AM: Python version set to 2.7
9:45:05 AM: v16.19.1 is already installed.
9:45:06 AM: Now using node v16.19.1 (npm v8.19.3)
9:45:06 AM: Enabling Node.js Corepack
9:45:06 AM: Started restoring cached build plugins
9:45:06 AM: Finished restoring cached build plugins
9:45:06 AM: Attempting Ruby version 2.7.2, read from environment
9:45:06 AM: Using Ruby version 2.7.2
9:45:07 AM: Using PHP version 8.0
9:45:07 AM: Started restoring cached corepack dependencies
9:45:07 AM: Finished restoring cached corepack dependencies
9:45:07 AM: No npm workspaces detected
9:45:07 AM: Started restoring cached node modules
9:45:07 AM: Finished restoring cached node modules
9:45:07 AM: Installing npm packages using npm version 8.19.3
9:45:44 AM: npm ERR! code 1
9:45:44 AM: npm ERR! path /opt/build/repo/node_modules/netlify-cli/node_modules/@netlify/esbuild
9:45:44 AM: npm ERR! command failed
9:45:44 AM: npm ERR! command sh -c -- node install.js
9:45:44 AM: npm ERR! [esbuild] Failed to find package "@netlify/esbuild-linux-64" on the file system
9:45:44 AM: npm ERR!
9:45:44 AM: Failed during stage 'building site': Build script returned non-zero exit code: 1 (https://ntl.fyi/exit-code-1)
9:45:44 AM: npm ERR! This can happen if you use the "--no-optional" flag. The "optionalDependencies"
9:45:44 AM: npm ERR! package.json feature is used by esbuild to install the correct binary executable
9:45:44 AM: npm ERR! for your current platform. This install script will now attempt to work around
9:45:44 AM: npm ERR! this. If that fails, you need to remove the "--no-optional" flag to use esbuild.
9:45:44 AM: npm ERR!
9:45:44 AM: npm ERR! node:internal/modules/cjs/loader:1021
9:45:44 AM: npm ERR! const err = new Error(message);
9:45:44 AM: npm ERR! ^
9:45:44 AM: npm ERR!
9:45:44 AM: npm ERR! Error: Cannot find module 'esbuild'
9:45:44 AM: npm ERR! Require stack:
9:45:44 AM: npm ERR! - /opt/build/repo/node_modules/netlify-cli/node_modules/@netlify/esbuild/install.js
9:45:44 AM: npm ERR! at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1021:15)
9:45:44 AM: npm ERR! at Function.resolve (node:internal/modules/cjs/helpers:114:19)
9:45:44 AM: npm ERR! at downloadedBinPath (/opt/build/repo/node_modules/netlify-cli/node_modules/@netlify/esbuild/install.js:86:46)
9:45:44 AM: npm ERR! at checkAndPreparePackage (/opt/build/repo/node_modules/netlify-cli/node_modules/@netlify/esbuild/install.js:232:15)
9:45:44 AM: npm ERR! at Object. (/opt/build/repo/node_modules/netlify-cli/node_modules/@netlify/esbuild/install.js:247:1)
9:45:44 AM: npm ERR! at Module._compile (node:internal/modules/cjs/loader:1191:14)
9:45:44 AM: npm ERR! at Object.Module._extensions..js (node:internal/modules/cjs/loader:1245:10)
9:45:44 AM: npm ERR! at Module.load (node:internal/modules/cjs/loader:1069:32)
9:45:44 AM: npm ERR! at Function.Module._load (node:internal/modules/cjs/loader:904:12)
9:45:44 AM: npm ERR! at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
9:45:44 AM: npm ERR! code: 'MODULE_NOT_FOUND',
9:45:44 AM: npm ERR! requireStack: [
9:45:44 AM: npm ERR! '/opt/build/repo/node_modules/netlify-cli/node_modules/@netlify/esbuild/install.js'
9:45:44 AM: npm ERR! ]
9:45:44 AM: npm ERR! }
9:45:44 AM: npm ERR! A complete log of this run can be found in:
9:45:44 AM: npm ERR! /opt/buildhome/.npm/_logs/2023-02-28T17_45_07_535Z-debug-0.log
9:45:44 AM: Error during npm install
9:45:44 AM: Build was terminated: Build script returned non-zero exit code: 1
9:45:44 AM: Failing build: Failed to build site
9:45:45 AM: Finished processing build request in 43.338s

Multiple indexes in Algolia

Motivation

Nicholas from Kontent.ai support suggested I submit a feature request.

Proposed solution

The feature request is to sync content from one Kontnet.ai site to more than one Algolia index. We want to have the same data in two different indexes in Algolia. One index would be for development (sandbox) and the other index would be for production.

Error message is not descriptive

Motivation

Why is this feature required? What problems does it solve?

Error message: "Some environment variables are missing. Please check the documentation" is not informative to developers. When the error is displayed in the webhook dashboard, they find it challenging to look for resources to help themselves.

Proposed solution

Better error message. Like what exactly is missing would help.

Suggestions:
Environment variables are missing. Please add kontent secret and algolia API key.

Additional context

None

Index initialization Issue

Brief bug description

Running the index initialization give me "Missing or invalid body, please check the documentation" and the index doesn't get created.

Repro steps

In postman I attempt to run the index initialization and pass it the required parameters. The only response I get is "Missing or invalid body, please check the documentation", No errors Netlify.

Expected behavior

What is the correct behavior?

Test environment

  • Platform/OS: [e.g. .NET Core 2.1, iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Additional context

I worked with Kontent.ai support and we couldn't get it resolved.

Screenshots

Add links to screenshots, if possible.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.