Search for people by name in FQL - facebook

I'm trying to develop a search for people by name feature using the Facebook API.
I'm trying this FQL query:
SELECT uid, username, name, pic_square FROM user WHERE strpos(name, 'Alfredo Artiles') >= 0
But I get an "Your statement is not indexable. The WHERE clause must contain an indexable column." error.
I also tried to add a "and uid > 0" condition, but that didn't work too.
Any idea?

SELECT uid, username, name, pic_square FROM user WHERE contains('Alfredo Artiles')
I have no idea why it works, as it violates the "at least and indexable field in WHERE", but it does.
EDIT: this is where I read it: link

later edit
Ok, sorry for the mistake regarding strpos, I didn't remeber it existence last time i checked the fql docs. The thing about it is that it can be used just in certain cases. You need to have a primar indexable column in your where clause and a second condition with strpos (at least this is how I succeded using it). For example:
SELECT actor_id, message FROM stream WHERE source_id = me() AND strpos(message, 'stuff') > 0 limit 50
I hope this clarifies a little bit the confusion with this function.

U need to specify the uid in where clause fetch details in facebook user table. we don't get the the all user details.here get the details of currently logged in user
"SELECT name FROM user WHERE uid IN(SELECT uid2 FROM
friend WHERE uid1 = me()) AND strpos(name,'Alfredo Artiles') >=0"

YES, YOU CAN!!!
Try to search in profile table, instead of user table. In profile table, the name field is indexable, so your statement would have to change only a bit:
SELECT id, username, name, pic_square FROM profile WHERE contains('alfredo artiles') and type='user'
Contains is not case-sensitive while strpos is. You could still use lower and strpos, but you would also have the problem of special characters. Contains is the solution.
You can find all fields of profile table in facebook doc:
Hope it helps!

Related

FQL like operator

Can someone tell me why this works:
SELECT name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me()) AND strpos(lower(name),"jo") >=0
however this does not:
"SELECT url, id, type, site FROM object_url WHERE strpos(lower(url), 'sephora') >= 0"
When I put it in http://developers.facebook.com/docs/reference/rest/fql.query/
I get this response:
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
however "url" in indeed indexable.
any thoughts? I've already scoured stackoverflow but no answers found.
Site isn't indexable, it's only selectable. It doesn't have an * next to it here https://developers.facebook.com/docs/reference/fql/object_url/
you need to use url or id as the indexable column, so basically one of those needs to be in your where clause.
FQL != SQL You can't a a WHERE on any column you want. Not surprising considering the performance required with so many concurrent writers, I mean, one person in every ten in the world has signed up.
The problem in the second example is that a field used as a parameter in one of the string functions loses its indexability. It has to be the column itself.

Does Facebook friends sequence remains same while using FQL?

I am using FQL to get the list of friends
$fql = select uid, name, work_history,
education_history, current_location
from user where uid IN (select uid2
from friend where
uid1=FACEBOOK_USER_ID);
does this query returns same friend sequence all the time or it is different in few cases.
suppose list returns : Friend1,friend2,friend3 .... etc
So does it return the same sequence all the time or not?
Please let me know if any further detail required or if the problem is not clear.
-deepak
I'd take all doubt out of the equation and just add an ORDER BY:
SELECT uid, name, work_history, education_history, current_location
FROM user
WHERE uid IN (select uid2 from friend where uid1=<FACEBOOK_USER_ID>)
ORDER BY uid
It appears that they come back in sequential order based on the user id. So if you had three friends with the user ids 111111, 222222, and 3333333, they would be returned in that order.
With THAT said, I wouldn't rely on this behavior. Even if it never changes, it is probably bad practice to assume it will always be that way. It's a much better practice to get the data, then order it on your end to your needs. That way you know it is always in the order you want even if the ordering on Facebook's end changes.

Can I filter friends with FQL?

I'm interested in rendering only a list of friends of a certain sex. I'm using the query below, but not having any luck:
"SELECT name FROM user WHERE sex= $sex AND uid IN ( SELECT uid2 FROM friend WHERE uid1= $fb_uid )"
From what I understand, it should work because AT LEAST ONE of the constraints is indexable. Am I missing something?
Here are my thoughts:
Make sure that the user have the right permission, I guess it's read_stream
Make sure the user you are matching is the logged in user (maybe using me() instead)
the sex field accepts only: male and female

Query all friends in a network

Is there any way to retrieve all friends in a particular network? For example, How can I view the list of my Facebook friends joined the network of my organization.
I think you can say
SELECT uid, name
FROM user
WHERE
affiliations.nid = [your network id]
AND uid IN (SELECT uid2 FROM friend WHERE uid1=[your user id])
You can write an FQL query such as this:
SELECT uid, name FROM user WHERE affiliations.nid = <your network id>
The problem is that affiliations are not indexable, so you would need to have a constraint on either the username, name, or uid column(s) (which kind of defeats the purpose).
So the moral of the story is that if you know the ID of the user or are able to generate a range of users, then you can fetch this data. Otherwise, Facebook pukes on you.
Hope this helps!

Facebook Comments Count

I am trying to write a Facebook query that will return all comments posted by a user to his friends,
however I can't seem to find the correct schema. Its as if there are no 'indexable' fields to build this.
Any suggestions please?
With thanks,
Wineshtain
The indirect path for stream comments would be something like
select * from comments where fromid = <my_id> and object_id in (
select post_id from stream where sourceid in (
select uid1 from friend where uid2 = <my_id> ) )
for photos, substitute the middle query with
SELECT pid FROM photo WHERE aid IN ( SELECT aid FROM album WHERE owner IN ( ...
Unfortunately the security settings may restrict the querying your friends wall posts and photos.
I don't believe you can accomplish this in a direct manner as you describe. FQL tables are generally only indexed on a limited criteria (for performance reasons I'm sure). In the case of the Comments FQL Table, you can only select comments via a post ID or an xid.
Unfortunately, this means that you have to know the objects that a user has commented on before you can get the comments for it. You would have to have previously selected all the posts, photos, etc. that you wished to get the comments for before you could retrieve them.