Search code examples
rubyruby-on-rails-3twitter

Exclude multi-word keywords from Twitter Search API


I have a list of keywords to be excluded from search here

KEYWORDS = %w[
    covid corona subway railway travel plane brazil ]

exclude = Twitter::KEYWORDS.split(",").join(" -")

and this is how my search query looks like

 json_response = @client.search("(javascript) -#{exclude}", lang: "en", result_type: "recent", tweet_mode: "extended", count: 100)

How can I pass multi-word keywords here to be excluded, for example keywords like "off the hand" or "game plan"?

Adding them along with the other keywords doesn't work as expected.


Solution

  • In case anyone comes back looking for the same problem, this is how I solved it:

    @client.search("(javascript) -#{exclude} -\"off the hand\" -\"game plan\", lang: "en", result_type: "recent", tweet_mode: "extended", count: 100)
    
    

    So, basically by using escape characters which allowed me to pass multi-word keywords as an exact string.