Pat, Thank you for all the help my search code now finaly looks ok and works as anticipated, there is ofcourse one more thing I am unable to figure out...
The client has 2 fields in his db active & final_at where active is a bool and final_at is a timestamp to make the search accurate we need to include all the books that are active (simple active => 1) or has a final_at not equal to NULL it is this or statement I am struggling with. Hope you are able to help.. -- Peter On Mon, Apr 20, 2009 at 3:44 PM, Pat Allan <[email protected]> wrote: > What about the following... > filters = {} > filters[:tag_ids] = params[:g] unless params[:g].empty? > > And then :with => filters in your search query? > > -- > Pat > > On 20/04/2009, at 10:55 PM, Peter Tellgren wrote: > > Ahhhh, > thanks that indeed explains what is going on. > > My issue now is how do I solve my :with filter for my tags in the best > way.. > > params[:g] ||= [] > > :with => { tag_ids => params[:g] } does not work when params[:g] is empty > but of course so does > :with_all => { tag_ids => params[:g] } but it is not the way I want it to > work. > > Any ideas ? > > > On Mon, Apr 20, 2009 at 2:24 PM, Pat Allan <[email protected]>wrote: > >> The reason is attribute filters are different to fields. >> For example, :conditions => {:country => "Melbourne"} becomes "@country >> Melbourne" when sent through to Sphinx - as that's how it expects >> field-specific searches. And they get appended to the main query. >> >> Book.search "Discworld", :conditions => {:country => "United Kingdom"} >> # is sent to Sphinx as: >> "Discworld @country United Kingdom" >> >> If you specified nothing for country, then that would become "Discworld >> @country" - and so it has nothing to match on the country field. >> >> Filters are completely separate - they are not part of the query string, >> but sent through to the Sphinx daemon one by one. Nil is treated as 0 by >> Sphinx, and so can be a valid match. >> >> Not the best description I've written, but hopefully it sheds some light. >> >> Cheers >> >> -- >> Pat >> >> On 20/04/2009, at 10:07 PM, Peter Tellgren wrote: >> >> Thanks Pat, >> >> the ||= is indeed cleaner :) >> >> however, I am unsuccessful with building the conditions hash. >> >> Whatever I do I end up with an empty result, so for now I will stick to my >> previous.. >> >> something that confuses me though is, how come :countries work even if the >> params[:c] is empty("") or nil where as my :type makes the search >> return 0 found if the params[:t] is nil or empty? >> >> the same goes for my tags. if I supply no tags but an empty array >> (params[:g] ||= []) I end up with no results.. >> >> all comments are welcome. >> >> >> On Mon, Apr 20, 2009 at 1:38 PM, Pat Allan <[email protected]>wrote: >> >>> Hi Peter >>> You could change it to >>> >>> params[:t] ||= [1,2] >>> >>> Which is slightly cleaner. Looks like I misunderstood the issue the first >>> time around. The best approach is perhaps to build your conditions hash, >>> perhaps as follows: >>> >>> conditions = {:author_country => params[:c]} >>> conditions[:type] = params[:t] if params[:t] >>> >>> Book.search params[:q], conditions, :with => {:tag_ids => params[:g]}, >>> :page => params[:page] >>> >>> You should be able to shift the field weights into your define_index >>> block too, if you want them to *always* apply. >>> >>> set_property :field_weights => { >>> "book_name" => 20, >>> "author_name" => 15, >>> "book_desc" => 15, >>> "author_bio" => 10 >>> } >>> >>> Hope this helps. >>> >>> Cheers >>> >>> -- >>> Pat >>> >>> On 20/04/2009, at 9:32 PM, Peter Tellgren wrote: >>> >>> Hi Pat, >>> >>> Here is my define block >>> >>> define_index do >>> # fields >>> indexes name, :as => :book_name >>> indexes user.freetext, :as => :author_bio >>> indexes content, :as => :book_desc >>> indexes [user.firstname, user.lastname], :as => :author_name >>> indexes user.country, :as => :author_country >>> >>> # attributes >>> has user.taggings.tag(:id), :as => :tag_ids >>> has created_at, :as => :book_created >>> has book_type_id, :as => :type >>> end >>> >>> I actually solved the it with this somewhat ugly metod in my controller >>> >>> params[:t] = [1,2] unless params[:t] >>> >>> which then allowed me to use the following search call: >>> >>> @books = Book.search params[:q], >>> :conditions => { >>> :author_country => params[:c], >>> :type => params[:t] >>> }, >>> :field_weights => { >>> :book_name => 20, >>> :author_name => 15, >>> :book_desc => 15, >>> :author_bio => 10 >>> } >>> :with => { >>> :tag_ids => params[:g] >>> }, >>> :per_page => 20, >>> :page => (params[:page] || 1) >>> >>> If I am doing something very wrong here I am happy to find out :D.. a bit >>> of a newbie regarding search so eager to learn >>> >>> On Mon, Apr 20, 2009 at 1:17 PM, Pat Allan <[email protected]>wrote: >>> >>>> >>>> Hi Peter >>>> >>>> What's your define_index block looking like? You'll probably need an >>>> attribute for book_type_id, and then use :with (although :conditions >>>> will work too, but I recommend keeping filters (for attributes >>>> -- :with) and query arguments (for fields -- :conditions) separate. >>>> >>>> Cheers >>>> >>>> -- >>>> Pat >>>> >>>> On 20/04/2009, at 7:39 PM, peter_tellgren wrote: >>>> >>>> > >>>> > Dear all >>>> > I am struggling a bit here with my advance search form where I want my >>>> > users to be able to find books. >>>> > I have in my search a select with 3 options, All, Paperback and >>>> > Hardcover. >>>> > >>>> > What I got in my db is a column with book_type_id 1 = paperback, 2 = >>>> > Hardcover. >>>> > >>>> > The problem I am having is that my all option is returning no results >>>> > since there are no records with the id = 0. >>>> > >>>> > How do I solve this i my case? >>>> > >>>> > I have tried: >>>> > Book.search params[:q], :conditions => { :type => params[:t] } >>>> > Book.search params[:q], :with => { :type => params[:t] } >>>> > >>>> > with both I get no result when params[:t] is not passed (i.e. all >>>> > types interestin). >>>> > >>>> > grateful for any help. >>>> > > >>>> >>>> >>>> >>>> >>> >>> >>> >>> >>> >>> >>> >> >> >> >> >> >> >> > > > > > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/thinking-sphinx?hl=en -~----------~----~----~----~------~----~------~--~---
