Join tables when searching using Sunspot/Solr in Rails 3 - ruby-on-rails

I have an ActiveRecord model Products with associated Suppliers (via belongs_to/has_many association). I am using Sunspot for full-text searching. I make a search with that code:
#search = Products.search do
fulltext params[:search]
end
#products = #search.results
But I'd like to include suppliers too, so every time I call, for example,
#products.first.supplier
it wouldn't make a new request to the database. I tried to use
#search = Products.search(include: :supplier) do
but it didn't help. Is there any possible way to do that in Sunspot?

You can try this
#search = Sunspot.search[Products, Supplier] do
.....
end

Related

Search Model and will_paginate fail

I'm new at the rails world, i am trying to implement will_paginate with my Search model. The Search model is responsible for searching products.
My problem is that i can't figure out how to make will_paginate works with this two models, because the products are displayed by the search model and will_paginate only deals with controllers, i have spent a couple of days trying to make this work but without luck :(
This is my Search.rb
class Search < ActiveRecord::Base
def search_products
products = Product.all
products = products.where(["modelo LIKE ?","%#{modelo}%"]).order(created_at: :desc) if modelo.present?
products = products.where(["preco >= ?",min_price]) if min_price.present?
products = products.where(["preco <= ?",max_price]) if max_price.present?
products = products.where(["troca LIKE ?","%#{troca}%"]) if troca.present?
products = products.where(["estado_id LIKE ?","%#{estado_id}%"])
return products
end
end
Search controller
def show
#search = Search.find(params[:id])
#estado = Estado.all
#products = #search.search_products.paginate(:page => params[:page], :per_page => 2).order('id DESC')
end
and search show
<%= will_paginate #products%>
I think that is missing something ;S Please guys, give me a north of how to solve this problem ;S
ps: To make the Search i followed the Railscast tutorial.

Rails - Sunspot filter models based on params hash

I have a global search which at the moment, searches against 3 models.
#query = Sunspot.search [Person, Property, Expense] do
fulltext params[:search] + "~"
end
In the search form I have a checkbox_tag for each of these.
params[:people]
params[:properties]
params[:expenses]
What I would like to do is only search against the models who's checkbox is checked.
What is the best way to do this?

Group Solr results by class

I'm searching two models with Solr like so:
#query = Sunspot.search Location, Employee do
with(:category_id, params[:category_id]) if params[:category_id].present?
fulltext params[:search]
facet :category_id
order_by(:weight, :desc)
paginate :page => params[:page], :per_page => 10
end
I would like to create two discrete objects from the results, containing matches from both models so I can split them up in the UI.
I'm currently doing:
#results = #query.results.group_by(&:class)
#locations = #results[Location]
#employees = #results[Employee]
but that's not working. #locations and #employees are both empty. What am I missing here? If i debug #results, I see matches and they're already grouped by model. I just want to expose that to my view so I can split up results into hideable/showable containers. Thoughts?
I don't have solr, so I can't assume exactly what #results looks like (I assume it's a hash).
With that assumption, the hash keys are either Symbols or Strings.
So try
#locations = #results[:Location]
#employees = #results[:Employee]
or
#locations = #results["Location"]
#employees = #results["Employee"]
One of the two of those (probably the first) should give you the information in each instance variable. The reason is that ruby will set the hash keys of class names as symbols. If the names were already strings, the keys will be strings.

Tire not working with will_paginate in controller

I'm currently searching two models (Posts and Channels), using Tire and ElasticSearch.
I need to add pagination using will_paginate. The issue is, Tire doesn't appear to support will_paginate in the controller (:page and :per_page don't work...). It turns out, to use pagination with Tire, you have to call it in your model.
The problem with calling Tire in my models is, if I do this, Tire won't let me search my two models (Posts and Channels) at the same time.
So... long story short... if I use Tire in the controller, I can't have pagination but I can search my two models at the same time. If I use Tire in my models, I can't search both of my models at the same time, but I can have pagination.
This just seems dumb. Is there a way around this?
For reference, here's the code from my controller that allows me to search both of my models at the same time:
def browse
#user = current_user
#search_items = Tire.search(['posts_index', 'channels_index'], load: true) do |search|
if params[:query].present?
search.query do |q|
q.string params[:query], default_operator: "AND"
end
end
search.filter :term, :visibility => ['public']
search.sort { by :created_at, "desc" }
end
#results = #search_items.results
end

How to return search results with Sunspot and Solr in rails app?

I am having some trouble with Sunspot and Solr. When I pass a parameter, I'm getting no results. I've included in the comments at 2 points in the code.
I've confirmed that there is also data in the Review table in my db and that the string does occur in the records.
Any advice on how to return the search results?
puts "search query is #{params[:search]}" //returns search query
if params[:search]
#search = Review.search do
fulltext params[:search]
end
puts "search results are #{#search.results}" //returns []
#reviews_search_results = #search.results
end
Get the search result using
#result = #search.results
Take a look at this railscasts tutorial

Resources