Or operator with a :with

5 views
Skip to first unread message

pmlarocque

unread,
Nov 24, 2009, 4:51:14 PM11/24/09
to Thinking Sphinx
Hello,

Can't figure out to query the following :

MyModel.search "" , :conditions => { :level => option1, :level =>
option2 }, :with => {:option_spec_id => option2.spec.id }

The point is option1 and option2 are mutually exclusive by default. I
want everything that correspond to option1 and the one that correspond
to options 2 but only for the given spec.


Hope I was clear enough!

Thanks!

pmlarocque

unread,
Nov 24, 2009, 11:56:08 PM11/24/09
to Thinking Sphinx
found out that it's not possible, what about a or
between :conditions ... ?

Thanks

Pat Allan

unread,
Nov 25, 2009, 12:09:38 AM11/25/09
to thinkin...@googlegroups.com
Unfortunately Sphinx's logic with multiple filters is AND - there's no
way to switch it to OR. The only way around this is to combine both
attributes into a single one (if that's possible in your scenario).

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

Pat Allan

unread,
Nov 25, 2009, 12:11:23 AM11/25/09
to thinkin...@googlegroups.com
I think you can manually combine conditions, perhaps something like
the following:

Model.search "(@foo alpha) | (@bar beta)", :match_mode => :extended

Keep in mind that the following two lines are exactly the same:
Model.search :conditions => {:foo => "alpha"}
Model.search "@foo alpha", :match_mode => :extended

--
Pat

pmlarocque

unread,
Nov 25, 2009, 12:23:36 AM11/25/09
to Thinking Sphinx
Thanks!

Is there a way to do something like.

Model.search "", :conditions => { :field1 => "foo", :field2 => "bar"}

And the evaluation between both condition will be a OR, maybe my test
aren't ok but I feel it is a AND.

pmlarocque

Pat Allan

unread,
Nov 25, 2009, 12:30:20 AM11/25/09
to thinkin...@googlegroups.com
That's correct, Sphinx implicitly uses ANDs. The only way to get an OR
is using the manual syntax I mentioned in the previous email - I
realise it's not ideal, but at the moment, that's as neat as it's
going to get.

--
Pat

pmlarocque

unread,
Nov 25, 2009, 12:33:14 AM11/25/09
to Thinking Sphinx
Ok thanks for your time!

pmlarocque

unread,
Nov 25, 2009, 12:50:31 AM11/25/09
to Thinking Sphinx
hmmm trying this as suggested :

Item.search "(@source_id #{sources}) | (@level #{level}) |
(@specialty_id #{specialty.id})", :match_mode => :extended

note that the sources is a string like "1 | 2 | 3"

I get the following error :

index information_item_core: query error: field specification is only
allowed at top level

Looking in sphinx docs...

Thanks

pmlarocque

unread,
Nov 25, 2009, 12:57:21 AM11/25/09
to Thinking Sphinx
hmmm my guess is because source_id and level are defined via
item.source(:id) and item.source.level.name.

Am I right? Any workaround ?

Thanks

PML

James Healy

unread,
Nov 25, 2009, 12:59:54 AM11/25/09
to thinkin...@googlegroups.com
pmlarocque wrote:
> hmmm my guess is because source_id and level are defined via
> item.source(:id) and item.source.level.name.
>
> Am I right? Any workaround ?

It sounds like sphinx doesn't like nested OR operators, which would make
the following query invalid:

"(@field 1 | 2 | 3) | @otherfield blah"

You may need to revisit your strategy.

-- James Healy <ji...@deefa.com> Wed, 25 Nov 2009 16:59:36 +1100

pmlarocque

unread,
Nov 25, 2009, 1:02:22 AM11/25/09
to Thinking Sphinx
I see thanks!

pml
Reply all
Reply to author
Forward
0 new messages