Sphinx 索引用户数据

Sphinx Indexing User Data

所以我在 sphinx.conf 文件中有一堆这些源配置,它们索引并工作正常。

source company
{
    type            = mysql
    sql_host        = mysql
    sql_user        = root
    sql_pass        = root
    sql_db          = database_name
    sql_port        = 3306
    sql_query       = SELECT id, name FROM company
}
index company
{
    source          = company
    path            = /var/lib/sphinxsearch/data/company
    min_prefix_len  = 2
    morphology      = stem_en
}

我正在尝试设计一个索引所有用户朋友姓名的集合。 例如:

users (id, name):
1 Tom
2 Dick
3 Harry
4 Jane

friends (id, user_a, user_b):
1 1 2
2 1 3
3 2 3
4 4 3

问题

您将如何编写索引查询: 1.汤姆的朋友:[迪克,哈利] 2.迪克的朋友:[汤姆,哈利] 3.哈利的朋友们:[汤姆、迪克、简] 4.简的朋友:[哈利]

因此,当 Harry 开始键入以搜索他的朋友姓名时,他只能在列表中找到他的朋友姓名,所有其他用户也是如此(应该只能找到他们自己的朋友姓名)。 谢谢

假设你在 MySQL 中有这个:

mysql> select * from users; select * from friends;
+------+-------+
| id   | name  |
+------+-------+
|    1 | Tom   |
|    2 | Dick  |
|    3 | Harry |
|    4 | Jane  |
+------+-------+
4 rows in set (0.00 sec)

+------+--------+--------+
| id   | user_a | user_b |
+------+--------+--------+
|    1 |      1 |      2 |
|    2 |      1 |      3 |
|    3 |      2 |      3 |
|    4 |      4 |      3 |
+------+--------+--------+
4 rows in set (0.00 sec)

您可以将以下内容添加到您的来源中:

sql_query_pre   = set @id=0;
sql_query       = select (@id:=@id+1) id, u.name user, (if(u.id=user_a,u3.name,if(u.id=user_b,u2.name,''))) friend from users u left join friends f on f.user_a = u.id or f.user_b = u.id left join users u2 on f.user_a = u2.id left join users u3 on f.user_b = u3.id
sql_field_string = friend

这会给你这个:

mysql> set @id=0;select (@id:=@id+1) id, u.name user, (if(u.id=user_a,u3.name,if(u.id=user_b,u2.name,''))) friend from users u left join friends f on f.user_a = u.id or f.user_b = u.id left join users u2 on f.user_a = u2.id left join users u3 on f.user_b = u3.id;
Query OK, 0 rows affected (0.00 sec)

+------+-------+--------+
| id   | user  | friend |
+------+-------+--------+
|    1 | Tom   | Dick   |
|    2 | Tom   | Harry  |
|    3 | Dick  | Tom    |
|    4 | Dick  | Harry  |
|    5 | Harry | Tom    |
|    6 | Harry | Dick   |
|    7 | Harry | Jane   |
|    8 | Jane  | Harry  |
+------+-------+--------+
8 rows in set (0.00 sec)

下面是它在 Sphinx 中的工作方式:

mysql> select friend from company where match('@user tom @friend di*');
+--------+
| friend |
+--------+
| Dick   |
+--------+
1 row in set (0.00 sec)

mysql> select friend from company where match('@user tom @friend ha*');
+--------+
| friend |
+--------+
| Harry  |
+--------+
1 row in set (0.00 sec)

mysql> select friend from company where match('@user tom @friend ja*');
Empty set (0.00 sec)

mysql> select friend from company where match('@user harry @friend ja*');
+--------+
| friend |
+--------+
| Jane   |
+--------+
1 row in set (0.00 sec)

您可能也对 CALL SUGGEST / CALL QSUGGEST 感兴趣。这是一个互动课程 - https://play.manticoresearch.com/simpleautocomplete/