Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
128 lines (116 sloc) 27.3 KB

This guide explores how to use Facebook's graph search syntax to find posts, comments, people, and other entities on Facebook. This guide emphasizes showing how to write your own queries rather than just giving examples of queries or giving an exhaustive list of search options.

Table of Contents

Steps

Reviewing the Mathematical Prerequisites

  1. Know what the mathematical prerequisites are. The main mathematical prerequisites for developing fluency with graph search are Boolean algebra, naive set theory, reverse Polish notation, and operator arity.
    • It is possible to work with graph search even without an understanding of these topics. For instance, you can use some of the existing interfaces listed in the "Explore existing interfaces" section below, modify example searches, or stick to very simple queries. Thus, this section can be skipped on a first read. Nonetheless, having a solid understanding of these topics will ensure you aren't making any mistakes in writing queries, and will allow you to craft tricky queries.
  2. Understand Boolean algebra. Boolean algebra is an abstract way of formalizing logical notions like "and", "or", and "not". Many search engines incorporate Boolean algebra. For instance, Google has an advanced search page[1] where one can find a box to search all of the given words (Boolean "and"), a box to search any of the given words (Boolean "or"), and a box for searching none of the given words (Boolean "not").
    • Facebook graph search incorporates parts of Boolean algebra. In particular, it has a family of intersect operators (corresponding to a Boolean "and") and a family of union operators (corresponding to a Boolean "or"). However, it is lacking a notion of negation, so there is no operator corresponding to the Boolean "not". Concretely, this means you cannot search for things like "all posts not written by Mark Zuckerberg".[2]
  3. Understand naive set theory. Naive set theory is an instance of a Boolean algebra, where the Boolean "and" is called intersection, the Boolean "or" is called union, and the Boolean "not" is called set complement.
    • The key point is this: in Facebook graph search, the operands will all be conditions that specify sets. For instance "posts liked by Dustin Moskovitz" specifies the collection (set) of all posts on Facebook that have been "liked" by Dustin Moskovitz. Then, using the union and intersect operators (remember, you have part of a Boolean algebra), you can form new sets like "posts written by Mark Zuckerberg that were liked by Dustin Moskovitz", i.e. the intersection of "posts written by Mark Zuckerberg" and "posts that were liked by Dustin Moskovitz".
    • One thing to be careful about is that the English word "and" can sometimes mean "union", for instance when one says "the set of all cats and dogs" (this is the union of "the set of cats" and "the set of dogs", rather than the intersection). There are many similar subtleties in mathematical logic; for more information see Tim Gowers's blog post on logical connectives[3] as well as Terence Tao's lecture notes on mathematical logic[4] and set theory.[5]
  4. Understand reverse Polish (a.k.a. postfix) notation. Reverse Polish notation specifies a syntax for how operators and operands are written. Usually, you use infix notation to describe arithmetic, for example 3 × 4 + 5 = 17 and 3 + 4 × 5 = 23. To distinguish between the two, you rely on operator precedence (e.g. multiplication has higher precedence than addition). At other times, you rely on parentheses to group operations, for example (8 ÷ 4) ÷ 2 = 1 and 8 ÷ (4 ÷ 2) = 4.
    • In contrast, both prefix and postfix notations rely only on the order in which the operands and operators are written (and the arity of the operators). For instance, to write 3 × 4 + 5 in prefix (a.k.a. Polish) notation, one writes + × 3 4 5, where the implicit grouping is (+ (× 3 4) 5), and to write 3 + 4 × 5 one writes × + 3 4 5, where the implicit grouping is (× (+ 3 4) 5). In postfix (a.k.a. reverse Polish) notation, the operator comes after the operands, so to write 3 × 4 + 5 one writes 3 4 × 5 +, where the implicit grouping is ((3 4 ×) 5 +) and to write 3 + 4 × 5 one writes 3 4 + 5 ×, where the implicit grouping is ((3 4 +) 5 ×).
    • Note that in prefix and postfix notations, there is no concept of operator precedence and no requirement to use parentheses: whatever operator is in the right place "gobbles up" the (two) arguments following it (in prefix notation) or in front of it (in postfix notation).
    • The relevance of reverse Polish notation to Facebook graph search is this: in Facebook graph search, queries are written in postfix notation, where the operators are called "intersect" and "union" (along with variants of these). So to search for (A and B) or C, one types essentially A B intersect C union.
  5. Understand operator arity. The arity of an operator is the number of arguments it uses up. For instance, both addition and multiplication have arity two (also called binary) because they both use up two arguments. In contrast, the ternary operator in many programming languages takes three arguments (i.e. has arity 3 or is ternary): a boolean (value that is true or false) and two expressions, and outputs the first expression if the boolean is true and the second expression if the boolean is false.
    • The relevance of arity to Facebook graph search is this: in Facebook graph search, there isn't just a single union and single intersect operator. Rather, each is a family of operators, union, union-2, union-3, union-4, etc., and intersect, intersect-2, intersect-3, intersect-4, etc. Each "-n" variant has arity n. For example, union-3 takes three arguments A, B, C, and produces the union of all of them; in other words, (A B C union-3) = A ∪ B ∪ C (remember, you are using postfix notation!). The special union and intersect operators (without a number) have variable arity: they use up all the arguments coming before them. For instance, (A B C union) = A ∪ B ∪ C. Thus, there are multiple ways to write the same query.[6]

