The best way forward on this, I think, is to store the birthday as an attribute, and then when it comes to filtering by age, figure out what the min and max dates of birth are for the corresponding age range, and use that via the :with option.
It's not super elegant, but I think it's more flexible/reliable than storing a computed age via some convoluted SQL snippet in your Sphinx index, which is the other other approach that currently comes to mind. Cheers -- Pat On 14 Aug 2014, at 4:21 am, C Wilson <cwilso...@gmail.com> wrote: > I have Thinking Sphinx setup and I was wondering how can search based on age? > For example I should be able to enter "18-25" and it will show me all users > that fall in that category based on their date of birth attribute from Users > table. > > I have age calculated already based on the users birthday which works perfect > on their profile. > > User.rb: > > def age > now = Time.now.utc.to_date > now.year - birthday.year - ((now.month > birthday.month || (now.month > == birthday.month && now.day >= birthday.day)) ? 0 : 1) > end > > > Search.rb: > > def users > @users ||= find_users > end > > private > > def find_users > users = User.order(:id) > users = users.where(gender: gender) if gender.present? > users = users.where(zip_code: zip_code) if zip_code.present? > users = users.where(children: children) if children.present? > users = users.where(religion: religion) if religion.present? > users = users.where(ethnicity: ethnicity) if ethnicity.present? > if min_age.present? && max_age.present? > min = [ min_age, max_age ].min > max = [ min_age, max_age ].max > min_date = Date.today - min.years > max_date = Date.today - max.years > users = users.where("birthday BETWEEN ? AND ?", max_date, min_date) > users > end > users > end > end > > Indexes: > > ThinkingSphinx::Index.define :user, :with => :active_record do > indexes name, :as => :user, :sortable => true > indexes religion, zip_code, birthday, about_me, career, sexuality, > children, user_smoke, user_drink, gender, ethnicity, education, username > has created_at, updated_at > end > > -- > You received this message because you are subscribed to the Google Groups > "Thinking Sphinx" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to thinking-sphinx+unsubscr...@googlegroups.com. > To post to this group, send email to thinking-sphinx@googlegroups.com. > Visit this group at http://groups.google.com/group/thinking-sphinx. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group. To unsubscribe from this group and stop receiving emails from it, send an email to thinking-sphinx+unsubscr...@googlegroups.com. To post to this group, send email to thinking-sphinx@googlegroups.com. Visit this group at http://groups.google.com/group/thinking-sphinx. For more options, visit https://groups.google.com/d/optout.