Excerpts with indexed has_many fields

10 views
Skip to first unread message

Chris Roos

unread,
Nov 19, 2009, 7:19:53 AM11/19/09
to Thinking Sphinx
Given this (fictional) setup.

class Folder < ActiveRecord::Base
# Fields: id, name
has_many :files
define_index do
indexes name
indexes files.name, :as => :file_names
end
end
class File < ActiveRecord::Base
# Fields: id, folder_id, name
belongs_to :folder
end

irb> folder = Folder.create!(:name => 'no-match')
irb> folder.files.create!(:name => 'matching-text')

Given that I've indexed, I can issue these queries to return the
matching folder object:

irb> ThinkingSphinx.search 'matching-text'
irb> Folder.search 'matching-text'

How would I go about displaying an excerpt in this case? The
#excerpts method just returns the folder object with the name field
html escaped. I'd expect (although I'm new to thinking_sphinx/sphinx
so this might not be possible) to be able to use something like
result.excerpts.file_names (i.e. the alias in Folder.define_index) to
get something that I could display in my search results. Should I be
doing something else or is this not possible at the moment?

Cheers, Chris

Pat Allan

unread,
Nov 25, 2009, 12:08:20 AM11/25/09
to thinkin...@googlegroups.com
Hi Chris

Unfortunately the excerpts object only ties into the underlying model,
and doesn't add shortcuts to the defined fields.

A work around could be to add a file_names method in your model:
def file_names
files.collect { |file| file.name }.join(" ")
end

And then result.excerpts.file_name will work.

--
Pat
> --
>
> You received this message because you are subscribed to the Google
> Groups "Thinking Sphinx" group.
> To post to this group, send email to thinkin...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/thinking-sphinx?hl=
> .
>
>

Reply all
Reply to author
Forward
0 new messages