Search code examples
twittertwitter4j

Twitter4j friends search not ordered by relevency


I am trying to get suggestions or autocomplete for Twitter friends. This is my code:

ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setOAuthConsumerKey(PropertiesLoader.getProperty("twitter_consumer_key"));
    builder.setOAuthConsumerSecret(PropertiesLoader.getProperty("twitter_consumer_secret"));
    Configuration configuration = builder.build();
    TwitterFactory factory = new TwitterFactory(configuration);
    Twitter twitter = factory.getInstance();

    twitter.setOAuthAccessToken(new AccessToken("[TOKEN]", "[SECRET]"));
    ResponseList<twitter4j.User> users = null;
    try {
        users = twitter.searchUsers("sah", 1);
    } catch (TwitterException e) {
        e.printStackTrace();
    }

    System.out.println(users);

This returns me the list of user suggestions. But it is not ordered by relevancy. When I search on search.twitter.com it is ordered as first my friends who match the query and then unknown people. But here it is not ordered this way. How do I use this to order it correctly?


Solution

  • The documentation for User Search shows that there is no way to order the results. In this respect, the API is different from the website.

    That said, there is a way that you can manually sort through the users returned. Your returned set of results should include entities.

    In that entity, you will see some fields such as

    "friends_count": 28,
    "profile_background_image_url": "http://a0.twimg.com/656932170/urr667rpqtg6l7psohaf.png",
    "default_profile_image": false,
    "statuses_count": 338,
    "screen_name": "example",
    "following": true
    

    That last one "following": true shows that the requesting user is following that user.

    If you are in any doubt about the nature of the relationship, you can use friendships/show.