Searcher do not work or I do not work

Hi.
I want to learn more about ferret. So I downloaded ferret-0.10.14 and
write a simple test script

Only query = TermQuery.new(:content, ‘program’) gives result.
If I change ‘program’ with ‘Good’ or ‘Extra’ -> no result
and searching on (:title, ‘Ruby’) -> no result

Strange, Strange

Here is the script

require ‘rubygems’

require ‘ferret’

include Ferret

include Ferret::Search

include Ferret::Index

index = Index.new(:path => ‘./index’)

index << {:location => ‘here’,

      :title => 'Programming Ruby',

      :content => 'Good excellent program'

      }

index << {:location => ‘local’,

      :title => 'Programming Rubyist',

      :content => 'Extra ordinary program'

      }

index.close()

searcher = Searcher.new(’./index’)

query = TermQuery.new(:content, ‘Good’)

searcher.search_each(query) do |id, score|

doc = searcher[id]

puts “Document #{id} found with a score of #{score}”

puts doc[:content]

end

Thanks Andy
Your code is efficient

docs = index.search(‘Good’)
docs.hits.each do |hit|
puts hit.inspect
puts “Document #{hit.doc} found with a score of #{hit.score}”
puts index[hit.doc][:content]
end

On 01.02.2007, at 13:57, Ngoc Ngoc wrote:

searcher.search_each(query) do |id, score|
doc = searcher[id]
puts “Document #{id} found with a score of #{score}”
puts doc[:content]
end

Try ‘good’ in lowercase and it’ll work.

The reason is that your index converts each word in your document to
lowercase. This is due to the default analyzer used by the index
which happens to be Ferret::Analysis::StandardAnalyzer (see the rdocs
for details).

Because of the way you build your query, this lowercase conversion is
not applied to your query string, hence no match.

The trick is, that your queries need to go through the same analyzer
that is used for indexing in order to get the desired results.

Let me suggest a better way to write this script:

require ‘rubygems’
require ‘ferret’
include Ferret::Index

index = Index.new

index << {
:location => ‘here’,
:title => ‘Programming Ruby’,
:content => ‘Good excellent program’
}

index << {
:location => ‘local’,
:title => ‘Programming Rubyist’,
:content => ‘Extra ordinary program’
}

docs = index.search(‘Good’)
docs.hits.each do |hit|
puts hit.inspect
puts “Document #{hit.doc} found with a score of #{hit.score}”
puts index[hit.doc][:content]
end

As you see, you don’t need to employ a Searcher, nor do you have to
build a TermQuery explicitely. Just call #search on your Index and
everything works as expected (plus you get all the nice features of
Ferret’s query parser).

Since we have used the Index#search method, the search automatically
uses the same analyzer for parsing your query that was used for
indexing the document.

Cheers,
Andy