Thinking sphinx get records which did not satisfy a particular condition

79 views
Skip to first unread message

mohitnegi

unread,
Feb 6, 2012, 5:37:41 AM2/6/12
to thinkin...@googlegroups.com
Hi,
I was going through the documentation there i found that if i wish to exclude any particular attribute value then i can pass it as

Model.search :without => {:attribute=>value}

But lets say if i wish do to exclude any field value then is there a way for that.

let say my index has

define_index 
 indexes :status
end

now if i want records where status value is anything other than "inactive", How to get that







Pat Allan

unread,
Feb 6, 2012, 6:20:43 AM2/6/12
to thinkin...@googlegroups.com

This is tricky - you cannot search in Sphinx with *just* a negative term (i.e.: anything where a field is *not* inactive). It requires at least one word to positively match on - so this is fine:
Model.search "foo -bar", :match_mode => :extended

It matches results with the word foo but not the word bar. But Sphinx won't like this:
Model.search "-bar", :match_mode => :extended

So, it may be better, given a status column has a known set of values, to search on all of those values instead:
Model.search :conditions => {:status => "(active | pending)"}

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

mohitnegi

unread,
Feb 6, 2012, 6:36:35 AM2/6/12
to thinkin...@googlegroups.com
got it,
But its like the status field can be NULL also.

So can i provide it in the query like
 Model.search :conditions => {:status => "(active | pending | NULL)"}



Pat Allan

unread,
Feb 6, 2012, 6:40:39 AM2/6/12
to thinkin...@googlegroups.com

NULLs are empty strings as far as Sphinx is concerned - I would recommend not having NULL statuses, whether or not Sphinx is involved - it's a pretty key column to have a value in.

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

mohitnegi

unread,
Feb 6, 2012, 6:48:49 AM2/6/12
to thinkin...@googlegroups.com
Actually this status is from the other associated table. which may or may not have current table data, that's why this field (status) value is NULL for some records.


mohitnegi

unread,
Feb 6, 2012, 6:56:07 AM2/6/12
to thinkin...@googlegroups.com
Is there a workaround for that ?

mohitnegi

unread,
Feb 7, 2012, 2:00:26 AM2/7/12
to thinkin...@googlegroups.com

Hi Pat,


lets say if i make this as an attribute in my index.

Is there a way that i can assign some integer value to all the NULL fields and also edit the existing status to some integer value


has  x(:status),  :as=>:chk_status, :type=>:integer { so before storing will have to edit the status values}
Reply all
Reply to author
Forward
0 new messages