# Algolia:initialize not working (rails aborted - don't know how to build task)

**URL:** https://meta.discourse.org/t/algolia-initialize-not-working-rails-aborted-dont-know-how-to-build-task/253293
**Category:** Support
**Tags:** algolia-search
**Created:** [January 30, 2023, 9:54am UTC](https://meta.discourse.org/t/algolia-initialize-not-working-rails-aborted-dont-know-how-to-build-task/253293 "2023-01-30T09:54:08Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![M.Anderson](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@M.Anderson](https://meta.discourse.org/u/M.Anderson)
#### Post date: [January 30, 2023, 9:54am UTC](https://meta.discourse.org/t/algolia-initialize-not-working-rails-aborted-dont-know-how-to-build-task/253293/1 "2023-01-30T09:54:08Z")

</div>

Hi,

_first of all, this is a copy of my post into official algolia-discourse. As told by Discourse-Support, I can now post it here, therefore this is a copy-message._

I would like to setup our discourse with algolia, but it is not working like described on GitHub. I have newest version: 3.1.0.beta1.

To make first init, I open my discourse-docker with

```plaintext
./launcher enter app

```

After that, I tried different commands (bundle exec rails or only rails, also rake), but all results into:

> root@rbx-communtity-service-app:/var/www/discourse# LOAD\_PLUGINS=1 rails algolia:initialize  
> rails aborted!  
> Don’t know how to build task ‘algolia:initialize’ (See the list of available tasks with `rails --tasks`)  
> Did you mean? algolia:reindex

When I run just algolia:reindex, i get the following:

```plaintext
root@rbx-communtity-service-app:/var/www/discourse# LOAD_PLUGINS=1 rake algolia:reindex
Clearing users from Algolia
Pushing users to Algolia
..
Successfully pushed xxx users to Algolia
Clearing tags from Algolia
Pushing tags to Algolia
..
Successfully pushed xxx tags to Algolia
Clearing posts from Algolia
Pushing posts to Algolia
rake aborted!
NoMethodError: undefined method `id' for nil:NilClass
/var/www/discourse/plugins/discourse-algolia/lib/discourse_algolia/post_indexer.rb:66:in `to_object'
/var/www/discourse/plugins/discourse-algolia/lib/discourse_algolia/indexer.rb:23:in `block in process!'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/relation/batches.rb:71:in `each'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/relation/batches.rb:71:in `block in find_each'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/relation/batches.rb:138:in `block in find_in_batches'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/relation/batches.rb:245:in `block in in_batches'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/relation/batches.rb:229:in `loop'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/relation/batches.rb:229:in `in_batches'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/relation/batches.rb:137:in `find_in_batches'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/relation/batches.rb:70:in `find_each'
/var/www/discourse/plugins/discourse-algolia/lib/discourse_algolia/indexer.rb:21:in `process!'
/var/www/discourse/plugins/discourse-algolia/lib/tasks/discourse_algolia.rake:36:in `block in algolia_reindex'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/relation/batches.rb:245:in `block in in_batches'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/relation/batches.rb:229:in `loop'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/relation/batches.rb:229:in `in_batches'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/querying.rb:22:in `in_batches'
/var/www/discourse/plugins/discourse-algolia/lib/tasks/discourse_algolia.rake:33:in `algolia_reindex'
/var/www/discourse/plugins/discourse-algolia/lib/tasks/discourse_algolia.rake:7:in `block in <main>'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/bundler-2.4.1/lib/bundler/cli/exec.rb:58:in `load'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/bundler-2.4.1/lib/bundler/cli/exec.rb:58:in `kernel_load'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/bundler-2.4.1/lib/bundler/cli/exec.rb:23:in `run'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/bundler-2.4.1/lib/bundler/cli.rb:491:in `exec'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/bundler-2.4.1/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/bundler-2.4.1/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/bundler-2.4.1/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/bundler-2.4.1/lib/bundler/cli.rb:34:in `dispatch'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/bundler-2.4.1/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/bundler-2.4.1/lib/bundler/cli.rb:28:in `start'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/bundler-2.4.1/exe/bundle:45:in `block in <top (required)>'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/bundler-2.4.1/lib/bundler/friendly_errors.rb:117:in `with_friendly_errors'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/bundler-2.4.1/exe/bundle:33:in `<top (required)>'
/usr/local/bin/bundle:25:in `load'
/usr/local/bin/bundle:25:in `<main>'
Tasks: TOP => algolia:reindex
(See full trace by running task with --trace)

```

Any idea whats the issue here?

Thanks in advance!  
M.Anderson

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [January 30, 2023, 1:04pm UTC](https://meta.discourse.org/t/algolia-initialize-not-working-rails-aborted-dont-know-how-to-build-task/253293/3 "2023-01-30T13:04:05Z")

</div>

I have installed this on my test site to give it a run-through and I also see a similar thing. Let me see what I can find out. 👍

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [January 30, 2023, 1:37pm UTC](https://meta.discourse.org/t/algolia-initialize-not-working-rails-aborted-dont-know-how-to-build-task/253293/5 "2023-01-30T13:37:25Z")

</div>

Have you set the `algolia_discourse_username` site setting by any chances?

---

<div class="post-metadata">

### Author: ![M.Anderson](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@M.Anderson](https://meta.discourse.org/u/M.Anderson)
#### Post date: [January 30, 2023, 1:42pm UTC](https://meta.discourse.org/t/algolia-initialize-not-working-rails-aborted-dont-know-how-to-build-task/253293/6 "2023-01-30T13:42:29Z")

</div>

It is set to “system”

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [February 14, 2023, 6:45pm UTC](https://meta.discourse.org/t/algolia-initialize-not-working-rails-aborted-dont-know-how-to-build-task/253293/7 "2023-02-14T18:45:33Z")

</div>

`algolia:initialize` does not exist any more. See [FEATURE: Update to latest algolia gem (#33) · discourse/discourse-algolia@9a1f08f · GitHub](https://github.com/discourse/discourse-algolia/commit/9a1f08ff4735a33764cb2e53432ba9f359c5b697#diff-18d29890dc9b35653fcaef050f734d67b0658baddc4116c2f5da8264742c2fd6)

The `README.md` needs an update.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [February 15, 2023, 1:55pm UTC](https://meta.discourse.org/t/algolia-initialize-not-working-rails-aborted-dont-know-how-to-build-task/253293/11 "2023-02-15T13:55:17Z")

</div>

README updated 👍

[https://github.com/discourse/discourse-algolia/commit/0de9b24aa479dc87b8272fc212ad4b3a8d0ad282](https://github.com/discourse/discourse-algolia/commit/0de9b24aa479dc87b8272fc212ad4b3a8d0ad282)

---

<div class="post-metadata">

### Author: ![M.Anderson](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@M.Anderson](https://meta.discourse.org/u/M.Anderson)
#### Post date: [February 24, 2023, 12:50pm UTC](https://meta.discourse.org/t/algolia-initialize-not-working-rails-aborted-dont-know-how-to-build-task/253293/12 "2023-02-24T12:50:15Z")

</div>

The issue regarding reindex is still existent. I made a complete upgrade of discourse, including algolia. Also, I could not yet found a code-change inside algolia-plugin. Are there any plans on working on this?

Right now, only readme is changed, but function is still not working like initially written by me.

If needed, I can send you fresh logs from console.

Thanks,  
M.Anderson

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [February 24, 2023, 1:10pm UTC](https://meta.discourse.org/t/algolia-initialize-not-working-rails-aborted-dont-know-how-to-build-task/253293/13 "2023-02-24T13:10:56Z")

</div>

There was a bug fix earlier today for an issue with reindexing and topic counts:

> [@Algolia plugin broken: undefined method \`topic\_count'](https://meta.discourse.org/t/algolia-plugin-broken-undefined-method-topic-count/255962/3):
>
> Thank you for reporting @RGJ I have a fix for this in [https://github.com/discourse/discourse-algolia/pull/82](https://github.com/discourse/discourse-algolia/pull/82)

On the off-chance, could this have solved your issue too?

---

<div class="post-metadata">

### Author: ![M.Anderson](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@M.Anderson](https://meta.discourse.org/u/M.Anderson)
#### Post date: [February 24, 2023, 2:01pm UTC](https://meta.discourse.org/t/algolia-initialize-not-working-rails-aborted-dont-know-how-to-build-task/253293/14 "2023-02-24T14:01:05Z")

</div>

> [@JammyDodger](#):
>
> On the off-chance, could this have solved your issue too?

Unfortunately no ☹ This fix is already inside my discourse-version. Same issue as before.

> Pushing posts to Algolia  
> rake aborted!  
> NoMethodError: undefined method `id’ for nil:NilClass
> 
> ```
> id: post.topic.id,
> ^^^
> 
> ```
> 
> /var/www/discourse/plugins/discourse-algolia/lib/discourse\_algolia/post\_indexer.rb:66:in `to_object' /var/www/discourse/plugins/discourse-algolia/lib/discourse_algolia/indexer.rb:23:in `block in process!’  
> /var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4.1/lib/active\_record/relation/batches.rb:71:in `each' /var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4.1/lib/active_record/relation/batches.rb:71:in `block in find\_each’  
> /var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4.1/lib/active\_record/relation/batches.rb:138:in `block in find_in_batches' /var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4.1/lib/active_record/relation/batches.rb:245:in `block in in\_batches’  
> /var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4.1/lib/active\_record/relation/batches.rb:229:in `loop' /var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4.1/lib/active_record/relation/batches.rb:229:in `in\_batches’  
> /var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4.1/lib/active\_record/relation/batches.rb:137:in `find_in_batches' /var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4.1/lib/active_record/relation/batches.rb:70:in `find\_each’

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [February 25, 2023, 8:05am UTC](https://meta.discourse.org/t/algolia-initialize-not-working-rails-aborted-dont-know-how-to-build-task/253293/18 "2023-02-25T08:05:00Z")

</div>

I was over optimistically hoping for some ‘magically fixed’ 🙁

I’ve given it another go on my test site and I also hit the same issue again. However, I have managed to find out some more information. 🥳 Apparently we’re both experiencing an issue where we have at least one orphaned post that lacks a topic id, which is throwing off the rake task. The advice is to track those down and delete them, and then the rake reindex can run smoothly.

I haven’t put this into practice for finding the problem post(s) yet so haven’t got a step-by-step, but thought I’d give you a heads up in case this is something you’d like to explore before I get to it. 👍

---

<div class="post-metadata">

### Author: ![kris.kotlarek](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kris.kotlarek/32/176919_2.png) [@kris.kotlarek](https://meta.discourse.org/u/kris.kotlarek)
#### Post date: [February 28, 2023, 11:06pm UTC](https://meta.discourse.org/t/algolia-initialize-not-working-rails-aborted-dont-know-how-to-build-task/253293/20 "2023-02-28T23:06:21Z")

</div>

I am debugging it right now.

Did you change default `algolia_discourse_username` site setting? If yes, please ensure that you are using non-admin user.

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [March 1, 2023, 9:58am UTC](https://meta.discourse.org/t/algolia-initialize-not-working-rails-aborted-dont-know-how-to-build-task/253293/24 "2023-03-01T09:58:17Z")

</div>

I have given this yet another go… 🙂 And I can confirm that removing ‘system’ from the `algolia discourse username` setting and running `rake algolia:reindex` with a dummy user instead worked without error. 👍

I shamefully did not spot the bit in the instructions about:

> It is recommended that you create a dummy separate user who can only see content you consider to be public, and change this value to their username.

And only matched my settings per this instruction:

> Once all of the settings are turned on and populated, the plugin configuration should look like this:
> 
> ![image](https://global.discourse-cdn.com/meta/original/4X/c/3/1/c3129d80a7daec45cca0de2040082a3e2f060613.png)

Personally, (and I may be biased 🙂) I think we could maybe make those instructions a little clearer. 🙂 I would advocate for a section ‘Create your algolia discourse user’ as part of the configuration flow.

---

<div class="post-metadata">

### Author: ![kris.kotlarek](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kris.kotlarek/32/176919_2.png) [@kris.kotlarek](https://meta.discourse.org/u/kris.kotlarek)
#### Post date: [March 2, 2023, 12:08am UTC](https://meta.discourse.org/t/algolia-initialize-not-working-rails-aborted-dont-know-how-to-build-task/253293/25 "2023-03-02T00:08:13Z")

</div>

Actually, in latest PR I removed that option as it is possibly dangerous. When a user with admin access is used, it can show restricted posts in search.

[https://github.com/discourse/discourse-algolia/pull/83](https://github.com/discourse/discourse-algolia/pull/83)

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [March 2, 2023, 3:40am UTC](https://meta.discourse.org/t/algolia-initialize-not-working-rails-aborted-dont-know-how-to-build-task/253293/26 "2023-03-02T03:40:58Z")

</div>

That’s an even better idea. 🙂

---

<div class="post-metadata">

### Author: ![M.Anderson](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@M.Anderson](https://meta.discourse.org/u/M.Anderson)
#### Post date: [March 7, 2023, 8:04am UTC](https://meta.discourse.org/t/algolia-initialize-not-working-rails-aborted-dont-know-how-to-build-task/253293/27 "2023-03-07T08:04:58Z")

</div>

Hi,  
thanks, now it is working 🙂 My three indices are created and sent to algolia!

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [April 6, 2023, 8:05am UTC](https://meta.discourse.org/t/algolia-initialize-not-working-rails-aborted-dont-know-how-to-build-task/253293/28 "2023-04-06T08:05:21Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
