- A+
sphinx是一个开源的全文搜索引擎,与mysql、postgreSQL、xml完美结合,查询速度非常快,支持分布式检索,可扩展性好,并行处理,高效的利用内存和磁盘IO。强大的过滤、分组、排序、分页功能,强化mysql的功能和性能,针对mysql分库分表后,sphinx也是一种可行的解决方案。关于sphinx应用的一些文章可以参见http://www.ttlsa.com/?s=sphinx
在使用sphinx SphinxQL接口过程中,使用多个属性来进行过滤,发现不能使用OR。
查询语句:select * from ttlsa_com_main where usecount=8290 or uptime=1342076161;
报错的信息如下:ERROR 1064 (42000): syntax error, unexpected TOK_OR, expecting $end near 'or'。
查了下官方文档有这么一句:WHERE clause. This clause will map both to fulltext query and filters. Comparison operators (=, !=, <, >, <=, >=), IN, AND, NOT, and BETWEEN are all supported and map directly to filters. OR is not supported yet but will be in the future.
目前WHERE子句不支持OR。那么该如何解决呢?
1. 对于单个属性列的多个值过滤可以使用IN。如
1 |
mysql> select * from ttlsa_com_main where usecount in (15974, 10932, 6807); |
2. 对个多个属性列。可以这么来做,查询语句还是上面的那句,改成下面的
1 2 3 4 5 6 7 8 |
mysql> select *, if((usecount=8290 or uptime=1342076161),1,0) as or_filter from ttlsa_com_main where or_filter=1; +------+--------+----------+------------+--------+--------+--------+--------+--------+--------+--------+--------+-------+----------+-----------+ | id | weight | usecount | uptime | status | score1 | score2 | score3 | score4 | score5 | avgint | avgfra | isdel | unittype | or_filter | +------+--------+----------+------------+--------+--------+--------+--------+--------+--------+--------+--------+-------+----------+-----------+ | 9 | 1 | 8290 | 1357702102 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | | 16 | 1 | 13175 | 1342076161 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | +------+--------+----------+------------+--------+--------+--------+--------+--------+--------+--------+--------+-------+----------+-----------+ 2 rows in set (0.00 sec) |
这样就可以了。 sphinx做为全文搜索,很多地方都在大量的时候。强烈推荐,谁用谁知道。
如需转载请注明出处: sphinx SphinxQL不支持OR操作解决方案 http://www.ttlsa.com/html/2361.html

07/11/2015 上午 11:56 沙发
So easy 看这里就是用的Sphinx: http://www.jihaoba.com
11/09/2015 下午 2:54 板凳
sphinxQL实现手机号模糊搜索如何实现?
11/09/2015 下午 2:53 地板
sphinxQL如何实现手机号模糊搜索?
04/05/2014 上午 11:06 4楼
多谢分享
04/05/2014 上午 11:29 1层
@masaka
” />
16/08/2013 下午 10:20 5楼
和mongodb是一个思路,不错
17/08/2013 下午 12:14 1层
@路人 怎么说?
16/08/2013 下午 7:25 6楼
mark下
16/08/2013 上午 10:28 7楼
一个字,好
16/08/2013 上午 9:38 8楼
sphinx 谁用谁知道
15/05/2017 下午 10:20 1层
@sphinxer goudan.org 狗蛋磁力链接