Hi!
On Sat, Mar 01, 2008 at 09:29:39PM -0800, Benjamin A. wrote:
I am having the same problem.
I cannot figure out how to get filters working using options for
Index::Index search.
http://ferret.davebalmain.com/api/classes/Ferret/Index/Index.html#M000022
For example suppose I am trying to run the following query:
Search for “frog” filtering on color=green or color=red
your problem most probably results from improper API usage. That
shouldn’t be a reason for Ferret to segfault, but that’s another story.
The QueryFilter class is a filter, and not a query, and you can’t add
a filter to a boolean query. Instead, build a FilteredQuery to combine
your original query with the filter.
Here’s a snippet that achieves what you want:
require ‘rubygems’
require ‘ferret’
include Ferret
include Ferret::Search
i = I.new
i << {:color => ‘green’, :content => ‘frog’}
i << {:color => ‘red’, :content => ‘frog’}
i << {:color => ‘blue’, :content => ‘frog’}
Search for “frog”
t = TermQuery.new(:content, “frog”)
Filter on color=(green OR red)
t1 = TermQuery.new(:color,“red”)
t2 = TermQuery.new(:color,“green”)
b1 = BooleanQuery.new
b1.add_query(t1,:should)
b1.add_query(t2,:should)
filter = QueryFilter.new(b1)
filtered_query = FilteredQuery.new(t, filter)
puts i.search(filtered_query)
Now, I have a bit of a “noob” question… Is there a way to apply
multiple filters to the same query? For example, I want to apply both
a RangeFilter and a QueryFilter, but from what I’ve seen in the API
docs, the :filter parameter that can be passed to the Searcher accepts
only one filter. It does mention the possibility of applying filters
to each other, but provides no examples.
Is this possible? How can it be done?
Build FilteredQueries like above and chain them together:
i << {:color => ‘red green’, :content => ‘frog’}
Search for “frog”
t = TermQuery.new(:content, “frog”)
Filter on color=red
f1 = QueryFilter.new(TermQuery.new(:color,“red”))
Filter on color=green
f2 = QueryFilter.new(TermQuery.new(:color,“green”))
fq1 = FilteredQuery.new(t, f1)
fq2 = FilteredQuery.new(fq1, f2)
puts i.search(fq2)
Cheers,
Jens
–
Jens Krämer
Finkenlust 14, 06449 Aschersleben, Germany
VAT Id DE251962952
http://www.jkraemer.net/ - Blog
http://www.omdb.org/ - The new free film database