[beginner] Search by model id

rails
#1

Hello,

Love algolia as it works great and is so easy to use. However, I am running into a seemingly simple issue where I cannot search by a model id. This is my code:

algoliasearch do
attribute :id, :name
end

ransacker :ransack_algolia, formatter: proc { |value|
resp = Product.algolia_raw_search(value, hitsPerPage: 1000)
return [0] unless resp
resp[‘hits’].size.positive? ? resp[‘hits’].map { |b| b[‘id’] } : [0]
} do |parent|
parent.table[:id]
end

In the filter search box, and put in the id of the record I am looking for, and it’s not returned in the algolia response. Searching by name works great. What am I missing?

#2

Hi @michael.ginalick,

Thanks for contacting Algolia. A few things to look at in your Algolia Dashboard after logging in:

  • id attribute - can you confirm it was successfully indexed and is in your Algolia index records
  • searchable attribute - is the id included in the list of searchableAttributes
  • try the search from the Browse tab - instead of from your Rails app, try running the search directly from the Dashboard Browse tab

In essence, you need to confirm first that the search works in the Dashboard, then this will confirm whether it is perhaps a Rails app issue or not.

I hope this helps get you started. Let us know how it goes.