I managed to have a query for getting all the friends and friends of friends of a particular user. I have a users table and a friendships join table. For simplicity, lets just say that the users table only has a primary key. The friendships table has the user_id and friend_id columns. The query is as follows:
"SELECT DISTINCT friends.user_id, friends.friend_id, users.*
FROM FRIENDSHIPS friends, USERS
WHERE USERS.id = friends.friend_id AND friends.user_id = #{u.id}
UNION
SELECT DISTINCT fof.user_id, fof.friend_id, users.*
FROM FRIENDSHIPS friends, FRIENDSHIPS fof, USERS
WHERE USERS.id = fof.friend_id AND friends.friend_id = fof.user_id AND friends.user_id = #{u.id}"
u.id is the id of the user whom I wish to get all the friends and friends of friends.
This query works very well however I would like to convert this to a query usable by sphinx's sql_attr_multi. I tried directly placing it like so:
sql_attr_multi = uint fwfof from query; \
SELECT DISTINCT `friends`.`user_id`, `friends`.`friend_id`, `users`.`id` \
FROM `friendships` friends, `users` \
WHERE `users`.`id` = `friends`.`friend_id` AND `friends`.`user_id` = $id \
UNION \
SELECT DISTINCT `fof`.`user_id`, `fof`.`friend_id`, `users`.`id` \
FROM `friendships` `friends`, `friendships` `fof`, `users` \
WHERE `users`.`id` = `fof`.`friend_id` AND `friends`.`friend_id` = `fof`.`user_id` AND `friends`.`user_id` = $id
but that obviously failed.
How do I make this query usable by sphinx's indexer?
Thanks in advance for anything that will point me in the right direction.