Finding the ID Number of an Entity

  1. Understand what an entity ID number is. Each entity on Facebook (such as a person or organization) has a unique ID number associated with it. To create the most useful queries involving such entities, you will need to find the Facebook ID of those entities.
  2. Find the ID number of a person. To get the Facebook ID of a person, you can use https://stalkscan.com/ or https://findmyfbid.com/ or right click on a user's profile picture and select "Copy link address" ("Copy link location" in some browsers) and check the value of referrer_profile_id or profile_id.
  3. Find the ID number of an organization. To get the Facebook ID of an organization, go to the organization page, right click the organization photo, and select "Copy link address" ("Copy link location" in some browsers). Then the organization's ID number is the first number after facebook.com.
  4. Find the ID number of a job title. The ID of a job title is the ID of the page corresponding to the job title, but there can be many such pages. One way to find the ID number of a job title is to go to https://www.facebook.com/search/117968858296276/pages/str/JOBTITLE/pages-named/intersect where JOBTITLE is the name of the job title. Then you can find the ID of the job title in the URL of the search results.[7] There also exist lists containing job title IDs for common job titles.[8][9]
  5. Find the ID number of a group. To get the Facebook ID of a group, go to the Members page of the group and check the link of the "Members" header on that page. The ID number is the number that appears in the link.

