Advanced Search

Dec 27, 2009 at 4:23 AM

I understand how to do basic searching on 1 term:

var tweets = from tweet in twitterDataContext.Search
                    where tweet.Type == SearchType.Search
                    && tweet.Query == searchTerm
                    select tweet;

Though is there a way to do advanced searches? (referenced from: http://search.twitter.com/advanced)

Thanks!

Daniel

Coordinator
Dec 27, 2009 at 7:50 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Feb 21, 2010 at 7:00 AM

Hi Daniel,

You can perform advanced queries by including search operators in your query.  Here's an example:

        /// <summary>
        /// shows how to perform a twitter search, using a Twitter Search operator
        /// </summary>
        /// <param name="twitterCtx">TwitterContext</param>
        private static void SearchOperatorDemo(TwitterContext twitterCtx)
        {

            var queryResults =
                from search in twitterCtx.Search
                where search.Type == SearchType.Search &&
                      search.Query == "to:JoeMayo"
                select search;

            foreach (var search in queryResults)
            {
                // here, you can see that properties are named
                // from the perspective of atom feed elements
                // i.e. the query string is called Title
                Console.WriteLine("\nQuery:\n" + search.Title);

                foreach (var entry in search.Entries)
                {
                    Console.WriteLine(
                        "ID: {0}, Source: {1}\nContent: {2}\n",
                        entry.ID, entry.Source, entry.Content);
                }
            }
        }

You can find the available operators at: http://search.twitter.com/operators.

This will work in a lot of cases, but I'm still going to add special parameters for advanced queries, since the query itself can only contain 140 characters.

Joe