Search code examples
azurebingbing-api

How do you form a compound query using Bing Marketplace API?


I have an application that is using the new Bing API from the Azure Datamarketplace. I used to be able to query the Bing API using a simple syntax using OR AND, etc. That doesn't seem to work in the new API.

Old syntax:

"Jacksonville Jaguars" OR "NFL Jaguars" OR "Atlanta Falcons"

That would give me a query with any of those phrases (I am making a rt_Sports query for news).

I am calling HttpEncode on the query first, but am still not getting results. It works if I remove all the " marks, but then I am getting results sometimes for news about Falcons and Jaguars (the animals)... Not what I wanted.

Anyone have any idea how you can form a query that takes multiple phrases?

I have tried to not use the OR, not use the ', use a ", use the | instead of OR. All of these work against BING the website, just not in the API.

I just tried this via Bing and got 36 Million results:

NFL Football | Seattle Seahawks | New York Giants | Dallas Cowboys | New Orleans Saints | New England Patriots | Jacksonville Jaguars

Same thing in the API returns 0.

I got an email from a friend who I also emailed this question out to and his thought was that I was going about it wrong. That there should be a way to form a LINQ query off the Bing object with multiple where clauses.

But I don't see how that would be possible. You allow a BingSearchContainer and then call the News method on the container. The News method has only a single Query parameter.

var bingContainer = new Bing.BingSearchContainer(new Uri("https://api.datamarket.azure.com/Bing/Search"));

bingContainer.Credentials = new NetworkCredential(App.BingAPIAccountKey, App.BingAPIAccountKey);

string unifiedQuery = "NFL Football | Jacksonville Jaguars | Atlanta Falcons";

var newsQuery = bingContainer.News(unifiedQuery, null, "en-US", "Strict", null, null, null, "rt_Sports", "Relevance");

newsQuery.BeginExecute(BingNewsResultLoadedCallback, newsQuery);

Solution

  • Try changing unifiedQuery to the following:

    var unifiedQuery = "'NFL Football' or 'Jacksonville Jaguars' or 'Atlanta Falcons'";

    I tried something very similar to your sample code, using this format for the query string, and it worked for me:

    var bingUri = new Uri("https://api.datamarket.azure.com/Bing/Search/v1/", UriKind.Absolute);
    var bingContainer = new BingSearchContainer(bingUri);
    bingContainer.Credentials = new NetworkCredential(BingAPIUserName, BingAPIAccountKey);
    var unifiedQuery = "'NFL Football' or 'Jacksonville Jaguars' or 'Atlanta Falcons'";
    
    var newsQuery = bingContainer.News(unifiedQuery, null, "en-US", "Strict", null, null, null, "rt_Sports", "Relevance");
    
    var results = newsQuery.Execute();
    foreach (var item in results)
    {
        Console.WriteLine(item.Title);
    }
    

    Here are my results:

    Fantasy Football 2012: Ranking the Top 25 RBs
    NFL Football No Longer Just a Sunday Game
    Ravens Notebook: Ed Reed decided to play in game vs. Falcons since he 'wasn't doing anything else'
    PrimeSport Partners with Jacksonville Jaguars to Offer Tickets and Official Fan
    Packages for all Home and Away Games in 2012 Season
    Jaguars cut former Ravens wide receiver Lee Evans
    Falcons left tackle Baker finally feels healthy
    Jaguars release veteran WR Lee Evans
    NFC West: 2012 NFL Training Camp
    Atlanta Falcons 2012 NFL TV Schedule
    Jaguars training camp: Veteran WR Lee Evans released
    Jaguars score 18 points in second half to beat Giants 32-31
    Jacksonville Jaguars put running back Maurice Jones-Drew on reserve/did not report list
    Postcard from camp: Falcons
    Questions abound as NFL preseason opens in earnest
    NFL fantasy football: Ryan Mathews loses value
    

    The format for the unifiedQuery string is basically the OData URI query string format. For a full description of how these query strings work, check out the OData URI conventions documentation at http://www.odata.org/documentation/uri-conventions.