Understanding the General Syntax of a Query

  1. Understand the general pattern of a query. The general syntax is https://www.facebook.com/search/ followed by some sequence of atomic conditions and operators, so that the resulting URL might look like https://www.facebook.com/search/A/B/operator₁/C/operator₂, where A, B, and C are atomic conditions. Each atomic condition specifies a simple set such as the set of all posts written by Mark Zuckerberg. (More on atomic conditions in the next section.) Each operator is one of union, union-2, union-3, etc., or one of intersect, intersect-2, intersect-3, etc.
  2. Understand the grouping of the operators. As explained in the mathematical prerequisites section, Facebook graph search uses a postfix notation. This means that as soon as an operator is seen, it "gobbles up" a certain number of arguments appearing before it (which varies based on the operator's arity, another concept explained in the mathematical prerequisites section). For instance, a sequence like A/B/C/union-2/D/intersect-3 groups into (A (B C union-2) D intersect-3), which means the same thing as A ∩ (BC) ∩ D. The special union and intersect operators without a number will gobble up all the arguments preceding it, so the previous query could also have been written A/B/C/union-2/D/intersect or B/C/union/A/D/intersect.
  3. Consider practicing the query pattern. The following are some exercises (answers given in black boxes; highlight to see the text):
    • What does the query A/B/C/union/D/intersect mean? (answer: (ABC) ∩ D)
    • What does the query A/B/C/D/union-2/intersect/E/union mean? (answer: (AB ∩ (CD)) ∪ E)
    • How would you write the query A or (B and C)? (answer: A/B/C/intersect-2/union works, as does B/C/intersect/A/union)
    • How would you write the query (A and B) or (C and D)? (answer: A/B/intersect-2/C/D/intersect-2/union-2 works, as does A/B/intersect/C/D/intersect-2/union)

Finding the Atomic Conditions

  1. Understand what atomic conditions are. Atomic conditions specify simple sets, such as "all posts written by Mark Zuckerberg", "all posts about malaria", or "all people who have worked at the WHO". In this guide, these conditions are called "atomic" because they cannot be broken down into simpler conditions.[10] In contrast, compound conditions such as "all posts written by Mark Zuckerberg or Dustin Moskovitz" can be broken down into "all posts written by Mark Zuckerberg" and "all posts written by Dustin Moskovitz" and obtained via the union operation. Some things to keep in mind as you work with atomic conditions:
    • Each different type of entity has its own atomic conditions. For example, the atomic conditions for posts are different from those of people.
    • Below, we won't give an exhaustive list of atomic conditions, since such lists already exist (we will refer to some of these lists at the end of this section). Instead, we will teach you how to figure out how to write your desired query.
    • Atomic conditions can contain a slash (/), even though a slash is also used to separate atomic conditions from each other and from operators. Always make sure you know whether a particular slash is part of an atomic condition or is used as a separator!
    • The shortcut me can be used instead of your own Facebook ID.
  2. Keep in mind some of the atomic conditions for posts. Some of the most useful ones are:
    • str/KEYWORD/stories-keyword: post contains KEYWORD; for example str/malaria/stories-keyword finds all posts containing the word "malaria"
    • GROUPID/stories-in: search for posts in the group with ID GROUPID; for example 461729987257061/stories-in finds all posts in the Open Borders Action Group
    • FACEBOOKID/stories-by: post by user with ID FACEBOOKID; for example 4/stories-by finds all posts by Mark Zuckerberg (who has the Facebook ID of 4)
    • FACEBOOKID/stories-liked: post liked by user with ID FACEBOOKID; for example 4/stories-liked finds all posts liked by Mark Zuckerberg (who has the Facebook ID of 4)
    • FACEBOOKID/stories-commented: post commented on by user with ID FACEBOOKID; for example 4/stories-commented finds all posts commented on by Mark Zuckerberg (who has the Facebook ID of 4)
    • YYYY/date/stories: post made during the year YYYY; for example, 2017/date/stories finds all posts made in 2017
    • MON/YYYY/date-2/stories: post made during the month MON of year YYYY; for example sep/2017/date-2/stories finds all posts made during September 2017
    • DD/MON/YYYY/date-3/stories: post made during the day DD of month MON of year YYYY; for example 05/sep/2017/date-3/stories finds all posts made on September 5, 2017
  3. Keep in mind some of the atomic conditions for people. Some of the most useful ones are:
    • FACEBOOKID/employees/ever: people who have worked at organizations with ID of FACEBOOKID
    • FACEBOOKID/friends: friends of user with Facebook ID of FACEBOOKID
    • PAGEID/likers: people who have liked the page with ID of PAGEID
  4. Know where to find more complete lists of atomic conditions. Below are some webpages or documents that give a more complete listing of atomic conditions:
    • "Facebook Graph Search Operators"[11]
    • "Facebook Graph search links"[12]
    • "The Facebook #Sourcing Tutorial Part 2"[13]
    • "The hidden language of Facebook Graph Search - keywords without explanation"[14]
    • "Facebook Graph Search Important Changes" (2018).[15]

Putting Together the Query

  1. Use the general pattern and atomic conditions to write your query. Now that you know the general syntax of a query and have some examples of useful atomic conditions, you can put everything together into an actual query. Probably the easiest way to do this is to write out the query informally in infix notation, then convert it to postfix notation, and finally replace each informal condition by its corresponding atomic condition and the spaces and parentheses by slashes. Here is an example of this process:
    • Suppose you want to find all posts by either Mark Zuckerberg or Dustin Moskovitz that were made in 2015. You write this in infix notation as: (by-Mark-Zuckerberg or by-Dustin-Moskovitz) and made-in-2015. Now you convert to postfix as: ((by-Mark-Zuckerberg by-Dustin-Moskovitz union) made-in-2015 intersect). Finally, you replace the informal conditions by the corresponding atomic conditions and the spaces and parentheses by slashes: 4/stories-by/6/stories-by/union/2015/date/stories/intersect.
  2. Look at some example queries. Here are some example queries that give some idea of what is possible. Using the concepts discussed above, you can craft much more complicated queries.

Understanding the Functions View of Graph Search

  1. Understand what the functions view is. There is a more sophisticated view of graph search than the view given in the above sections. In this new view, there aren't just sets, but also functions that map between different sets. Functions allow you to transform a set of objects of one type into a set of objects of another type.The outputs of functions will be sets, on which you can use union and intersect operators, just as before.
  2. Understand the general pattern of a function. A function f: AB takes an object of type A and outputs an object of type B. In the case of graph search functions, B will be of the form power(C) (i.e. the power set of C) for some type C, meaning that each output will itself be a set of objects of type C.[16]
    • In addition, each function f: A → power(C) induces a function F : power(A) → power(C) defined by F(E) = ⋃eE  f(e) for each EA. In other words, F takes each object e in E, produces f(e), and then takes the union of all of these outputs. In graph search, f and F are denoted the same, which might sound confusing, but in practice it is not confusing. For example, stories-liked: Person → power(Post) returns the posts liked by a person, so if you see 4/stories-liked you know it is posts liked by a single person (in this case, that person happens to be Mark Zuckerberg), but if you see 4/friends/stories-liked, then the object appearing before stories-liked is a set of people (namely, the friends of Mark Zuckerberg), so you know it is the power set version of the stories-liked function.
  3. Learn the type signatures of common functions. Below, Person is the set of all people, Post is the set of all posts, Keyword is the set of all keywords, Date is the set of all date intervals, Group is the set of all groups, and Page is the set of all Pages.
    • stories-liked: Person → power(Post)
    • stories-by: Person → power(Post)
    • stories-commented: Person → power(Post)
    • stories-tagged: Person → power(Post)
    • stories: Date → power(Post)
    • stories-keyword: Keyword → power(Post)
    • pages-liked: Person → power(Page)
    • friends: Person → power(Person)
    • employees: Page → power(Person)
    • employees/ever: Page → power(Person)
    • likers: Page → power(Person)
    • groups: Person → power(Group)
    • stories-in: Group → power(Post)
    • non-friends: Person → power(Person)
    • non-groups: Person → power(Group)
    • pages: {book, musician, …} → power(Page)
    • males: power(Person)
    • users-interested: {males, females, both} → power(Person)
    • political-views: Person → power(Page)
    • members: Group → power(Person)
  4. Understand how to combine functions with union and intersect operators.
    • Imagine you are trying to search for all posts liked by your friends. First, we can use the me shortcut and the friends function to get your friends as me/friends. Then we can apply the stories-liked function to this set of people to get the stories liked by any of them: me/friends/stories-liked.
    • Imagine you are trying to search for all posts in the Effective Altruism Facebook group containing the word "malaria" or "alignment". This group has group ID 437177563005273. Since we want posts, we can use the stories-in function to go from the group to its posts. We can also use the stories-keyword function to go from each of the keywords to posts containing those keywords. In effect, we are trying to search for stories-in(effective altruism) and (stories-keyword(malaria) or stories-keyword(alignment)). Using postfix notation, we can convert this to str/malaria/stories-keyword/str/alignment/stories-keyword/union/437177563005273/stories-in/intersect. (Unfortunately, as of the time of this writing, Facebook fails to return posts containing one of the keywords.) Note that this example could have been understood using the previous, atomic conditions-only view. However, having the functions view in mind allows you to assign a meaning to each word such as "stories-keyword" and "stories-in".

Exploring Existing Interfaces

  1. Understand what a web application interface can do for you. Several web application interfaces exist to make building graph search queries easier. In a web application interface, you type words or numbers into boxes or select options, then hit "Submit" (or similar) to get your search results. This means you don't need to know anything about the graph search query syntax or how to write atomic conditions. This makes such interfaces useful for quickly getting started. On the other hand, none of the existing interfaces are very advanced, in the sense of being able to craft arbitrary queries with arbitrary conditions.
  2. Know some of the more well-known and feature-rich interfaces. Some of the more well-known and feature-rich interfaces are:
    • Facebook Scanner (stalkscan.com)[17]
    • Facebook Search Tool by IntelTechniques.com[18]
    • Facebook Graph Search Generator (graph.tips)[19]
  3. Consider finding other interfaces. If you don't like any of the interfaces listed above, consider looking for others using search terms like "Facebook graph search tool".

Warnings

  • Facebook occasionally changes the queries that are possible, so some of the specific details in this tutorial may stop working in the future.
  • In some queries, Facebook will try to show the English translation of the query in the search box and page title; for example 4/stories-by/6/stories-commented/intersect has the translation "Mark Zuckerberg's posts commented on by Dustin Moskovitz". In simples cases this English translation is usually correct, but in more complicated cases, Facebook's English translation is sometimes badly wrong. In botched queries such as 6/stories-commented/union/union/4/stories-by/intersect, the translation may sound strange ("Mark Zuckerberg's posts that are posts or are commented on by Dustin Moskovitz or are posts"). In even more complicated cases, Facebook will give up and not show an English translation. In such cases, don't worry; the query is working but the translation is just unreliable.

Related wikiHows

Sources and Citations