konyak
konyak

Reputation: 11706

Sunspot Search with Multiple Types vs. Single Type

What are the pros and cons of Sunspot Search with Multiple Types vs Single Type? What is the preferred way?

I've looked through a lot of docs and can't seem to find the answers.

Example of Multiple Type Search:

Sunspot.search(Post, Comment) do
  with :blog_id, 1
  fulltext 'hello' do
    fields(:comment_body)
  end
end

Example of Single Type Search with association to another Model/Type:

class Post < ActiveRecord::Base
  searchable do
    text :comments do
      comments.map { |comment| comment.body }
    end
end

Post.search do
  fulltext 'hello' do
    fields(:comments)
  end
end

It seems I can achieve the same results using either methods (Multiple Types or Single Type).

Upvotes: 2

Views: 674

Answers (1)

konyak
konyak

Reputation: 11706

After heading down the path of multiple type search, let's see if I can provide some answers to my questions.

Cons of Multiple Type Search

  • What most people want returned is a homogenous list of results/hits to display. When you do Sunspot.search(Type1, Type2, etc) or Sunspot.search which by default searches all Types, then calling .results gets a mix of data types which makes it harder to display in the view.
  • If Post has many Comments and I want to find Posts that have "xyz" in the comments, then Sunspot.search(Comment).results will return a list of comments, not the list of Posts I really want. I can use the post_id in the Comment results/hits to retrieve a list of Posts, but that's a performance hit.
  • It's harder to do a Sunspot.search.hits on multiple types to get a list of common fields to display to the view.
  • According to http://sunspot.github.com/sunspot/rails/docs/index.html, "Sunspot is entirely agnostic about whether searches are for one or more types; the only restriction is that columns used for restriction, ordering, etc. are defined in the same way for all types being searched." The columns have to be defined the sames for all types.

Therefore, Single Type Search with associations and mapping to multiple models, which doesn't have any of these issues and can return the same results easily, is preferred IMHO.

Upvotes: 2

Related Questions