Facebook API: Getting list of profiles ordered by relevance - facebook-graph-api

How can I get a list of profiles via Facebook Graph API or FQL ordered by their relevance to the current user just as the result I would get after a name search within Facebook?
A graph API search like /search?type=user&fields=name&q=NAME with my current access token returns an irrelevant list of NAMEs while a search within Facebook for NAME returns most probable matches for me, starting from friends, friends of friends with most mutual friends, etc.
With and FQL query like
SELECT uid, name FROM user
WHERE strpos(name, 'NAME') >=0
AND uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
ORDER BY mutual_friend_count DESC
I can search within my friends and get the results ordered by mutual friend count. But I want to have non-friends in the result list as well.
How can I implement a Graph or FQL search query to get a similar set of profiles ordered by relevance to the current user making the query?

Related

FQL check if user is my friend

I currently use the following FQL to search users with name containing "Peter":
SELECT uid, name FROM user WHERE uid IN (SELECT id FROM profile WHERE CONTAINS('Peter'))
How can I know which user is my friend (<-- sounds silly)?
As far as I know, the following FQL can check whether the search result user is friend of me, or not:
SELECT uid2 FROM friend WHERE uid1 = me()
Apart from executing the 2nd query for each of the search result, is there a way to combine both query?
Update: Since FQL is going to be deprecated, Graph API solution is also welcomed.

How can I use FQL to count the number of specific pages each friend likes?

Here is an example query:
SELECT uid FROM page_fan WHERE page_id IN (104266592953439, 16155433, 5768707450)
AND uid IN (SELECT uid2 FROM friend WHERE uid1=me())
This will give me the uids of friends who like either of the above three pages.
I would like a solution that lets me know how many of the pages within the above subset is liked by each friend (provided they like at least one of them). Something that can scale out to 20+ pages within the subset would be ideal.
For example, if a friend likes all 3 of the pages, listing their uid 3 times would suffice. Although I'm not sure if that is possible in FQL.
Using PHP Facebook SDK.
The Graph API doesn't have a count method, so you're going to have to do this in your script.
Change your query above to this:
SELECT uid, page_id FROM page_fan WHERE page_id IN (104266592953439, 16155433, 5768707450)
AND uid IN (SELECT uid2 FROM friend WHERE uid1=me()) ORDER BY uid
Then loop through your result and count how many times each uid appears.

FQL query using Facebook Graph API

I have this query:
SELECT uid, name, current_location FROM user WHERE uid IN ( SELECT uid2 FROM friend WHERE uid1 = me() ) AND current_location.id IN (Select current_location.id from user where uid = me() )
It gives me results as required i-e friends only with a particular location.
Can i get the same results using Graph API instead? assuming I have the location id.
I tried this in graph API:
me/friends?location.id='111501225536407'&fields=location
Is something of ^ sort even possible using graph API?
Not that I can see. The Graph API is best for getting all of or the most recent items of an object. If you wanted to filter those friends by location, you would need to do that on the client side.
If you want Facebook to pre-filter your results on their servers, you need to use FQL.
you can execute fql queries (including multiqueries) through the graph api like this:
https://graph.facebook.com/fql?q={"albums":"select aid, name,cover_pid FROM album where owner=me()","album_covers":"select src from photo where pid in (select cover_pid from %23albums)"}&access_token=xxxxxx
It's a little tricky with the url encoding. Be sure that "#" sign used in multiqueries is encoded as %23

FQL does not give all the Friends likes

I am trying to retrieve all my friends' likes using FQL. But I am not getting all the likes only those pages which have "is_community_page" value true are being returned. Here's my FQL query
SELECT uid, page_id FROM page_fan WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
There is a max limit of 5000 results per FQL query that Facebook imposes before the results are trimmed for Privacy preferences of users. You can read this blog post about the same.
Facebook FQL retrieve a link to have full likes list and a samples list (quite unuseful), the point is not the limit of result, the point is fql has a different approach to list this data.
I stepped into it as well, trying to find a solution atm

Search profiles by city in Facebook API

I need to find a profile in a specific town or nation, but I just
found a way to get profiles by checking locale column in my Facebook
query. Is it possible to retrieve a user id and link with town and
other location information, but is it not possible to retrieve
profiles by town?
Using the Facebook API, it is not possible to query by locale since it's not indexed (see: http://developers.facebook.com/docs/reference/fql/user/ and look for column names with a * star). According to the documentation the only columns you can search on with the API is uid, username, and name.
If you only want to query over the friends associated with the provided access token, you can do this FQL query:
query = {}
# first get friends which have provided a current location
query['friends'] = "SELECT uid, name, current_location FROM user \
WHERE uid IN (SELECT uid1 FROM friend WHERE uid2=me()) \
AND current_location"
# then get the latlongs corresponding to their current location
query['latlong'] = "SELECT page_id, latitude, longitude FROM place \
WHERE page_id IN (SELECT current_location.id FROM #friends)"
You can use this information to assign a latlong to each friend. The filtering must be done client-side ... I don't think there is another way.
NOTE: Of course this needs the friends_location permission.
Using the Graph API, you can search profiles using text place names like so:
https://graph.facebook.com/search?q=new%20york&type=user&access_token=???
(Replace ??? with your current access_token.)
Not clear to me exactly what profile text is matched against, so you may end up finding people that have a name similar to your search query or that are just talking about a place. Best to check the users specified location as a second filter. Hope that helps.