📜 ⬆️ ⬇️

The implementation of the condition "OR" in SphinxQL

The SQL-like SphinxQL language does not yet support the “OR” clause in the “WHERE” statement. That is, when you query the search index can not write something like

SELECT * FROM `goods_index` WHERE `price` > 100 OR `price` = 0; 
select from the index all products with a price greater than 100 MNT or if the price is not specified

But the way out, of course, is: use calculations. Let's rewrite the query for the example invented by me in this form:
')
 SELECT *, IF(`price` > 100, 1, 0) + IF(`price` = 0, 1, 0) AS `pricematch` FROM `goods_index` WHERE `pricematch` = 1; 

The trick is that the pricematch will always be equal to one if the price is above 100 or equal to zero, and to check the compliance of the record, it suffices to check the equality of pricematch one.

Using such calculations in SphinxQL and traditional SQL queries, one can get out of the situation in other situations when the “or” condition is necessary in the query.

update.

artifex in comments provides a much more transparent and readable version of using OR in a SELECT statement:

 SELECT *, (`price` > 100 OR `price` = 0) AS `pricematch` FROM `goods_index` WHERE `pricematch` > 0; 


update 2.

Faced a problem: an indexable attribute in the database is of type BIGINT. The Sphinx config is described as
 sql_attr_bigint = myattr 


At the same time, such a request does not give the desired result, returning empty set:

 SELECT * FROM index WHERE myattr > 87124599823547; 


As with the condition “or”, the transfer of the expression to the SELECT section helped:

 SELECT *, ( myattr > 87124599823547 ) AS attrmatch FROM index WHERE attrmatch = 1; 

Source: https://habr.com/ru/post/195918/


All Articles