Return which field/index a result hit was found in

Hello!

This forum is an extremely valuable resource! Thanks to everyone who has
contributed, especially Jens and Dave. I hope I’ll be able to give
something
back to the community soon!

I have a model, ‘Book’, that has associations to several other models,
comments, tags, authors, etc… I have implemented acts_as_ferret to
search
the Book model and the associations as follows:

class Book < ActiveRecord::Base


#asociations
has_many :details
has_many :authors etc…

acts as ferret implementation

acts_as_ferret :fields => [:title, :tag_list, :comments_list,
:details_value_list, :details_title_list, :author_names]

def details_value_list
  return self.details.collect do |detail|
    detail.value
  end.join(",")
end

def details_title_list
  return self.details.collect do |detail|
    detail.title
  end.join(",")
end

def details_text
  return self.details.collect do |detail|
    detail.text_title + detail.text_value
    end.join(",")
end

def author_names
  return self.authors.collect do |authors|
    authors.name
    end.join (",")
end

I can successfully search the Book model and all of its associations.
Next,
I would like to print out in which field/association the hit occurred.
For
example, I would like to print to the user whether the hit was in the
book
title, a tag, in a comment or in the collection of authors, as I am only
printing the title of the book and some higher level details in the
search
results.

Does anyone have suggestions? I have scoured the internet to no
avail…maybe I am not searching with the right terms?

Thank you kindly for any help,
Andy

On Fri, Apr 27, 2007 at 09:27:48AM -0400, andy mitlenatch wrote:

Hello!

[…]

I have a model, ‘Book’, that has associations to several other models,
comments, tags, authors, etc… I have implemented acts_as_ferret to search
the Book model and the associations as follows:

[…]

I can successfully search the Book model and all of its associations. Next,
I would like to print out in which field/association the hit occurred. For
example, I would like to print to the user whether the hit was in the book
title, a tag, in a comment or in the collection of authors, as I am only
printing the title of the book and some higher level details in the search
results.

The information in which field a hit occured is not available from
Ferret results directly, so to find this out you’d have to re-run the
query against each of the fields in question for each record in your
result set (scope your search to exactly this record by using
“id:#{record.id}” as a part of your query).

Not a good solution, I must admit - might be easier (and faster) to run
separate queries for each field in the beginning and then manually merge
these results, preserving the information which field had the hit.

But beware of duplicates because of records having matches in multiple
fields. Also such records would score better if you only ran one query
across
all fields - so maybe you would want to still do that to get exact
scores for sorting the results by relevancy…

I feel like there should be an easier way to do this, maybe somebody
else has a better idea?

Jens


Jens Krämer
webit! Gesellschaft für neue Medien mbH
Schnorrstraße 76 | 01069 Dresden
Telefon +49 351 46766-0 | Telefax +49 351 46766-66
[email protected] | www.webit.de

Amtsgericht Dresden | HRB 15422
GF Sven Haubold, Hagen Malessa