Locally, tire and elasticsearch work. However, tire doesn't seem to be working well with Bonsai on Heroku.
The index builds properly, however, Tire is not hitting the index with the search method. When I inspect the search method called on my model (Page), I get the following:
#<Tire::Results::Collection:0x0000000659ab90 @response={"took"=>2, "timed_out"=>false, "_shards"=>{"total"=>1, "successful"=>1, "failed"=>0}, "hits"=>{"total"=>0, "max_score"=>nil, "hits"=>[]}}, @options={:load=>true, :size=>100}, @time=2, @total=0, @facets=nil, @max_score=0.0, @wrapper=Tire::Results::Item>
You can see, Tire isn't hitting the index, almost like it gets lost along the way... the "hits" key returns an empty array.
I can confirm the index exists, and it has been built properly. I'm able to hit it with a curl, and return search results successfully. For example:
curl -XGET 'http://my-url.bonsai.io/sampleapp-production/page/1'
This returns the page object in the "sampleapp-production" index, with the ID "1". Perfect.
However, my rails app is having no luck using tire to do this.
Below is my controller action:
def browse
@user = current_user
if params[:category]
@category = Category.find(params[:category])
@pages = @category.pages
else
@pages = Page.search(params)
end
respond_to do |format|
format.js
format.html
end
end
Below is my model (Page):
include Tire::Model::Search
include Tire::Model::Callbacks
index_name BONSAI_INDEX_NAME
def self.search(params)
tire.search(load: true, page: params[:page], per_page: 100) do
query { string params[:query], default_operator: "AND" } if params[:query].present?
sort { by :created_at, "desc" } if params[:query].blank?
filter :term, :visibility => ['public']
end
end
def to_indexed_json
to_json(:include => [:user])
end
Below is my initializer:
ENV['ELASTICSEARCH_URL'] = ENV['BONSAI_URL']
# Optional, but recommended: use a single index per application per environment
app_name = Rails.application.class.parent_name.underscore.dasherize
app_env = Rails.env
BONSAI_INDEX_NAME = "#{app_name}-#{app_env}"
I'd love any help here.