FQL, search for non-friend people and sort by mutual friends - facebook-fql

I look for facebook users (any user, not just my friends or the friends of a specific user) with a pattern in the name, like this:
SELECT first_name,uid FROM user WHERE contains('pete')
This returns an array of people who's name contains "pete" (i.e. also "peter"). The number is quite high, so the results are of course truncated. I would like to grab, in this set (before the server-side truncation) who has most mutual friends with me (or with an arbitrary user id). However, issuing
SELECT first_name,uid FROM user WHERE contains('pete') AND mutual_friend_count > 1
gives and empty set (and no errors). I think the server truncates before applying the latter selection, as in normal SQL would happen, for performance reasons (or explicit limits I missed in the documentation).
I made a second attempt by trying to retrieve all the friends of friends of a userid, and then select on the name. However, retrieving friends of friends is not allowed (and technically difficult because some friends hide their list of friends, and this breaks the query).
I can see no other way to achieve my goal... is there any? (I mean: given a userid, search for users with highest number of mutual friends).

Your best bet, since there's no FQL solution, would be to begin a database of your own following Facebook's best practices and policies of course. That way you can structure your data. Data can be kept current by setting up Facebook's Realtime updates API (https://developers.facebook.com/docs/reference/api/realtime/) to capture when new friendships are made.

I just tried it with "order by mutual_friend_count desc" and it worked. So the query needs to be:
SELECT first_name,uid FROM user WHERE contains('pete') order by mutual_friend_count desc

Related

Public search facebook user by Non-exact name

I want to do a public search on all facebook users according to the name of the user, but i want the search not to be on the exact name. for exmaple if a user will search for 'John Ada' the result 'John Adam' will be retreived too.
After that the results need to be ordered by mutual_friend_count.
I manged to do this partly with the following FQL :
SELECT uid,name, pic_square, profile_url , mutual_friend_count FROM user WHERE contains("Name Surename")
order by mutual_friend_count desc
but it returns only exact names, and if the name was missing a letter like my earlier example the user was not retreived.
I tried to mess around with the Offset and Limit but with no success either.
Thanks!
The Facebook search algorithms seem to be optimized to find full names. A partial name never seems to return as many results as a full name will. In my experience, Facebook knows how to expand names for common variants. A search for "John Adam" will return "John Adams", "Johnny Adams" and "John Addams"
The other battle you are fighting is the Facebook filtering algorithm. Facebook first finds all matches that meet your query, then filters them for visibility to your user.
You can try adding AND NOT is_blocked to your WHERE statement. This should filter out anything that isn't visible to you before the filtering algorithm.

FQL query,is possible to get all events in your area?

Talking aout Facebook FQL,
is it possible to do a query like
SELECT name, venue, location, start_time FROM event
WHERE location ="New York"
to search all public events, posted on Facebook, and available in your area?
No, since non of fields you listed are indexable
You can however search Graph API for public events in specified time ranges.
Which probably not suitable for your needs, since location field in results still not always contain reliable data, and filtering all the public events in specified range may be overkill...

Facebook FQL Query, searching on non-indexable item

I would like to generate a list of facebook user id's of people who are not in the United States. Rather than iterating through a list of user id's and checking each locale, I would like to do an FQL query. I attempted to execute this statement:
SELECT uid, name FROM user WHERE locale != "en_US"
However, when I make the query, I recieve the response:
(code 604): Your statement is not indexable.
This is because locale is not an indexable field. Do any of you know what I can do to get around this? Or perhaps another way of doing this entirely?
Thanks in advance!
It says "Your statement is not indexable." That means, you can't just use any query (like, extreme case, SELECT uid, name FROM user to get all facebook users) that does not conform. Only queries that include WHERE on fields that are indexable can be used. It's a policy, you can not circumvent it.

How to get (better) demographics for fans of a Facebook page?

I'm trying to get demographics for fans of a page on Facebook - mostly country and city, but age and gender as secondary.
The primary way to do it is using FQL and doing a query in the insights table. Like so:
FB.api({
method: 'fql.query',
query: "SELECT metric, value FROM insights WHERE object_id='288162265211' AND metric='page_fans_city' AND end_time=end_time_date('2011-04-16') AND period=period('lifetime')"
}, callback);
The problem with this, however, is that the table returns a maximum of 19 records only, both for the country and the city stats. The response for a page I'm testing is as such:
[
{
"metric": "page_fans_city",
"value": {
"dallas": "12345",
"atlanta": "12340",
(...)
"miami": "12300"
}
}
]
So I'd like to know if there's any alternative to that -- to get demographics of the current fans of a page (no snapshot necessary).
Things I've tried:
Using LIMIT and OFFSET on the query do nothing (other than, sometimes, give me an empty list).
One alternative that has been discussed in the past is to use the "/members" method from the Graph API (more here) to get a list of all users, and then parse through that list. That simply doesn't work - a method exists, and it may have worked in the past, but it's not valid anymore (disabled?).
Request:
https://graph.facebook.com/platform/members?access_token=...
Response:
{"error":
{
"type":"OAuthException",
"message":"(#604) Your statement is not indexable. The WHERE clause must contain an indexable column. Such columns are marked with * in the tables linked from http:\/\/developers.facebook.com\/docs\/reference\/fql "
}}
Other solution was to do a query to the page_fan table and filtering by page_id. This doesn't work, either; it may have worked in the past, but now it says that the page_id column is not indexable therefore it cannot be used (same error as above, which leads me to believe /members uses the same internal API that has been disabled). Page_fan query is only useful to check if individual users are fans of a page.
There's also the like table, but that's only useful for Facebook items (like posts, photos, links, etc), and not Facebook Pages.
Going to the insights website about the Page, you can see the data in some nice graphs and tables, and download an Excel/CSV spreadsheet with the historic demographics data... however, it also limits the data to 19 entries (sometimes 20 with a few holes in there as cities trade top positions though).
Any other hint on how to get that data? I'd either like the insights query with more results, or at least a way to get all the page fans so I could do the location query myself later (even if the page I want to get it from has almost 5 million fans... gulp).
The data pipeline for this metric is currently limited to 20 items. This is a popular feature request and something Facebook hopes to improve soon.

Use Facebook FQL to select the work information from the profile

I would like to get work place information of a user using FQL.
When I use the Graph API and get the User object, it contains work information, which is essentially a list of the work history. The list elements contain nodes of employer, location, description, etc...
The nodes appear to be pages internally. If I take the id of a node, e.g. from the employer, and use FQL to query a page with that page_id, I do get an object with corresponding information.
My question now is, how do I use FQL to get the same information without accessing the Graph API? What table stores the work-related information, for example how do I find all the page_id of the employers of a given user?
The reason I insist on using FQL only is performance. Of course I could access the Graph API for all the users in question and get the info that way, but I'm looking for an FQL-only solution.
You can get this information from FQL. Read the "user" table and look for the work field. The JSON data returned should be the same format as the one for Graph, i.e., the result is an array and each result should include an "employer" object with an "id" and "name" field.
You will need user_work_history or friends_work_history to access this field.