如何使用Bing Marketplace API形成复合查询?

时间:2012-08-11 03:03:23

标签: azure bing bing-api

我的应用程序正在使用Azure Datamarketplace中的新Bing API。我曾经能够使用OR AND等使用简单的语法查询Bing API。这似乎不适用于新的API。

旧语法:

“Jacksonville Jaguars”或“NFL Jaguars”或“Atlanta Falcons”

这会给我一个查询任何这些短语(我正在为新闻做rt_Sports查询)。

我首先在查询上调用HttpEncode,但仍然没有得到结果。如果我删除了所有的“标记,但是我有时会得到有关猎鹰和美洲虎(动物)的新闻......它不是我想要的。

任何人都知道如何构建一个包含多个短语的查询?

我试图不使用OR,不使用',使用',使用|而不是OR。所有这些都反对BING网站,而不是在API中。

我刚刚通过Bing尝试了这个并获得了3600万结果:

NFL橄榄球|西雅图海鹰队|纽约巨人队|达拉斯牛仔队|新奥尔良圣徒队新英格兰爱国者队杰克逊维尔美洲虎

API中的相同内容返回0。

我收到了一封朋友发来的电子邮件,我也通过电子邮件发送了这个问题,他的想法是我错了。应该有一种方法可以在具有多个where子句的Bing对象上形成LINQ查询。

但我不明白这是怎么可能的。您允许BingSearchContainer,然后在容器上调用News方法。 News方法只有一个Query参数。

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);

1 个答案:

答案 0 :(得分:1)

尝试将unifiedQuery更改为以下内容:

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

我尝试了与您的示例代码非常相似的内容,使用此格式作为查询字符串,它对我有用:

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);
}

以下是我的结果:

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

unifiedQuery字符串的格式基本上是OData URI查询字符串格式。有关这些查询字符串如何工作的完整说明,请查看http://www.odata.org/documentation/uri-conventions处的OData URI约定文档。