3

I'm trying to search tweets using search method in twitter4j. My code is as follows,

    public List<Tweet> searchTweets(Query searchQuery) { 
        QueryResult queryResult = twitter.search(searchQuery); 

        return queryResult != null ? queryResult.getTweets() : new 
             ArrayList<Tweet>(0); 
    } 

How do I exclude retweets from my search query results

diya
  • 6,938
  • 9
  • 39
  • 55

5 Answers5

5

I was looking for how to exclude the replies on the query search, so I found this topic. To exclude retweets, this worked well for me:

Query query = new Query("from:"+twitterAccount + " +exclude:retweets");
gmojunior
  • 292
  • 1
  • 4
  • 7
2
Query query = new Query(yourQuery + " -filter:retweets");

source

PS. I don't know the difference betweeen +exclude:retweets and -filter:retweets; both seem to do the job similarly.

Luís Soares
  • 5,726
  • 4
  • 39
  • 66
2

I would rather comment on this but I can't yet so I'll post this as an answer. You need to edit the Query by appending parameters to it. The source code for Query.java can be found in the examples folder in the twitter4j folder.

public Query(String query) {
    this.query = query;
}

Check out Twitter search (atom) API - exclude retweets. The search basis is different but the concept is the same, which is appending +exclude:retweets to the end of your string query. Try it out and do let me know if it works!

Community
  • 1
  • 1
Wei Hao
  • 2,756
  • 9
  • 27
  • 40
  • Hi Wei, I already did it which above you said, Its working fine. Thanks for updating this answer. – diya Feb 21 '12 at 03:49
0

I used a different approach than above. See my code below:

List<Status> tweets = result.getTweets();
for (Status tweet : tweets )
{
      if( !tweet.isRetweet() )
      // processing .... 
} 

It works for me. However, there might be efficiency differences between these approaches.

Jarvis
  • 117
  • 1
  • 1
  • 10
-3

You can use set in the configuration builder the option setIncludeRTsEnabled to false, so is going to remove the retweets.

ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setIncludeRTsEnabled(false);
Twitter twitter = new TwitterFactory(builder.build()).getInstance();