Help setting up Algolia search plugin

I am trying to install the Algolia search plugin (https://github.com/getkirby-plugins/algolia-plugin), but I am running in some issues.

I copied the plugin into site/plugins/algolia
I added the following to my config.php (with the correct name / numbers / etc)

c::set('algolia.app', 'dkslakdlaskd');
c::set('algolia.key', '12wqdasdasda21e1w');
c::set('algolia.index', 'dsadsadas');

I replaced controllers/search.php with the following:

    <?php

return function($site, $pages, $page) {

  $query = get('q');
  $page  = param('page');

  if($query) {
    $results    = algolia()->search($query, $page);
    $pagination = $results->pagination();
  } else {
    $results    = array();
    $pagination = null;
  }

  return compact('results', 'pagination');

};

and site/templates/search.php with the following:

<?php snippet('header') ?>

<form>
  <input type="search" name="q" value="<?php echo esc($results->query()) ?>">
  <input type="submit" value="Search">
</form>

<ul>
  <?php foreach($results as $result): ?>
  <li>
    <a href="<?php echo $result->url() ?>">
      <?php echo html($result->title()) ?>
    </a>
  </li>
  <?php endforeach ?>
</ul>

<?php if($pagination && $pagination->hasPages()): ?>
<nav class="pagination">

  <?php if($pagination->hasNextPage()): ?>
  <a class="next" href="<?php echo $pagination->nextPageURL() ?>">&lsaquo; next page</a>
  <?php endif ?>

  <?php if($pagination->hasPrevPage()): ?>
  <a class="prev" href="<?php echo $pagination->prevPageURL() ?>">previous page &rsaquo;</a>
  <?php endif ?>

</nav>
<?php endif ?>

<?php snippet('footer') ?>

When I try running a search with:

/search?q=keyword

All I obtain is an error saying:

Warning : Division by zero in /kirby/toolkit/lib/pagination.php on line 65

What am I doing wrong?

I think, the problem is not the plugin but your pagination. You use $pagination = $results->pagination() without paginating before.

So it should probably be something like:

if($query) {
    $results    = algolia()->search($query, $page)->paginate(3);
    $pagination = $results->pagination();
  } else { ...

No, the pagination is already done in the plugin. The code example is from the README. :slight_smile:

@gvocale: Is there actually something in your Algolia index? Before searching, you need to run the manual indexing method once (put algolia()->index(); somewhere and then remove it again). I will make that more clear in the plugin docs.

You’re right, I forget to run the manual indexing once.

I did put the following at the bottom of my home.php

<?php algolia()->index(); ?>

When I then load the website, I get the following error:

Fatal error
: Uncaught AlgoliaSearch\AlgoliaException: Method not allowed with this API key in /website/site/plugins/algolia/vendor/algolia-client/src/AlgoliaSearch/Client.php:906 Stack trace: #0 /website/site/plugins/algolia/vendor/algolia-client/src/AlgoliaSearch/Client.php(738): AlgoliaSearch\Client->doRequest(Object(AlgoliaSearch\ClientContext), 'GET', 'HN7AQFHUA7-dsn....', '/1/indexes/TUBE...', NULL, NULL, 2, 30) #1 /website/site/plugins/algolia/vendor/algolia-client/src/AlgoliaSearch/Index.php(694): AlgoliaSearch\Client->request(Object(AlgoliaSearch\ClientContext), 'GET', '/1/indexes/TUBE...', NULL, NULL, Array, 2, 30) #2 /website/site/plugins/algolia/lib/algolia.php(90): AlgoliaSearch\Index->getSettings() #3 /website/site/templates/home.php(12): Kirby\Algolia->index() #4 /website/ in
/website/site/plugins/algolia/vendor/algolia-client/src/AlgoliaSearch/Client.php
on line
906

If I access my algolia dashboard, it says “Your index has no records…” and prompts me to upload a json / csv index, which confirms you theory of my index not being created.

In the “latest operations” it shows my queries, so I guess at least the website is communicating with Algolia.

That error message points out that you are using a read-only API key. Please use the “Write API Key” for the Kirby configuration variable as stated in the plugin documentation.

Thank you! I have changed the key in config.php with the one called “Admin API Key” from the Algolia Dashboard (I think they may have renamed it from Write API Key to Admin API Key).

The error on my page now disappeared, but when I search something, no results gets returned. When I check Algolia Dashboard, it still says that my index has no records in it.

Any idea of what could it be?

I have the following keys in my Algolia Dashboard:

There is both a write key and an admin key, you should use the write key.

Have run run the manual indexing? Search can only work if you have records in the index.

If the pagination is done in the plugin, wouldn’t it make sense to catch the no index error instead of getting a division by zero error?

Yes, indeed. It’s already on my list. :slight_smile:

1 Like

These are the keys showing up in my account

About the manual indexing, I have added <?php algolia()->index(); ?> in my footer.php, opened the website (from localhost) and browsed a couple of pages.

Is there some other action I should do to let the website create and upload the index?

That’s interesting…
The Admin API Key should work fine too, but it generally makes sense to use a key with only the permissions you need as the admin key has way more permissions.
Could you please contact the Algolia support about this? It would interest me as well why you don’t have a write key.

You should not just add it there and browser a couple of pages. Each call to algolia()->index() uses as many Algolia “operations” as you have indexable pages. You have a limited amount of these per month.
So you should call this method only once and then remove it from your footer. From that point, the Panel hooks will index the updated pages automatically when you change the content.

Have you configured the plugin completely? Please see the plugin README for the required steps and follow them carefully. Especially the algolia.templates option seems to be missing.

You can create new, restricted keys by clicking on the “Add new API key options”, you can restrict those new keys to individual operations.

@texnixe The pagination error is now fixed. It was a simple bug with the default value if the results are invalid because of a wrong API key. :slight_smile:
Shouldn’t change anything with the current issue however.

That’s always possible, but as the Write API Key exactly matched the permissions we need for the plugin, it would be great to know why it is not there here. If that key has been removed for new accounts or something, we need to change the plugin docs.

Yes, I just created a new account and don’t have that key either. I get exactly the same screen as @gvocale.

1 Like

I added the algolia.templates configurations and now my index is populated! . I somehow thought the options where optional :blush:

No, it says “Every template that is not in the list will be ignored”. :smiley:
Great that it’s working now.

I am trying to setting up Algolia in a site with 9000 pages. When I run the manual index it returns me the following error

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36864 bytes) in /Users/site/kirby/core/content.php on line 34

Is there something I can do?

I am using the following code to index:

<?php ini_set('max_execution_time', 0); //300 seconds = 5 minutes set_time_limit(0); //If set to zero, no time limit is imposed. algolia()->index() ; ?>

Yes:

ini_set('memory_limit', '512M');

The issue behind the memory issue is that the plugin currently loads every indexable page into memory and sends all data together. I have added a feature request for the plugin to change this. We might consider this if other users have the same problem.