Hi Stan

I would probably pull your other conditions in as attributes, so they can be 
used for filters...

  define_index do
    # existing fields and attributes here
    
    has active, start_date
    has players.id, :as => :player_ids
    has 'CRC32(parent_type)', :as => :parent_type
  end

Note that a NULL parent_type will result in a CRC value of 0... so, our search 
query can be all managed by Sphinx:

  Feature.search "@(title, subtitle, description) '#...@search}'",
    :match_mode => :extended,
    :per_page   => 50,
    :with       => {
      :active      => true,
      :start_date  => Time.at(0).to_date..this_day,
      :player_ids  => session[:hp_video_player_id],
      :parent_type => [0, 'Doctor'.to_crc32]

Mind you, I've not tested this code, but it or something close to it should 
probably work :)

Cheers

-- 
Pat

On 19/11/2010, at 3:47 PM, Stan Shore wrote:

> Thanks Pat.  That did the job.
> 
> Follow-up question.  I'm using search_for_ids to get the ids that match and 
> then inserting that into a sql statement that has some other conditions that 
> further narrow down the list.  I don't want to add the conditions to the 
> index because sometimes I want all of the results or want to restrict the 
> search with different conditions.  The problem is that the result ends up 
> sorted by the default sort on the table rather than in relevance order even 
> though the ids are returned in relevance order.  Is there anything that I can 
> do to retain the relevance order?
> 
> Here is my code:
> 
>     results = Feature.search_for_ids("@(title, subtitle, description) 
> '#...@search}' ", :match_mode => :extended, :max_matches => 50, :per_page => 
> 50).compact.join(',')
>     if results.blank?
>       @features = []
>     else
>       sql = "select distinct"
>       sql += ' f.id, f.title, f.subtitle, f.description'
>       sql += " from features as f, features_players"
>       sql += ' where features_players.feature_id = f.id'
>       sql += " and f.active and f.start_date <= '#{this_day}' and 
> features_players.player_id = #{session[:hp_video_player_id]}"
>       sql += " and (f.parent_type = 'Doctor' or f.parent_type is null)"
>       sql += " and f.id in (#{results})"
>       @features = Feature.find_by_sql(sql)
>     end
> 
> Thanks again,
> 
> Stan
> 
> 
> 
> 
> On Thu, Nov 18, 2010 at 7:37 PM, Pat Allan <[email protected]> wrote:
> Hi Stan
> 
> I think the issue could be that you're requesting the :any match mode, but 
> searching on specific fields requires :extended or :extended2. Indeed, when 
> you use conditions, the match mode defaults to :extended.
> 
> So, try removing your explicit match mode, see if that works a bit better.
> 
> If you want *any* word to match in either of the fields, then what you may 
> want to do is something like the following:
> 
>  Feature.search("@(title, subtitle) #...@search}", :match_mode => :extended, 
> :max_matches => 50, :per_page => 50)
> 
> That will make sure *all* query terms appear in *either* title or subtitle. 
> If you want *any* of the query terms to appear in either title or subtitle, 
> then you're going to need to break down @search and insert |'s (they act as 
> ORs).
> 
>  require 'shellwords' # Part of Ruby Standard Library
>  # ...
>  @search = '(' + Shellwords.split(@search).collect { |word|
>    "\"#{word}\""
>  }.join(" | ") + ')'
> 
> Shellwords respects quotes around multiple words - eg: 'foo "bar baz"' 
> becomes ['foo', 'bar baz'].
> 
> Have a read through the docs for the extended match mode, that should make 
> things a bit clearer.
> http://www.sphinxsearch.com/docs/manual-0.9.9.html#matching-modes
> 
> Cheers
> 
> --
> Pat
> 
> On 19/11/2010, at 12:52 PM, stasch wrote:
> 
> > I have a search box on my site which retrieves model instances that
> > contain the search term in any of three fields.  Here is the code I am
> > using:
> >
> > results = Feature.search(:match_mode => :any, :conditions => { :title
> > => @search, :subtitle => @search, :description =>
> > @search }, :max_matches => 50, :per_page => 50)
> >
> > Where Feature is the name of the model and @search contains the search
> > term.  I am getting rather peculiar results.  If I have multiple
> > matches, I sometimes get an extra record that does not contain the
> > search term in any of the fields.  It is always one of two records
> > which happen to be duplicates of each other.  If I have no matches, I
> > just get one of the extra records.
> >
> > Is my syntax wrong or should I be looking for a corrupt index?  How
> > would I fix the corrupt index if that is what's going on?
> >
> > Thanks for your help
> >
> > Stan
> >
> > --
> > 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.
> 
> 
> 
> -- 
> 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.

Reply via email to