Tore Darell
2006-Jun-27 09:51 UTC
[Ferret-talk] Using QueryParser vs building my own query
Hello all I finally caved in and decided I should build my own query instead of relying on QueryParser to do the job for me, but I''ve hit a strange problem.. Here''s how I build my query: #Main query query = Ferret::Search::BooleanQuery.new #Build query to match types typesquery = Ferret::Search::BooleanQuery.new @selected_types.each{|type| typesquery.add_query( Ferret::Search::TermQuery.new(Ferret::Index::Term.new(''type'', type)), Ferret::Search::BooleanClause::Occur::SHOULD ) } #Add types query to main query query.add_query(typesquery, Ferret::Search::BooleanClause::Occur::MUST) #Build query to match content and title contenttitlequery = Ferret::Search::BooleanQuery.new contenttitlequery.add_query( Ferret::Search::TermQuery.new(Ferret::Index::Term.new(''content'', params[:query])), Ferret::Search::BooleanClause::Occur::SHOULD ) contenttitlequery.add_query( Ferret::Search::TermQuery.new(Ferret::Index::Term.new(''title'', params[:query])), Ferret::Search::BooleanClause::Occur::SHOULD ) #Add content+title query to main query query.add_query(contenttitlequery, Ferret::Search::BooleanClause::Occur::MUST) The problem is that index.search(query) always gives me 0 results. However, if I do index.search(query.to_s) it returns the results I expect.. Here''s an example output of query.to_s: +(type:Foo type:Bar) +(content:baz title:baz) What I''m trying to do is search for items that is of type Foo or Bar and has "baz" in either the content or the title. Does anyone have an idea of what''s going on here? Thanks, Tore -- Posted via http://www.ruby-forum.com/.
David Balmain
2006-Jun-28 14:51 UTC
[Ferret-talk] Using QueryParser vs building my own query
On 6/27/06, Tore Darell <toredarell at gmail.com> wrote:> Hello all > > I finally caved in and decided I should build my own query instead of > relying on QueryParser to do the job for me, but I''ve hit a strange > problem.. > > Here''s how I build my query: > > #Main query > query = Ferret::Search::BooleanQuery.new > > #Build query to match types > typesquery = Ferret::Search::BooleanQuery.new > @selected_types.each{|type| > typesquery.add_query( > Ferret::Search::TermQuery.new(Ferret::Index::Term.new(''type'', > type)), > Ferret::Search::BooleanClause::Occur::SHOULD > ) > } > > #Add types query to main query > query.add_query(typesquery, Ferret::Search::BooleanClause::Occur::MUST) > > #Build query to match content and title > contenttitlequery = Ferret::Search::BooleanQuery.new > contenttitlequery.add_query( > Ferret::Search::TermQuery.new(Ferret::Index::Term.new(''content'', > params[:query])), > Ferret::Search::BooleanClause::Occur::SHOULD > ) > contenttitlequery.add_query( > Ferret::Search::TermQuery.new(Ferret::Index::Term.new(''title'', > params[:query])), > Ferret::Search::BooleanClause::Occur::SHOULD > ) > > #Add content+title query to main query > query.add_query(contenttitlequery, > Ferret::Search::BooleanClause::Occur::MUST) > > > > The problem is that index.search(query) always gives me 0 results. > However, if I do index.search(query.to_s) it returns the results I > expect.. > > Here''s an example output of query.to_s: > > +(type:Foo type:Bar) +(content:baz title:baz) > > What I''m trying to do is search for items that is of type Foo or Bar and > has "baz" in either the content or the title. > > Does anyone have an idea of what''s going on here?You must have used a lowercasing analyzer. You''ll need to lowercase the "Foo" and "Bar". QueryParser will do that for you. An easy fix. Cheers, Dave
Tore Darell
2006-Jun-29 13:23 UTC
[Ferret-talk] Using QueryParser vs building my own query
David Balmain wrote:> On 6/27/06, Tore Darell <toredarell at gmail.com> wrote: >> >> #Add types query to main query >> Ferret::Search::TermQuery.new(Ferret::Index::Term.new(''title'', >> The problem is that index.search(query) always gives me 0 results. >> Does anyone have an idea of what''s going on here? > You must have used a lowercasing analyzer. You''ll need to lowercase > the "Foo" and "Bar". QueryParser will do that for you. An easy fix. > > Cheers, > DaveYou''re right, thanks. For some reason I had set the type field to be tokenised.. Tore -- Posted via http://www.ruby-forum.com/.