sphinx_scope conditions/with issue

66 views
Skip to first unread message

Claudio Poli

unread,
Aug 21, 2012, 8:59:15 PM8/21/12
to thinkin...@googlegroups.com
Hello,
I have this:

  define_index do
    indexes :name, :sortable => true
    indexes description
    indexes hashtags
    indexes tag(:name), :as => :tag, :sortable => true
    indexes comments.comment, :as => :comment

    has state, :type => :string
    has user_id, created_at, updated_at, tag_id, box_id
    has "RADIANS(latitude)",  :as => :latitude,  :type => :float
    has "RADIANS(longitude)", :as => :longitude, :type => :float

    group_by "latitude", "longitude"
  end

`state` is a string, I'm trying to build a scope that returns items with only two particular states:

  sphinx_scope(:for_public_sphinx) {
    {
      :conditions => {
        :state => ['public', 'on_sale']
      }
    }
  }

controller:

      search_options = { page: params[:page] || 1, per_page: 40, match_mode: :any, star: true }
      ...
      @items = Item.for_public_sphinx.search(params[:q], search_options)

But I can't make it return the correct items, I tried also with `with` instead of conditions, changing the match_mode to extended, use directly state as `indexes` but I get mixed results or sometimes errors.

With the configuration above I get items matching my search query, but also items with state private.

Any pointers are appreciated, thanks.

Pat Allan

unread,
Aug 28, 2012, 2:33:22 PM8/28/12
to thinkin...@googlegroups.com
Hi Claudio

You're using state as a field, but have it listed as an attribute. I know you've tried this, but you'll definitely want to change it to a field. Also, to have the scope return items that are either public or on sale, you've got to use some OR logic manually:

:conditions => {:state => "(public | on_sale)"}

Whenever you use the :conditions option, the match mode is automatically set to :extended, unless you're specifying something yourself - which you have been, so you should change that. The :any match mode applies to all query terms provided and treats field labels as query terms as well - again, not what you want.

Hope this helps.

Cheers

--
Pat

> --
> You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/thinking-sphinx/-/kc2l8R9MflsJ.
> 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.


Reply all
Reply to author
Forward
0 new messages