Skip to content

simplelogica/algolia_drupal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Algolia adapter for Drupal

This module provides a base adapter that can be used by other modules to integrate with Algolia indexation services.

This module hooks automatically into the node lifecycle to trigger a sinchronization when a node is created, updated or deleted. It also sets a hook to trigger a full indexation on every cron run.

  1. Installation
  2. Setup
  3. Customization
  4. Usage

Installation

  1. Download the Algolia PHP client into the libraries folder of your Drupal application and rename its folder to algoliasearch.
  2. Download this module into your modules directory: git clone https://github.com/simplelogica/algolia_drupal.git sites/all/modules/custom/algolia. And enable it with the command drush en algolia.
  3. Create your own customization module to configure indexes, content-types and serializations (see next section).

Setup

In your settings.php you must configure the applications that will be used for the Algolia integration. You can have as many applications as you want.

$conf["algolia_accounts"] = [
  "application_1" => [
    "application_id" => "SUPER-SECRET-APP-ID",
    "admin_api_key" => "SUPER-SECRET-ADMIN-API-KEY",
    "api_key" => "SUPER-SECRET-SEARCH-API-KEY",
  ],
  "application_2" => [
    "application_id" => "SUPER-SECRET-APP-ID",
    "admin_api_key" => "SUPER-SECRET-ADMIN-API-KEY",
    "api_key" => "SUPER-SECRET-SEARCH-API-KEY",
  ],
  ...
];

Customization

This module is meant to provide a generic functionality that can be extended by other modules to indicate which indexes exist, which content-types belong to each index and how they are serialized before being sent to Algolia.

You must create a module and include a dependency in your yourmodule.info file:

dependencies[] = algolia

Then you must implement hook_configure_algolia() in your yourmodule.module file. The hook must return an array like the following one:

'my_index' => [
  'account' => 'my_account',
  'config' => [
    'attributesToIndex' => ['title', 'body', 'description'],
    'slaves' => ['my_index_slave']
  ],
  'content' => [
    'content_type_a' => 'my_serialization_callback',
    'content_type_b' => 'my_other_serialization_callback',
  ],
],
'my_index_slave' => [
  'account' => 'alumni',
  'config' => [
    'attributesToIndex' => ['title', 'body']
  ],
],
...

The array has the names of the existing indexes as keys. Each index must state the account it belongs to, its configuration and its content types. The values set in config are passed as-is to Algolia.

The content key is used during indexation to know which content types belong to a given index. For each content type we define a serialization callback that will receive a node of the given content type, and must return an array with the attributes defined in the index. For example:

function my_serialization_callback($node) {
  // $node is always of type `content_type_a`
  return [
    'title' => $node->title,
    'body' => $node->body,
    'description' => $node->body->summary
  ];
}

Usage

This module automatically hooks into the node lifecycle and updates Algolia indexes on node insertions, deletions and updates. Algolia also triggers a reindexation on every cron run.

Should you wish to update indexes content or settings manually, you can use the following drush commands:

  • drush algolia-update-index-settings: updates the settings (such as attributesToIndex or slaves) for all defined indexes.
  • drush algolia-index-content: reindexes all applicable content. You can also add the --type=CONTENT-TYPE-MACHINE-NAME option to reindex only the content of the given content-type. Another useful option is --uri=http://example.com:8888 to generate the links with the proper base URL.

Releases

No releases published

Packages

No packages published

Languages