Actually, if companies like Facebook and Google are taking part in this (like many people assure) they probably have developed code like this for their services.
The number of people that would need to cooperate to keep the existence of this type of code secret is unfeasibly high. It's much more likely that the NSA simply has the companies' TLS keys and can intercept "secure" traffic. They then would have the technology to put in into a structured format and enable querying over the sniffed data.