在Sphinx搜索中,如何通过填充的字符串匹配



希望这是一个简单的。

我正在尝试搜索所有名称为John,而LastName不是一个空字符串(''(。在常规SQL中,这看起来像...

select id, firstname, lastname from users where firstname = 'john' and lastname != '';

使用Sphinx的扩展查询语法,从我的文档中理解的内容,应该看起来像这样。

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')

我得到的结果与上述完全相同,这让我相信最后一个名词没有做任何事情。

以前有没有人使用sphinxsearch做到这一点?任何指针或帮助都将不胜感激。

在索引(计划或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索引的普通索引或重新创建。

sphinx(以及有关此事的Manticore( - 在文档中索引单词。因此它无法匹配"无",因为索引中没有任何匹配!

作为使用长度属性的替代方案,可以使'nothing'

sql_query = SELECT id, firstname, IF(lastname='','_NONE',lastname) as lastname FROM ... 

然后可以匹配

... where match('@firstname john @lastname -_NONE');

,或者即使要始终想排除这样的行,也可以将它们排除在索引中:(

sql_query = SELECT ... FROM users WHERE lastname != ''

最新更新