Multiple Filters?

First, I’d like to thank you all for your patience and help on the
“Document Scores” issue :).

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?


Bira

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


I have tried the following with no success:

Search for “frog”

t = TermQuery.new(:content, “frog”)
b = BooleanQuery.new(t,:should)

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)
b2 = BooleanQuery.new
b2.add_query(b1,:must)

merge query and filter

b.add_query(FilterQuery.new(b2),:must)

setup index

index = Index::Index.new(:path => @index_dir)

search

@result = index.search(b, options={})


This last line causes the following error:

ruby(534,0xa06fdfa0) malloc: *** error for object 0x4c1a21: Non-
aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
ruby(534,0xa06fdfa0) malloc: *** error for object 0x7038: Non-aligned
pointer being freed
*** set a breakpoint in malloc_error_break to debug
label:president content:president +
limit10offset0
— Entering query execution —
/Library/Ruby/Gems/1.8/gems/ferret-0.11.6/lib/ferret/index.rb:768:
[BUG] Segmentation fault
ruby 1.8.6 (2007-09-24) [universal-darwin9.0]

Abort trap


???

Benjamin

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