希望这是一个简单的。
我正在尝试搜索名字为john且姓氏不是空字符串的所有记录('')。在常规SQL中,这看起来像......
select id, firstname, lastname from users where firstname = 'john' and lastname != '';
使用sphinx的扩展查询语法,根据我在documentation中的理解,它应该是这样的。
select id, firstname, lastname from users where match('@firstname john @lastname !\'\'');
但是,通过上述查询,我仍然会得到空白的姓氏。
+---------+------------------+----------+
| id | firstname | lastname |
+---------+------------------+----------+
| 110809 | John | |
| 313681 | John | |
| 520045 | John | |
| 554136 | John | |
如果我尝试此查询:
select id, firstname, lastname from users where match('@firstname john')
我得到与上面完全相同的结果,让我相信lastname子句没有做任何事情。
以前有没有人必须使用sphinxsearch?任何指针或帮助将不胜感激。
答案 0 :(得分:1)
在索引(计划或RT)配置中使用index_field_lengths = 1。之后,您应该自动拥有一个属性<field_name>_len
,您可以使用该属性来过滤(或查找)具有空字段内容的文档,例如
mysql> desc table;
+----------+------------+
| Field | Type |
+----------+------------+
| id | bigint |
| name | field |
| a | string |
| name_len | tokencount |
+----------+------------+
4 rows in set (0.00 sec)
mysql> insert into table values(1,'abc', 'abc');
Query OK, 1 row affected (0.00 sec)
mysql> insert into table values(2,'', '');
Query OK, 1 row affected (0.00 sec)
mysql> select * from table where name_len != 0;
+------+------+----------+
| id | a | name_len |
+------+------+----------+
| 1 | abc | 1 |
+------+------+----------+
1 row in set (0.00 sec)
index_field_lengths需要重新索引普通索引或重新创建RT索引。
答案 1 :(得分:0)
Sphinx(以及Manticore) - 索引文档中的单词。所以它无法匹配'没有',因为索引中没有任何东西可以匹配!
作为使用长度属性的替代方法,可以使“无”某事进行索引:)
... where match('@firstname john @lastname -_NONE');
然后可以匹配
sql_query = SELECT ... FROM users WHERE lastname != ''
或者即使总是希望排除这些行,也可以将它们从索引中排除:)
const void *