Rorcraft Blog

Optimising your search results with ferret

Acts_as_ferret is a great plugin for adding search capability to your active records. If you use the default setup, you might find that ferret does not return some expected results. For example, if you have an article:

1
2
3
4
5
6
7
8
9
10
11
12
article: 
  id: 1
  title: this is an article

class Article < ActiveRecord
  
  acts_as_ferret( {:fields => {
    :name => {:boost => 3 },
    :published_at_for_sort => {:index => :untokenized_omit_norms, :term_vector => :no} 
  }, :remote => AAF_REMOTE} )

end

Article.find_by_contents(“article”)

return 1 result

Article.find_by_contents(“this is the article title”)

return 0 result

How to solve this?

Omit Stopword

The default analyser will remove common stop-words like “and”, “the”, “a” and “for”.
You can create a new StandardAnalyzer that doesn’t remove those stopwords.

1
2
3
4
5
6
7
8
class Article < ActiveRecord
  
  acts_as_ferret( {:fields => {
    :name => {:boost => 3 },
    :published_at_for_sort => {:index => :untokenized_omit_norms, :term_vector => :no} 
  }, :remote => AAF_REMOTE} ,  {:analyzer => Ferret::Analysis::StandardAnalyzer.new([nil]) } )

end

PerFieldAnalyzer

Using the way above will include all the stopwords in every field, that is usually not what you want. What you need is to use PerFieldAnalyzer and only omit stopwords for title.

From the mailing list archive, there’s reported problem with the C version of PerFieldAnalayser.
“Thanks to Ben from omdb.org for tracking this down and creating this workaround. You can read more about the issue there: blog.omdb-beta.org/2007/7/29/tracking-down-a-memory-leak-in-ferret-0-11-4”

Save this file as per_field_analaysis.rb in /lib

http://pastie.caboo.se/83194

1
2
3
4
5
6
7
8
9
10
11
class Article < ActiveRecord
  
  pfa = PerFieldAnalyzer.new( Ferret::Analysis::StandardAnalyzer.new )
  pfa[:name] = Ferret::Analysis::StandardAnalyzer.new([])

  acts_as_ferret( {:fields => {
    :name => {:boost => 3 },
    :published_at_for_sort => {:index => :untokenized_omit_norms, :term_vector => :no} 
  }, :remote => AAF_REMOTE} ,  {:analyzer => pfa } )

end

Contact

We love to hear about your web projects.
Email:
Sydney: +61 421 591 943
Hong Kong:+852 6901 2682

Categories