How can I display results for a Rails SOLR multimo

2019-08-12 17:46发布

问题:

In my rails controller i write

@all = Sunspot.search(Car, Article, Organization) do 
      fulltext params[:search]
    end
    @all_res = @all.results

and then i have this data:

[#<Car id: 25, vehicle_manufacturer_id: 2, vehicle_model_id: 2, price: 0, year: 1900, eng_capacity: 1, fuel_type_id: 2, gearbox_id: 2, wheeldrive_id: 2, is_not_rus: false, color_id: 3, vehicle_registration_id: 2, condition_id: 1, body_type_id: 3, is_exchangeable: false, is_not_sell: nil, is_right_wheel: false, is_without_run_in_russia: false, is_gt: false, is_new: false, is_hybrid: nil, has_ac: false, has_leather: false, has_alloy_wheels: false, has_el_packet: false, has_signalization: false, user_id: 3, description: "Кароче типо крутая тачка", created_at: "2014-03-18 18:20:51", updated_at: "2014-03-18 20:29:32", vehicle_mileage: 0, contact_name: "df sdf ", city_id: 3, telephone: "123123", is_vip: nil>, #<Article id: 6, meta_description: "dfsdf", meta_keywords: "dssd", title: "sdffsdf", tags: "1", intro_text: "sdfsdf", content: "sfsdf sdf sdf ds \r\nкрутая", author_id: 1, articles_type_id: nil, published: true, publishing_start: "2014-02-25 18:02:00", publishing_end: "2014-02-25 18:02:00", is_advert: true, created_at: "2014-02-25 18:03:07", updated_at: "2014-02-25 18:03:07", article_image_file_name: nil, article_image_content_type: nil, article_image_file_size: nil, article_image_updated_at: nil, rostov: nil, krasnodar: nil, stavropol: nil>]

but how could i display pretty this?

for example if car, then view some field, if article than other field in view search results? also maybe is it real to show - where this word was finded?

回答1:

Try this, it may suitable in your case

@all.results.each do |result|
  case result.class.name
  when 'Car'
    <%= result.vehicle_manufacturer_id %>
    <%= result.vehicle_model_id %>
  when 'Article'
    <%= result.meta_description %>
    <%= result.meta_keywords %>
 end
end


回答2:

Since you don't have any of main fields in common between the Car and Article models, you should separate the searches. This way, it allows you to later add more criteria to narrow down the results easily.

Let's assume you want to search for cars and articles, for easier explanation.

You could have a page that searches for cars, and another page that searches for articles. Or if you want, have one page with one search box and have the controller's action calls two separate search methods. You'll then store the two results of the searches in two instance variables. In the view, you could have two results sections, one for cars and one for articles, looping through each instance variable separately.

The Searcher would be something like:

#app/models/searcher.rb
class Searcher
  def self.search_cars(term)
    Car.search do
      fulltext term
    end
  end

  def self.search_articles(term)
    Article.search do
      fulltext term
    end
  end
end


回答3:

Using Rails' partial rendering (specifically, rendering collections) you could try something like this in your view:

<%= render @all.results %>

By default it will look for partial based on the class of each record in the collection. Your results array included Car and Article objects, so the render method will look for a cars/_car and articles/_article partial.

If you want to make views that are specific to just search results (e.g., the cars/_cars partial is to be used elsewhere for another purpose), you can create additional partials within the views directory corresponding with your search controller.

Let's say your controller is SearchResultsController and the corresponding views directory is search_results. You can create the following partials in /app/views/search_results/:

_car.html.erb
_article.html.erb
_organization.html.erb

Then in the view where your results are to be displayed you could do something like the following:

<% @all.results.each do |result| %>
  <%= render partial: "search_results/#{result.class.to_s.underscore}", object: result %>
<% end %>

With the object option, Rails will assign that object to a local variable matching the name of the partial. For example, your first result is a Car object. This code will look for a search_results/_car partial and assign that Car record to a car variable which can be used in the partial:

# /app/views/search_results/_car.html.erb
<%= content_tag_for(:article, car) do %>
  <%= debug car %>
<% end %>  


回答4:

We can also use is_a? method to check model and render corrosponding partial for each models.

<%= @all_res.each do |result| %>
  <% if result.is_a?(Car) %>
    <%= render 'car', car: result %>
  <% elsif result.is_a?(Article) %>
    <%= render 'article', article: result %>
  <% elsif result.is_a?(Organization) %>
    <%= render 'organization', organization: result %>
  <% end %>
<% end %>

know I am late but this might be helpful :)