Sphinx索引用户数据

问题描述 投票:0回答:1

所以我在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开始键入搜索他的朋友的名字时,他只能在列表中找到他的朋友的名字,而其他所有用户都一样(应该只能找到他们自己的朋友的名字)。谢谢。

php full-text-search sphinx
1个回答
0
投票

假设您在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)

下面是在狮身人面像中的工作方式:

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/

© www.soinside.com 2019 - 2024. All rights reserved.