17

I'm trying to filter my twitter4j stream with the following code :

    TwitterStream twitterStream = getTwitterStreamInstance();

    // Listener
    twitterStream.addListener(listener);

    // Filter
    FilterQuery filtre = new FilterQuery();
    String[] keywordsArray = { "iphone", "samsung" };
    filtre.track(keywordsArray);
    twitterStream.filter(filtre);

    // Listening
    twitterStream.sample();

But the result is, for example :

27/59 - "Taking a risk over something only means that you want it more than anything"
28/63 - The more attractive you are, the more awkward I am.
29/64 - the thing about pain is that it demands to be felt

And I don't recover the keywords I want to follow, where is the problem ?

Apaachee
  • 900
  • 2
  • 10
  • 32

1 Answers1

33

You don't need:

twitterStream.sample();

If you remove it, you should start seeing Tweets matching your filter query.

When you invoke filter your listener will receive filtered Tweets, however because you then invoke sample you are effectively replacing the filtered-stream your listener is receiving with the sample steam, which is a random selection Tweets.

So in other words call either sample or filter, but not both.

Jonathan
  • 20,053
  • 6
  • 63
  • 70
  • Great ! It was my problem. I also have another small question. If I choose to track a keyword that is not used much. Am I sure to reap all the tweets about? – Apaachee May 21 '13 at 13:40
  • 1
    @Apaachee I can't say this with completely certainty but I would expect so. I skimmed the docs and didn't notice anything significant - with the exception of [withheld content](http://dev.twitter.com/blog/new-withheld-content-fields-api-responses). – Jonathan May 21 '13 at 13:56
  • @Apaachee No problem - I'm glad I could help. :-) I'm not sure if you know, but you can [mark an answer as 'accepted'](http://meta.stackexchange.com/a/5235/195234) if you feel it answers your question. – Jonathan May 21 '13 at 14:39
  • @Apaachee It is possible that you might miss out on the unindexed tweets. Twitter does not take any guarantee of providing you with all of them. – vishalv2050 Jan 23 '14 at 12:51
  • @vishalv2050 Yes, but if the frequency of the tweets is enough low, you got them. So, with a big tweets filter, you can have all the tweets. – Apaachee Jan 23 '14 at 14:02