Acts_as_ferret highlight

I am getting nil returned when doing the following:

r.highlight(@condition, :field => ‘body’)
‘r’ is an instance of the a_a_f model. ‘body’ is a tokenized saved
field.

I am running latest ferret and a_a_f trunk. Am I doing something wrong?

Hi!
On Mon, Sep 25, 2006 at 07:12:59AM +0200, Winton wrote:

I am getting nil returned when doing the following:

r.highlight(@condition, :field => ‘body’)
‘r’ is an instance of the a_a_f model. ‘body’ is a tokenized saved
field.

I am running latest ferret and a_a_f trunk. Am I doing something wrong?

maybe :wink:
you should not use strings for field names any more. Maybe this already
fixes things…

what does happen if you call highlight without the :field option ?

Jens


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

Should I be using symbols then?

When I omit the :field option I get @condition thrown at me (which, btw,
is my query string).

Thanks for your help.

  • Winton

Jens K. wrote:

Hi!
On Mon, Sep 25, 2006 at 07:12:59AM +0200, Winton wrote:

I am getting nil returned when doing the following:

r.highlight(@condition, :field => ‘body’)
‘r’ is an instance of the a_a_f model. ‘body’ is a tokenized saved
field.

I am running latest ferret and a_a_f trunk. Am I doing something wrong?

maybe :wink:
you should not use strings for field names any more. Maybe this already
fixes things…

what does happen if you call highlight without the :field option ?

Jens


webit! Gesellschaft f�r neue Medien mbH www.webit.de
Dipl.-Wirtschaftsingenieur Jens Kr�mer [email protected]
Schnorrstra�e 76 Tel +49 351 46766 0
D-01069 Dresden Fax +49 351 46766 66

With what error message? I’m not sure why that would be happening.

Here’s my dev log:

ActionView::TemplateError (can’t convert Hash into String) on line #46
of app/views/search/index.rhtml:
46: body = r.highlight(@condition, :field => :body)
#{RAILS_ROOT}/vendor/rails/actionpack/lib/action_view/helpers/text_helper.rb:38:in
escape' #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_view/helpers/text_helper.rb:38:inhighlight’
#{RAILS_ROOT}/app/views/search/index.rhtml:46:in
_run_rhtml_search_index' #{RAILS_ROOT}/vendor/plugins/acts_as_ferret/lib/acts_as_ferret.rb:66:inmethod_missing’
#{RAILS_ROOT}/app/views/search/index.rhtml:45:in
_run_rhtml_search_index' #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_view/base.rb:316:incompile_and_render_template’
#{RAILS_ROOT}/vendor/rails/actionpack/lib/action_view/base.rb:292:in
render_template' #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_view/base.rb:251:inrender_file’
#{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/base.rb:726:in
render_file' #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/base.rb:648:inrender_with_no_layout’
#{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/layout.rb:245:in
render_without_benchmark' #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:53:inrender’
/Applications/Locomotive2/Bundles/rails112.locobundle/powerpc/lib/ruby/1.8/benchmark.rb:293:in
measure' #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:53:inrender’
#{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/base.rb:942:in
perform_action_without_filters' #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/filters.rb:368:inperform_action_without_benchmark’
#{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:69:in
perform_action_without_rescue' /Applications/Locomotive2/Bundles/rails112.locobundle/powerpc/lib/ruby/1.8/benchmark.rb:293:inmeasure’
#{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:69:in
perform_action_without_rescue' #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/rescue.rb:82:inperform_action’
#{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/base.rb:408:in
process_without_filters' #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/filters.rb:377:inprocess_without_session_management_support’
#{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/session_management.rb:117:in
process' #{RAILS_ROOT}/vendor/rails/railties/lib/dispatcher.rb:38:indispatch’
#{RAILS_ROOT}/vendor/rails/railties/lib/fcgi_handler.rb:150:in
process_request' #{RAILS_ROOT}/vendor/rails/railties/lib/fcgi_handler.rb:54:inprocess!’
/Applications/Locomotive2/Bundles/rails112.locobundle/powerpc/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/fcgi.rb:600:in
each_cgi' /Applications/Locomotive2/Bundles/rails112.locobundle/powerpc/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/fcgi.rb:597:ineach_cgi’
#{RAILS_ROOT}/vendor/rails/railties/lib/fcgi_handler.rb:53:in
process!' #{RAILS_ROOT}/vendor/rails/railties/lib/fcgi_handler.rb:23:inprocess!’
/Users/Merry/Sites/fall-release/public/dispatch.fcgi:24

@condition = title|body:“test”
Search works fine when I omit calling highlight.

  • Winton

Hi Winton,

On 9/26/06, Winton [email protected] wrote:

Should I be using symbols then?

Yes, although I’m not sure that is the problem in this case. I tried
to write all the code to work with Strings as well as Symbols but all
Ferret’s unit tests use Symbols for field names so String field names
are not supported.

When I omit the :field option I get @condition thrown at me (which, btw,
is my query string).

With what error message? I’m not sure why that would be happening.

Cheers,
Dave

On 9/27/06, Winton [email protected] wrote:

highlight' #{RAILS_ROOT}/app/views/search/index.rhtml:46:in _run_rhtml_search_index’
#{RAILS_ROOT}/vendor/plugins/acts_as_ferret/lib/acts_as_ferret.rb:66:in
`method_missing’

If you look at line 66 in acts_as_ferret it seems that r is actually
an instance of ARFerret::SearchResults which is basically an array of
acts_as_ferret model instances with a total_hits attribute. Try this:

body = r[0].highlight(@condition, :field => :body)

That should fix the problem.

Cheers,
Dave

On 9/27/06, Winton [email protected] wrote:

Sorry for not being clear, r comes from a “for r in @results” iteration.
So r would be an instance of the model.

Thanks for your help, Jens and Dave.

  • Winton

Well, I’m not sure what is going on but it isn’t a problem in
acts_as_ferret or Ferret. The highlight method being called is
ActionView::Helpers::TextHelper#highlight:

  def highlight(text, phrase, highlighter = '<strong

class=“highlight”>\1')
if phrase.blank? then return text end
text.gsub(/(#{Regexp.escape(phrase)})/i, highlighter) unless
text.nil?
end

As you can see, the phrase is supposed to be a String, not a Hash.
That is why the exception is being thrown.

Cheers,
Dave

Sorry for not being clear, r comes from a “for r in @results” iteration.
So r would be an instance of the model.

Thanks for your help, Jens and Dave.

  • Winton

So would this be a rails issue?

Not that I am doubting your expertise, but why would I be getting

#{RAILS_ROOT}/vendor/plugins/acts_as_ferret/lib/acts_as_ferret.rb:66:in
`method_missing’

before it tries the ActionView method?

Thanks again,
Winton

On 9/28/06, Winton [email protected] wrote:

Winton

Hi Winton,

The method_missing gets called when you do “for r in @results”. I
think this actually calls the @results.each method which is then
delegated to its internal array of results.

I couldn’t say whether it was a rails issue or not without looking at
your code. All I know is that Ferret’s highlight method is not being
called.

Cheers,
Dave