Forum: Ferret Which method to use to get content from index with a_a_f?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mischa B. (Guest)
on 2007-02-07 00:22
Hi everybody,

After staring at the a_a_f API for quite sometime now, I decided it's
time to ask...

Which method should I use to get content from the index without using
highlight? Consider the following controller action:

def preview
  if params[:search].blank? # normal case
    @text = @myfile. # which method do I use here to get the :text from
the index???
  else # if we come from the search results page
    @text = @myfile.highlight(params[:search], { :field => :text,
:excerpt_length => :all, :pre_tag => '[highlight]', :post_tag =>
'[/highlight]' })
  end
end

I didn't store my text in the database, only in the index. When I'm
coming from a search I use the highlight method, so the term I searched
for gets highlighted, but how do I get the text from the index in a
'normal case'. The highlight method feels inappropriate, because I don't
want to highlight anything. I don't see what other method to use.

Thanks in advance!

Mischa.
Jens K. (Guest)
on 2007-02-07 12:55
(Received via mailing list)
On Tue, Feb 06, 2007 at 11:22:39PM +0100, Mischa B. wrote:
>     @text = @myfile. # which method do I use here to get the :text from
> for gets highlighted, but how do I get the text from the index in a
> 'normal case'. The highlight method feels inappropriate, because I don't
> want to highlight anything. I don't see what other method to use.

that's because there is currently no aaf way to do this.

You can however get a handle to the Ferret::Index instance by calling
YourModel.ferret_index (or YourModel.aaf_index.ferret_index if you're
using the latest aaf trunk). Now you can query this index instance for
your primary key with something in the lines of "id:#{self.id}", and
then
retrieve field values via the standard ferret api.

Note that this won't work with a remote index, since the index in this
case is not necessarily on the same machine and therefore cannot be
accessed via Ferret's own API.

I already started thinking about a way to integrate this into the API,
maybe via an option to find_by_contents that allows to specify the
fields you want to fetch directly from the index. That could then be
combined with lazy-loading of the 'real' record from DB for more speed
when it comes to live-searches and the like.

Jens



--
webit! Gesellschaft für neue Medien mbH          www.webit.de
Dipl.-Wirtschaftsingenieur Jens Krämer 
removed_email_address@domain.invalid
Schnorrstraße 76                         Tel +49 351 46766  0
D-01069 Dresden                          Fax +49 351 46766 66
Mischa B. (Guest)
on 2007-02-07 22:44
Thanks for the tip! I expected aaf to have a method for this, but the
way you describe works fine.

I'm doing it like this now:
@text = Myfile.ferret_index[@myfile.document_number][:text]
This topic is locked and can not be replied to.