Search code examples
javatwitter4j

How to exclude retweets from my search query results Options


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


Solution

  • 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!