利用Bing API开发的搜索工具(MVC+WCF)

      Bing是微软公司的一款搜索引擎,跟一般的搜索引擎一样提供一些常规的搜索功能,至于是否足够强大,那就得看用户的实际需求了。像其他搜索引擎一下,bing也为开发者提供了调用搜索服务的api,鼓励开发人员进行一些第三方的搜索工具的开发,详情可登陆http://www.bing.com/toolbox/bingdeveloper/了解。codeproject最近有一篇文章介绍了如何使用bing 的api去开发简单的搜索客户端,今天自己也鼓捣了一下,做了个简单的搜索工具,是基于wcf和mvc开发的。先看看基本的成果:

基本搜索界面,支持网页、新闻和图片、视频搜索

搜索网页的结果页,利用bing的强大api基本上能达常见搜索引擎的功能

下面来说一下基本的开发步骤:

1、首先需要到bing的开发中心注册一个账号,http://www.bing.com/toolbox/bingdeveloper/,需要一个msn账号登录,然后再注册一个applicationId,过程非常简单且友好。

2、保存或记录好创建的applicationId,然后用这个id去调用bing的搜索服务。bing的搜索服务目前支持以下三种方式调用xml,json,soap。我个人比较偷懒,json和xml方式都是基于客户端,但写起代码来,非常的低效。我选择了soap方式,借助wcf的强大的客户端功能,调用soap的服务非常简单。下面是soap的调用步骤(其他方式请参考bing的api)。

3、用svcutil工具生成服务的客户端,服务的公开地址是http://api.search.live.net/search.wsdl?AppID=YOUR_APPID,AppID就是第一步中生成的ApplicationID,得到的应该是个功能完备的代理类,包含所用到的Model类以及contract类。跟其他wcf的自动生成客户端类型类似,包含一个client类,这里生成的是BingPortTypeClient,包含一个Search对外公开的方法。至于配置文件,用工具生成的基本ok了。

4、接下来的调用,就非常简单了,bing提供的api简单易懂,请求参数中需提供applictionId,查询关键字以及需要搜索的数据类型。

View Code
            SearchRequest request = new SearchRequest();
            request.AppId = WebConfigurationManager.AppSettings["BingSearchAppId"];//applicationID
            request.Query = q;//查询关键字
            request.Version = "2.0";
            request.Market = "zh-cn";//语言
            request.Sources = new SourceType[] { sourceType.Value };//查询类型
SearchResponse response = null;

            using (var client = new BingPortTypeClient())
            {
                try
                {
                    response = client.Search(request);
                }
                catch (Exception ex)
                {
                    ViewData["message"] = "搜索失败";
                }
            }

bing搜索支持的类型列表(从网上拷贝的,基本就是那个意思,搜索PHONEBOOK可理解为搜索黄页,示例中有个这个功能)

SourceType Description
WEB This sourcetype searches the query string and gets you the list of avalable crawn result based on its inbuilt automation ranking algorithm. This represents the basic search algorithm for Bing Services
IMAGE Returns the list of images relevant to the query.
VIDEO Returns List of video result for the searched query
AD Returns Advertisement links relevant to the query
NEWS News based entries for the current query based on location passed if any.
RELATEDSEARCH A unique feature that enables the Bing service to automatically determine the searches that are relevant to the current searches and display the result.
SPELL Spell Feature enables to automaticaly determine correct spellings of the word passed in the query from its database.
TRANSLATE It translates three sentences or less passed to the bing service from one language to another based on specified Target Language ID.
INSTANTANSWER This is another unique feature to enable you to get Instant answers from your current query. It gives authoratitive answers to your questions
PHONEBOOK Just on your imagination, it searches phone numbers. This is another great feature of Bing Services.
MOBILEWEB Returns shrink output for mobile browsers.

5、搜索得到的结果集合也很直接明了,按照自己的需求可自由定制结果的显示方式,感觉这种方式用MVC来做最适合不过了。接下来就都是页面展示的组合,没有什么好值得详细描述的了。

   具体源代码请下载 BingSearchWebSite

  做的比较简单,主要是体验一下,还可以继续的优化。不过调用API搜索还是有些优势,可能是没有监控那么严的原因,得到结果的尺度比较的宽,一些所谓的敏感内容也能搜索得到,特别是将请求的Market改成"en-us"之后,自由多了,但连接还是打不开的,顶多让你看个缓存页面。

参考:

http://www.codeproject.com/KB/cs/BingSearch.aspx

 

     

原文地址:https://www.cnblogs.com/shenba/p/2264439.html