# Return which field/index a result hit was found in

**URL:** <https://www.ruby-forum.com/t/return-which-field-index-a-result-hit-was-found-in/93100>\
**Category:** Ferret\
**Created:** [April 27, 2007, 3:28pm UTC](https://www.ruby-forum.com/t/return-which-field-index-a-result-hit-was-found-in/93100 "2007-04-27T15:28:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy\_mitlenatch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e0b2c6/32.png) [@andy\_mitlenatch](https://www.ruby-forum.com/u/andy_mitlenatch)\
**Post date:** [April 27, 2007, 3:28pm UTC](https://www.ruby-forum.com/t/return-which-field-index-a-result-hit-was-found-in/93100/1 "2007-04-27T15:28:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![andy\_mitlenatch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e0b2c6/32.png) [@andy\_mitlenatch](https://www.ruby-forum.com/u/andy_mitlenatch)\
**Post date:** [May 2, 2007, 2:07pm UTC](https://www.ruby-forum.com/t/return-which-field-index-a-result-hit-was-found-in/93100/2 "2007-05-02T14:07:14Z")

</div>

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  
removed\_email\_address@domain.invalid | [www.webit.de](http://www.webit.de)

Amtsgericht Dresden | HRB 15422  
GF Sven Haubold, Hagen Malessa
