Forum: Ferret Searcher do not work or I do not work

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8b19bf87f6c5ff8a3613704eea16123b?d=identicon&s=25 Ngoc Ngoc (ngoc)
on 2007-02-01 13:57
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
B6a4a3523b3053cb138ee3b986128f15?d=identicon&s=25 Andreas Korth (Guest)
on 2007-02-01 14:27
(Received via mailing list)
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
8b19bf87f6c5ff8a3613704eea16123b?d=identicon&s=25 ngoc (Guest)
on 2007-02-01 15:19
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
>
This topic is locked and can not be replied to.