Plugin - Algolia Index Search

Here is a quick integration for Algolia to index your site. This plugin for now just submits fields on resource save to the Algolia API.

You will have to download the client from Github, unless you can use Composer

I am using SEO Keywords, so I added the field to the index. You will have to modify for your needs. It does not index the content field. I am relying on good descriptions and keywords to return the best results.

Plugin Code

<?php
$path = $modx->getOption('algolia_path', null, MODX_CORE_PATH . 'components/algolia/', true);
// composer autoload
// require __DIR__ . $path;

// if you are not using composer
require_once $path . 'vendor/autoload.php';

$ALGOLIA_APP_ID = $modx->getOption('ALGOLIA_APP_ID');
$ALGOLIA_ADMIN_API_KEY = $modx->getOption('ALGOLIA_ADMIN_API_KEY');
$ALGOLIA_INDEX_NAME = $modx->getOption('ALGOLIA_INDEX_NAME');


$eventName = $modx->event->name;
switch($eventName) {
 
        // Documents
        case 'OnDocFormSave':

        $resourceArray = $scriptProperties['resource']->toArray();

        if ($resourceArray['published'] == 0){
          $client = Algolia\AlgoliaSearch\SearchClient::create($ALGOLIA_APP_ID, $ALGOLIA_ADMIN_API_KEY);
          $index = $client->initIndex($ALGOLIA_INDEX_NAME);
          $index->deleteObject($resource->get('id'));
        }

        if ($resourceArray['published'] == 1 && $resourceArray['deleted'] == 0 && $resourceArray['searchable']) {

$client = Algolia\AlgoliaSearch\SearchClient::create($ALGOLIA_APP_ID, $ALGOLIA_ADMIN_API_KEY);
$index = $client->initIndex($ALGOLIA_INDEX_NAME);

$seoKeywords = $modx->getObject('seoKeywords', array('resource' => $resource->get('id')));

$description = $resource->get('description');

$parser = $modx->getParser();
$maxIterations= (integer) $modx->getOption('parser_max_iterations', null, 10);

$processFields = array("introtext", "description", "keywords");

foreach ($processFields as $value) {
  $$value = $resource->get($value);
if ($value == "keywords") {
  $keywords = $seoKeywords->get('keywords');
}
  $parser->processElementTags('', $$value, false, false, '[[', ']]', [], $maxIterations);
  $parser->processElementTags('', $$value, true, false, '[[', ']]', [], $maxIterations);
}

$index->saveObject(
    [
      'pagetitle'   => $resource->get('pagetitle'),
      'introtext'   => $introtext,
      'description' => $description,
      'url'         => $modx->makeUrl($resource->get('id'), '', '', 'full'),
      'keywords'    => $keywords,
      'alias'       => $resource->get('alias'),
      'objectID'    => $resource->get('id')
    ]
);

        }
        break;
}
return;

System Settings

Updated code to only index document resources…

<?php

$path = $modx->getOption('algolia_path', null, MODX_CORE_PATH . 'components/algolia/', true);
// composer autoload
// require __DIR__ . $path;

// if you are not using composer
require_once $path . 'vendor/autoload.php';

$ALGOLIA_APP_ID = $modx->getOption('ALGOLIA_APP_ID');
$ALGOLIA_ADMIN_API_KEY = $modx->getOption('ALGOLIA_ADMIN_API_KEY');
$ALGOLIA_INDEX_NAME = $modx->getOption('ALGOLIA_INDEX_NAME');


$eventName = $modx->event->name;
switch($eventName) {
 
        // Documents
        case 'OnDocFormSave':

        $resourceArray = $scriptProperties['resource']->toArray();

        if ($resourceArray['published'] == 0){
          $client = Algolia\AlgoliaSearch\SearchClient::create($ALGOLIA_APP_ID, $ALGOLIA_ADMIN_API_KEY);
          $index = $client->initIndex($ALGOLIA_INDEX_NAME);
          $index->deleteObject($resource->get('id'));
        }

        if ($resourceArray['content_type'] == 1 && $resourceArray['published'] == 1 && $resourceArray['deleted'] == 0 && $resourceArray['searchable']) {

$client = Algolia\AlgoliaSearch\SearchClient::create($ALGOLIA_APP_ID, $ALGOLIA_ADMIN_API_KEY);
$index = $client->initIndex($ALGOLIA_INDEX_NAME);

$seoKeywords = $modx->getObject('seoKeywords', array('resource' => $resource->get('id')));

$description = $resource->get('description');

$parser = $modx->getParser();
$maxIterations= (integer) $modx->getOption('parser_max_iterations', null, 10);

$processFields = array("introtext", "description", "keywords");

foreach ($processFields as $value) {
  $$value = $resource->get($value);
if ($value == "keywords") {
  $keywords = $seoKeywords->get('keywords');
}
  $parser->processElementTags('', $$value, false, false, '[[', ']]', [], $maxIterations);
  $parser->processElementTags('', $$value, true, false, '[[', ']]', [], $maxIterations);
}

$index->saveObject(
    [
      'pagetitle'   => $resource->get('pagetitle'),
      'introtext'   => $introtext,
      'description' => $description,
      'url'         => $modx->makeUrl($resource->get('id'), '', '', 'full'),
      'keywords'    => $keywords,
      'alias'       => $resource->get('alias'),
      'objectID'    => $resource->get('id')
    ]
);

        }
        break;
}
return;
1 Like

Thanks for sharing this here, @bwente. This will be helpful for some folks, for sure.

@rdegler this is what you’re looking for per our discussion in Slack I think :slight_smile:.