serprime
serprime

Reputation: 126

Twitter search: OR with Tags - but how?

I cannot search the twitter API for tweets which contain one of multiple tags.

Like: q="#tag1 OR #tag2 OR #tag3"

If I leave away the hashes and only search for words, the OR-ing works. For tags they don't. When I only use spaces, the search terms will be AND-ed, what shrinks the result...

I use the twitter4j library with:

Twitter rest = new TwitterFactory().getInstance();
Query query = new Query();
query.setQuery("#win | #fail");
QueryResult result = rest.search(query);

Isn't it possible, or didn't i use it correctly?

Upvotes: 1

Views: 2733

Answers (2)

Primus202
Primus202

Reputation: 646

Might just be easier to use twitter's REST API. You'll want to use the search query. Here's an example search url searching for #LA, #NYC or #Boston. Note the spaces and #s are all URL encoded. Just pop a URL like that into a getJSON call like below and you can easily extract your values from the returned JSON object as in the example.

var requestedData = "http://search.twitter.com/search.json?q=%23LA%20OR%20%23NYC%20OR%20%23Boston%22&callback=?"
$.getJSON(requestedData,function(ob)
{
  var firstTweet = ob.results[0].text;
  var firstTweeter = ob.results[0].from_user;
}

From there it's just a matter of looping through your results and pulling the appropriate fields which are all outlined in the JSON file if you simply visit that example search link in your browser! I don't know this TwitterFactory API but its possible they haven't updated to Twitter's new API or they're just not URL encoding appropriately. Good luck!

Upvotes: 1

Shcheklein
Shcheklein

Reputation: 6284

Try to use OR operator instead of "|":

query.setQuery("#win OR #fail");

See available Twitter search API operators here:

Using the Twitter Search API

Upvotes: 0

Related Questions