Algolia with Drupal: How to Fine Tune the Index

How One Gets Content from Drupal for an Algolia Search

If you want fast search of thousands of records, including type ahead where results display as you type, you might consider Algolia. Here are the basics of how to get content into an Algolia search. We will assume you already have an Algolia account with app id and api key. First, get the content from Drupal using the contrib modules search_api and search_api_algolia. Once the content is in records in an index on the Algolia server, write a front end app so users can search. The front end app can be written using InstantSearch, available in languages such as ReactJS and VueJS. You can have a great search with just those basics. But perhaps you want to fine tune your search.

Change a Content Type Label for a Filter using hook hook_search_api_algolia_objects_alter

Users often like to filter content so it is easier to find what they want. However, your content types may be named for internal reasons and not in a way that is meaningful to the users. For example, you may have a content type called Basic Page and another called Landing Page. In the front end app, you would like each of those content types to be labelled as Page. You could possibly do this on the front end. However, all the sorting of content is done on the Algolia server, so the front end is not the ideal place for a sort.

There is a hook that comes with search_api_algolia called hook_search_api_algolia_objects_alter. Using this hook, one can manipulate the records before they go to the server. Here’s how I converted the Basic Page and Landing Page to Page:

function my_module_search_api_algolia_objects_alter(array &$objects, IndexInterface $index, array $items) {
  foreach ($objects as $key => $object) {
    switch ($object['type']) {
      case 'basic_page':
      case 'landing_page':
        $objects[$key]['type'] = 'Page';
        break;
      case 'article':
      default: break;
     }
   }
}

Add PDF Documents to Search

Let’s say someone wants to add certain PDF documents to the index. How does one filter the documents so only certain ones go into the index? One way to achieve this goal is to set up custom module to select the documents that one wants, create the records, then send the records to Algolia. You can set up a view in Drupal that exports the data that you need. Then use jsondecode to get the data:

      $url = (some url that was output by your view)
      $jsonpdf = file_get_contents($url, false, $context );
      $pdfs = json_decode($jsonpdf);

The next step is to loop through the $pdfs and set up records for each one. Every Algolia record needs an objectID. You can use the media document link set by Drupal for this purpose. The rest of the data is up to you. You could use the media name for the title, or you can set your own titles based on the data. You can learn how to add records using PHP in the Algolia doc Indexing API client methods.