Hi Alex Smart thinking with the index going on the Campaign model! Sounds like that'll get around the multiple sites/promotions issue nicely.
What does your query look like when you're grouping? And what does your index definition look like in your Campaign model? Also, it's probably worth checking whether there's any warnings in the search - if you're using TS 2.0.2 or 1.4.2: Campaign.search(arguments go here).warning Otherwise, in older releases: Campaign.search(arguments go here).results[:warning] That may hold a clue to the zero results being returned. Cheers -- Pat On 28/01/2011, at 2:17 AM, Alex wrote: > Hi Pat, > > Thanks for your reply. What I've done is to define the index on the > model that provides the :through association, so in my case Campaign. > So, now when I perform a search with a location and keywords from the > Promotion, I get returned the Campaigns that match. As a Campaign > uniquely identifies one Promotion and one Site, I can walk the objects > and show the relevant Promotion/Site information. This is a big step > forward. > > What I was hoping was that I could then group_by the > Campaign.promotion_id, so that if my search returned the same > Promotion but at more than 1 site, the group_by would eliminate the > duplicates. However, when I apply the group_by to my search I get no > rows returned from the search at all. > > I've taken a look at the Sphinx docs for group_by, and I think I've > understood the purpose of the :attr group function, but still can't > understand why it doesn't work. Any ideas why this might be, or a way > to work around the problem? > > Thank you so much again. You've done a fantastic job on Thinking > Sphinx. > > Cheers > Alex > > > > On Jan 26, 10:59 am, Pat Allan <[email protected]> wrote: >> Hi Alex >> >> I've no idea how common what you're trying to do is - it certainly sounds >> reasonable, don't get me wrong. I've just never had someone ask in this >> group how to solve such a scenario. >> >> What I have seen a fair bit of is situations where it's more of a 'many >> franchises across multiple locations' kind of thing, where each location >> belongs to only one franchise. That's what I was thinking of when I first >> proposed a solution - because then you can search on locations and group by >> franchise id to find just the closest of each franchise. >> >> You'll have no problems searching for the closest site (as you've found) - >> but I don't think Sphinx will cut it for what you want with an array of >> lat/lng values for each promotion. You could search for promotions, then >> list sites by distance, but sorting by distance on promotions isn't >> something Sphinx will handle in your setup, I'm pretty sure. >> >> Cheers >> >> -- >> Pat >> >> On 25/01/2011, at 10:39 PM, Alex wrote: >> >> >> >>> Hi Pat, >> >>> I'm surprised to hear this isn't quite a common scenario. Applying the >>> logic to other similar models, for example Products and Stores. A >>> Store has a longitude and latitude, and the store has many Products. >>> Equally a Product can exist in many Stores. If you wanted to find >>> 'widgets' in stores in the London area, wouldn't this be modelled in a >>> similar way to my models described above? >> >>> Somehow I think I must be missing something, if this isn't a scenario >>> you've seen before. >> >>> As always, any insights would be greatly appreciated. >> >>> Thanks again. >>> Alex >> >>> On Jan 24, 11:56 pm, Pat Allan <[email protected]> wrote: >>>> Oh, it's a many-to-many... I didn't spot that the first time around. That >>>> does make things more complicated. I've not seen this scenario before. >> >>>> To be honest, I can't think of a neat solution at the moment - you could >>>> run a search per site using the same params but just for promotions, to >>>> get the relevant promotion per site, but the performance on that would not >>>> be ideal... and then you'll hit situations where for some sites you'll get >>>> more than one promotion matching. >> >>>> Sorry Alex, I can't think of an easy way to do this. >> >>>> -- >>>> Pat >> >>>> On 25/01/2011, at 7:14 AM, Alex wrote: >> >>>>> Hi Pat, >> >>>>> Thank you for replying so promptly, it's really appreciated. >> >>>>> As advised, I've added an index to the Site model, like so: >> >>>>> class Site < ActiveRecord::Base >>>>> acts_as_mappable >> >>>>> # associations >>>>> has_many :campaigns, :dependent => :destroy >>>>> has_many :promotions, :through => :campaigns >> >>>>> define_index do >>>>> indexes promotions.title, :as => :promotions_title >>>>> indexes promotions.details, :as => :promotions_details >>>>> has promotions(:id), :as => :promotions_ids >>>>> has 'RADIANS(sites.lat)', :as => :lat, :type => :float >>>>> has 'RADIANS(sites.lng)', :as => :lng, :type => :float >>>>> set_property :latitude_attr => "lat" >>>>> set_property :longitude_attr => "lng" >>>>> end >>>>> end >> >>>>> Then in my controller I do a Site.search 'widgets', :geo => [@lat, >>>>> @lng], :with => {"@geodist" => 0.0..10} >> >>>>> Now, here is where I get stuck. Sorry for the newb question.... >> >>>>> That returns me an array of Sites. How do I get to the Promotions that >>>>> are associated with those sites in my views? >> >>>>> Thank you again for all your help. >>>>> Alex >> >>>>> On Jan 24, 1:51 pm, Pat Allan <[email protected]> wrote: >>>>>> Hi Alex >> >>>>>> Sphinx doesn't understand arrays of floats - so you can't have multiple >>>>>> lat/lng values for a single record. >> >>>>>> What you're going to need to do is have a search index for your Site >>>>>> model, and search upon that. You can use associations to pull in >>>>>> promotion titles and details, as well as adding a promotion id as an >>>>>> attribute. >> >>>>>> If you want, you could group your Site search results by promotion id, >>>>>> thus ensuring you don't get duplicate promotions (if that's what you'd >>>>>> prefer). >> >>>>>> Let me know if you get stuck. >> >>>>>> Cheers >> >>>>>> -- >>>>>> Pat >> >>>>>> On 25/01/2011, at 12:35 AM, Alex wrote: >> >>>>>>> Hi, >> >>>>>>> I have models set up as follows: >> >>>>>>> class Promotion < ActiveRecord::Base >> >>>>>>> # associations >>>>>>> has_many :campaigns, :dependent => :destroy >>>>>>> has_many :sites, :through => :campaigns >> >>>>>>> define_index do >>>>>>> indexes :title >>>>>>> indexes :details >> >>>>>>> indexes sites.lat, :as => :site_lat >>>>>>> indexes sites.lng, :as => :site_lng >>>>>>> has 'RADIANS(sites.lat)', :as => :lat, :type => :float >>>>>>> has 'RADIANS(sites.lng)', :as => :lng, :type => :float >>>>>>> set_property :latitude_attr => "lat" >>>>>>> set_property :longitude_attr => "lng" >>>>>>> has status, starts_at, expires_at >>>>>>> end >>>>>>> end >> >>>>>>> class Campaign < ActiveRecord::Base >>>>>>> belongs_to :promotion >>>>>>> belongs_to :site >>>>>>> end >> >>>>>>> class Site < ActiveRecord::Base >>>>>>> acts_as_mappable >> >>>>>>> # associations >>>>>>> has_many :campaigns, :dependent => :destroy >>>>>>> has_many :promotions, :through => :campaigns >>>>>>> end >> >>>>>>> When I call Promotion.search I only get results for the promotions >>>>>>> that have a single record in Campaigns. So, If I have a Promotion that >>>>>>> is associated with more than 1 Site, I don't get any results back for >>>>>>> that Promotion. For example, I might have a Promotion with a title of >>>>>>> 'widgets' that is available in Birmingham and London. If I search with >>>>>>> the keyword 'widget' and location 'London', I would expect my search >>>>>>> to return the promotion, but, it doesn't. If I delete from the >>>>>>> Campaigns table the row that associates the promotion to the site in >>>>>>> Birmingham, and search again, the promotion gets returned >>>>>>> successfully. >> >>>>>>> Can anyone advise why this is, and what I need to change about my >>>>>>> search to be able to return Promotions that are associated with more >>>>>>> than one site? >> >>>>>>> Thanks for all your help >>>>>>> Alex >> >>>>>>> -- >>>>>>> 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 >>>>>>> athttp://groups.google.com/group/thinking-sphinx?hl=en. >> >>>>> -- >>>>> 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 >>>>> athttp://groups.google.com/group/thinking-sphinx?hl=en. >> >>> -- >>> 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 >>> athttp://groups.google.com/group/thinking-sphinx?hl=en. > > -- > 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. > -- 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.
