利用Bing API開發的搜尋工具(MVC+WCF)

來源:互聯網
上載者:User

      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

 

 

 

     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.