Filtering an attribute using a string with an associated Model

34 views
Skip to first unread message

Carson

unread,
Mar 23, 2010, 6:30:54 PM3/23/10
to Thinking Sphinx
Very new to this, so please stick with me. I have 2 models:

User:
id, first_name, last_name, created_at

define_index do
indexes user.first_name, user.last_name
end

Post:
id, user_id, title, post, created_at

define_index do
indexes title, post

has user(:id), :as => :author

set_property :enable_star => 1
set_property :min_prefix_len => 3
end

I'm trying to search all Posts with "page" in the title, that have
been authored by a certain user. I can get it to work by passing in
the ID of a user with conditions on the search. But how can I do it
by passing in a string of the first_name of the user? Is it possible?

Thanks in advance.


following is the actual search, that uses the user_id:

@per_page = 20

conditions = Hash.new

if params[:start_date] != nil && params[:end_date] != nil
start_date = Time.parse(params[:start_date])
end_date = Time.parse(params[:end_date])
conditions.merge!({ :created_at => start_date..end_date })
end

authors = Array.new
authors.push(16)

conditions.merge!({:author => authors})

@classes.push(User)
@classes.push(Post)

if @classes.count > 0
@results = ThinkingSphinx::Search.search '*' + params[:query]
+ '*', :with => conditions, :classes => @classes, :index_weights =>
{ School => 10, User => 10, UserWallpost => 70, GroupDiscussion => 90,
GroupDiscussionComment => 80, Item => 100 }, :retry_stale =>
true, :per_page => @per_page, :page => params[:page]
end

Amir Yalon

unread,
Mar 24, 2010, 4:52:36 AM3/24/10
to thinkin...@googlegroups.com
In stead of:
  has user(:id), :as => :author
try to declare:
  indexes user.first_name, :as => :author
and adjust your query accordingly.


--
You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.
To post to this group, send email to thinkin...@googlegroups.com.
To unsubscribe from this group, send email to thinking-sphi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/thinking-sphinx?hl=en.


Carson

unread,
Mar 24, 2010, 12:31:39 PM3/24/10
to Thinking Sphinx
That will allow me to search for user.first_name, and works, but what
about searching for that plus text from a post.

In other words, I want all posts by an author with the first name
'carson' that has a post title 'whatever'

Thanks for the help!

> > thinking-sphi...@googlegroups.com<thinking-sphinx%2Bunsubscribe@ googlegroups.com>

Pat Allan

unread,
Mar 28, 2010, 12:54:24 AM3/28/10
to thinkin...@googlegroups.com
Hi Carson

You can search on all fields and specific fields at once:

Post.search 'whatever', :conditions => {:user => 'Carson'}

Or even:

Post.search :conditions => {:user => 'Carson', :title => 'Whatever'}

Hope this helps

--
Pat

> To unsubscribe from this group, send email to thinking-sphi...@googlegroups.com.

Johann Tagle

unread,
Nov 28, 2012, 2:55:22 AM11/28/12
to thinkin...@googlegroups.com
Hi.  I'm just wondering, since this thread is more than two years old, if this is still the way to filter by a string from an associated model, or if there is already a way to compute a string ordinal for it.  Thanks.

Johann

Pat Allan

unread,
Nov 28, 2012, 6:39:47 AM11/28/12
to thinkin...@googlegroups.com
Hi Johann

If it needs to be an attribute filter, rather than the previous examples using :conditions on a field, then you should read through this item in the docs:
http://pat.github.com/ts/en/common_issues.html#string_filters

But I think using :conditions and fields is perhaps the way to go. These approaches are valid whether it's via an association or not. The only tricky part is matching on exact values from a field in a has_many association - all values get combined together.

--
Pat

> To view this discussion on the web visit https://groups.google.com/d/msg/thinking-sphinx/-/vKDcNSz-c70J.

Johann Vincent Paul Tagle

unread,
Nov 28, 2012, 7:49:14 AM11/28/12
to thinkin...@googlegroups.com
Hi Pat,

Thanks.  Yes I saw that part of the documentation, and have used that for strings within the same model.  However I could not find any way to apply it for a string in an associated model.  So I'll just stick to the :conditions.  Good thing the association in question is just a belongs_to so it's quite simple -- just feels overkill because the string was really for gender - "M" and "F".

Thanks again!

Johann
Reply all
Reply to author
Forward
0 new messages