0
votes

I'm using algolia to search in a rails app using the algoliasearch-rails gem.

In my product model, I have this:

algoliasearch index_name: "Product" do 
  attributes :id, :name, :description, :active?
  ...
end

I'm trying to filter results by a filter called "active?". I'm getting an error back from Algolia when I try to run the search with the parameter included.

{"hits"=>[], "hitsPerPage"=>0, "page"=>0, "facets"=>{}, "error"=>#<Algolia::AlgoliaProtocolError: 400: Cannot POST to https://algolia.net/1/indexes/Product_development/query: {"message":"Unknown parameter: active%3F","status":400}

I can't figure out how to properly reference the parameter. It is working to retrieve the "active?" attribute correctly when I don't filter by active?. I could change the parameter name, but would rather not.

How do I reference an Algolia search attribute that has a question mark in the name for filtering?

1
Are you using ActiveRecord? If so then probably :active? attribute should be just :active because it is how it is named in the database. :active? is just and alias for boolean field provided by ARSlava.K

1 Answers

0
votes

You can't use an attribute with a question mark like that.

As Slava.K mentioned in a comment, if your active? method is an Active Record attribute, using active instead will work just fine.

However, if active? is a method unrelated with Active Record, I suggest adding a custom attribute like so:

class Product < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch do
    attributes :id, :name, :description
    attribute :active do
      active?
    end
  end

  def active?
    ...
  end
end

More details on custom attributes here: https://github.com/algolia/algoliasearch-rails#custom-attribute-definition