# Help setting up Algolia search plugin

**URL:** <https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430>\
**Category:** Questions\
**Tags:** v2\
**Created:** [June 15, 2016, 11:28pm UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430 "2016-06-15T23:28:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gvocale](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gvocale/32/1277_2.png) [@gvocale](https://forum.getkirby.com/u/gvocale)\
**Post date:** [June 15, 2016, 11:29pm UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/1 "2016-06-15T23:29:00Z")

</div>

I am trying to install the Algolia search plugin ([GitHub - getkirby-v2/algolia-plugin: Algolia Search Plugin for Kirby](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?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [June 16, 2016, 6:07am UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/2 "2016-06-16T06:07:07Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [June 16, 2016, 10:46am UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/3 "2016-06-16T10:46:19Z")

</div>

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

@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.

---

<div class="post-metadata">

**Author:** ![gvocale](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gvocale/32/1277_2.png) [@gvocale](https://forum.getkirby.com/u/gvocale)\
**Post date:** [June 16, 2016, 3:35pm UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/4 "2016-06-16T15:35:17Z")

</div>

> [@lukasbestle](#):
>
> algolia()-\>index();

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

```

---

<div class="post-metadata">

**Author:** ![gvocale](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gvocale/32/1277_2.png) [@gvocale](https://forum.getkirby.com/u/gvocale)\
**Post date:** [June 16, 2016, 3:38pm UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/5 "2016-06-16T15:38:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [June 16, 2016, 6:42pm UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/6 "2016-06-16T18:42:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gvocale](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gvocale/32/1277_2.png) [@gvocale](https://forum.getkirby.com/u/gvocale)\
**Post date:** [June 16, 2016, 8:34pm UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/7 "2016-06-16T20:34:40Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [June 17, 2016, 9:26am UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/8 "2016-06-17T09:26:25Z")

</div>

I have the following keys in my Algolia Dashboard:

 ![](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/4/4345790c75d293c901b90331e3252470ed97e6a5.png)

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.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [June 17, 2016, 9:36am UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/9 "2016-06-17T09:36:59Z")

</div>

> [@lukasbestle](#):
>
> No, the pagination is already done in the plugin.

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?

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [June 17, 2016, 9:37am UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/10 "2016-06-17T09:37:20Z")

</div>

Yes, indeed. It’s already on my list. 🙂

---

<div class="post-metadata">

**Author:** ![gvocale](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gvocale/32/1277_2.png) [@gvocale](https://forum.getkirby.com/u/gvocale)\
**Post date:** [June 17, 2016, 12:42pm UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/11 "2016-06-17T12:42:58Z")

</div>

These are the keys showing up in my account

 ![](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/9/9f40d05dec98ec0bbf3d0c6f93ecfd03477ac2e0.jpg)

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?

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [June 17, 2016, 1:12pm UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/12 "2016-06-17T13:12:57Z")

</div>

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.

> [@gvocale](#):
>
> 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.

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.

> [@gvocale](#):
>
> Is there some other action I should do to let the website create and upload the index?

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.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [June 17, 2016, 1:27pm UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/13 "2016-06-17T13:27:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [June 17, 2016, 1:29pm UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/14 "2016-06-17T13:29:18Z")

</div>

@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. 🙂  
Shouldn’t change anything with the current issue however.

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [June 17, 2016, 1:30pm UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/15 "2016-06-17T13:30:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [June 17, 2016, 1:31pm UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/16 "2016-06-17T13:31:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gvocale](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gvocale/32/1277_2.png) [@gvocale](https://forum.getkirby.com/u/gvocale)\
**Post date:** [June 17, 2016, 9:57pm UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/17 "2016-06-17T21:57:06Z")

</div>

> [@lukasbestle](#):
>
> 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.

I added the algolia.templates configurations and now my index is populated! . I somehow thought the options where _optional_😊

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [June 18, 2016, 10:23am UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/18 "2016-06-18T10:23:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gvocale](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gvocale/32/1277_2.png) [@gvocale](https://forum.getkirby.com/u/gvocale)\
**Post date:** [July 24, 2016, 4:49pm UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/19 "2016-07-24T16:49:52Z")

</div>

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() ; ?>`

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [July 24, 2016, 5:45pm UTC](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430/20 "2016-07-24T17:45:44Z")

</div>

Yes:

```php
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](https://github.com/getkirby-plugins/algolia-plugin/issues/5) to change this. We might consider this if other users have the same problem.

[Next page](https://forum.getkirby.com/t/help-setting-up-algolia-search-plugin/4430.md?page=2)
