1

I have a model that looks something like this

class Post
  acts_as_tree
end

What I want to do is conduct a search on the contents of an entire thread of posts, but only return the parent post in the results, and paginate these results.

I've read about facets in sunspot/solr, and while this does return the correct results, they cannot be paginated.

Basically, I want to search a set of posts, but group by their parent_id. Is this possible in sunspot/solr?

Adam Singer
  • 2,377
  • 3
  • 18
  • 18

1 Answers1

2

You basically want to search over a field that is composed of all of a thread's child nodes. You can do this with sunspot's block syntax. If the post is a root node, index all of the node's children (you'll have to implement all_children, which shouldn't be too hard).

searchable do
  text :posts do
    all_children.map(&:post_body) unless parent
  end
end

Then search over this "posts" text field. Since you only indexed anything for root nodes, these are the only objects that will be returned in your result set.

Dan Fox
  • 629
  • 4
  • 7
  • 1
    I would also suggest you use acts_as_nested_set instead of acts_as_tree. It can handle the all_children call in a single query, whereas acts_as_tree could take many queries to achieve the same thing. – Dan Fox May 02 '11 at 21:11