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
--
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.
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>
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.
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.