Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best way to accomplish what you are looking for is to denormalize the content of the replies — and any other fields you'd like to make searchable — into their parent Message.</p> <p>That's pretty straightforward to do in Sunspot. Another common scenario you might research online would be searching for a blog post based on the contents of its comments.</p> <p>One important thing to note here: because of the denormalization, you'll need an <code>after_save</code> hook so that replies can reindex their parent when added or updated.</p> <p>In your case, the changes might look something like this…</p> <pre><code>class Message &lt; ActiveRecord::Base # … after_save :reindex_parent searchable do # … text :replies_content end def replies_content replies.collect(&amp;:content).join(" ") end def reindex_parent parent.solr_index! end end </code></pre> <p>(That <code>text :replies_content</code> could also accept an inline <code>lambda</code> if you want to save a few lines instead of defining a new method. That's up to you.)</p> <p>There is no real change in search syntax with this approach, since all the content of the replies will get lumped in to your default keywords search.</p> <p>If you have more specific use cases in mind, you'll need to clarify your question, but this seems like the best and simplest approach to me.</p> <p>One last note: this approach can be a bit heavy if, for example, your messages have a lot of replies. It's probably a good idea to make sure you're indexing asynchronously using DelayedJob or Resque. But that's a different conversation.</p> <h2>Update 1: Scoping with a certain category_id</h2> <p>First of all, I am assuming that each reply may have a <code>category_id</code> distinct from its parent. And, to re-state, you want to perform keyword matching against the parent <em>or</em> reply text content, and you want to scope by category. </p> <p>You have a couple options that I see. I'll start with the simplest, and then describe a few likely combinations. The simplest approach would be to do a pretty basic search — don't worry about denormalization or any of that — and reconstruct your parent-child messages with ActiveRecord associations.</p> <pre><code>@search = Message.search do keywords params[:q] with(:category_id, params[:category_id]) end @messages = @search.results </code></pre> <p>As you can see, scoping by <code>category_id</code> is pretty straightforward in Sunspot. It may be that this is the bulk of your question and I've just gone and made it more complicated than it has to be :)</p> <p>From there, some of those <code>@messages</code> will be parents, some will be replies. It is certainly within your view's capability to figure out which is which and render accordingly.</p> <pre><code>&lt;% if message.parent %&gt; … </code></pre> <p>There are a few other approaches here depending on the exact nature of your requirements. The above may be good enough, so I won't detail them here. But if you continue to pursue denormalization, you can also include a multi-value integer column for all of a message's replies' <code>category_id</code>s. Something like <code>integer :reply_category_ids, :multi =&gt; true</code>.</p> <p>This latter approach would provide looser matches against the message thread as a whole, which may or may not be worth the complexity of denormalizing, depending on your app. I'll leave the syntax to you, it mostly flows from my previous examples.</p> <p>As you can see, there are a few permutations here, depending on when and where you want to scope against that category. Hopefully my above examples give you enough to go on to figure out the exact specifics for your app.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload