Using highlight from aaf

Hi,

I’m trying to use highlight ferret method with trunk aaf and 0.10.1
ferret.
In my search display I use:

Myindexedclass.ferret_index.searcher.highlight(@query, result_line.id,
:content)

  • searcher is a protected method; how can I access to the searcher from
    aaf ?
  • is the doc id in aaf the same as my model id ?
  • is the first param, query, the string query or the query object ?

Jean-Christophe M.

Better Nested Set for rails:
http://opensource.symetrie.com/trac/better_nested_set

On 9/3/06, Jean-Christophe M. [email protected] wrote:

aaf ?
I’ve added a highlight method to Ferret::Index::Index so you’ll be
able to use it now.

  • is the doc id in aaf the same as my model id ?

No, but (I’m pretty sure) the model id is automatically stored in the
:id field in each document.

  • is the first param, query, the string query or the query object ?

As with all Index methods, Index#highlight takes a string or a Query
object. The Searcher#highlight method however only takes Query
objects. Note also that you must specify the field to highlight when
using Searcher#highlight however the default_field is used for
Index#highlight unless specified otherwise.

Cheers,
Dave

On 9/4/06, Jean-Christophe M. [email protected] wrote:


Jean-Christophe M.

Try this;

index.search_each(query) do |doc_id, score|
    puts index.highlight(query, doc_id, :field => :my_field).join(", 

")
end

Also, I think you should be doing something like this to get the
resulting object from the database;

MyModel.find index[doc_id][:id]

Hope that helps,
Dave

Hi Dave,

Le 3 sept. 06, à 15:26, David B. a écrit :

I’ve added a highlight method to Ferret::Index::Index so you’ll be
able to use it now.

Thanks. Trying to use this, I updated to 0.10.2 gem.
But I cannot get highlight return something else than nil.
I suspect highly the doc id not always being my indexed class id,
though aaf code seems to create docs with Model.id :confused:

I tried to do
i.search ‘rare_word’
Mymodel.find result-id # contains no such word
i.highlight(‘rare_word’, result-id, :field => :myfield)

But if I try to pass a model id containing this word for sure, I still
get nil result :confused:
i.highlight(‘rare_word’, model-id, :field => :myfield)

I’ll try to turn this into a test case in aaf.


Jean-Christophe M.

On Sun, Sep 03, 2006 at 10:30:24PM +0200, Jean-Christophe M. wrote:

Hi Dave,

Le 3 sept. 06, à 15:26, David B. a écrit :

I’ve added a highlight method to Ferret::Index::Index so you’ll be
able to use it now.

Thanks. Trying to use this, I updated to 0.10.2 gem.
But I cannot get highlight return something else than nil.
I suspect highly the doc id not always being my indexed class id,
though aaf code seems to create docs with Model.id :confused:

the ferret document id is not your Model primary key id. retrieving
the doc id from a given primary key isn’t that easy atm, since aaf’s
find_by_contents returns model instances and no doc ids.

Model.ferret_index.search(“id:#{model.id}”)
should give you access to the ferret search results and therefore the
document ids.

I’ll add better support for this to aaf soon. I’ve been thinking about
something like
model_instance.highlight(‘rare_word’, :field => :my_field)

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

Hi,

Le 4 sept. 06, à 01:48, David B. a écrit :

index.search_each(query) do |doc_id, score|
    puts index.highlight(query, doc_id, :field => 

:my_field).join(", ")
end

Tried it, I get only nil for index.highlight :confused:


Jean-Christophe M.

On 9/5/06, Jean-Christophe M. [email protected] wrote:

Hi,

Le 4 sept. 06, à 01:48, David B. a écrit :

index.search_each(query) do |doc_id, score|
    puts index.highlight(query, doc_id, :field =>

:my_field).join(", ")
end

Tried it, I get only nil for index.highlight :confused:

Can you give me an example of what doesn’t work? Something like this;

require 'rubygems'
require 'ferret'

i = Ferret::I.new(:default_field => :content)
i << {:content => "here is the content I want to highlight."}

puts i.highlight("content", 0)

Cheers,
Dave

Hi,

Le 5 sept. 06, à 01:18, David B. a écrit :

Can you give me an example of what doesn’t work? Something like this;

require 'rubygems'
require 'ferret'

i = Ferret::I.new(:default_field => :content)
i << {:content => "here is the content I want to highlight."}

puts i.highlight("content", 0)

In fact this example work. The difference here is that doc is not
stored in the index though aaf probably. It’s probably the reason why I
don’t have a result: even Myclass.ferret_index.doc(12) returns {}.


Jean-Christophe M.

HI Dave,

Le 5 sept. 06, à 02:24, David B. a écrit :

Ahhhh, of course. Sorry. Jens mentioned that yesterday so I should
have realized. You need to store the field as well as its term vector
:with_positions_offsets if you want to highlight it. The :term_vector
setting is :with_positions_offsets by default in aaf so you only need
to change the :store setting for the field you want to highlight.

I’m not convinced about storing everything once more whereas I already
store the texts in db. More, I don’t know how to do it in aaf :wink:

By the way, Myclass.ferret_index.doc(12) will always return {}. The
documents are lazy loading now so Myclass.ferret_index.doc(12)[:id]
will return the model ID. You can load all fields with the load
method. Try;

puts Myclass.ferret_index.doc(12).load().inspect()

That should show you which fields are actually stored which in the
case of acts_as_ferret will only be the model ID (I think??).

You are right. Anyway, I stick to my own ruby highlighting atm.

Jean-Christophe M.

On 9/5/06, Jean-Christophe M. [email protected] wrote:

i << {:content => "here is the content I want to highlight."}

puts i.highlight("content", 0)

In fact this example work. The difference here is that doc is not
stored in the index though aaf probably. It’s probably the reason why I
don’t have a result: even Myclass.ferret_index.doc(12) returns {}.

Ahhhh, of course. Sorry. Jens mentioned that yesterday so I should
have realized. You need to store the field as well as its term vector
:with_positions_offsets if you want to highlight it. The :term_vector
setting is :with_positions_offsets by default in aaf so you only need
to change the :store setting for the field you want to highlight.

By the way, Myclass.ferret_index.doc(12) will always return {}. The
documents are lazy loading now so Myclass.ferret_index.doc(12)[:id]
will return the model ID. You can load all fields with the load
method. Try;

puts Myclass.ferret_index.doc(12).load().inspect()

That should show you which fields are actually stored which in the
case of acts_as_ferret will only be the model ID (I think??).

Cheers,
Dave