How to create an instant search recommendation feature in IE8 search autosuggestions

Source: Internet
Author: User

Yesterday afternoon in the United States, Microsoft released IE8 beta2. In the new version of IE8 beta2, Microsoft has decided to extend opensearch to a new recommended data format, represented in XML or JSON. The new format displays real-time search results, summaries, images, and even search result classifications. Any website owner only needs to provide the appropriate format. This article will teach you how to add search recommendations,However, before reading this article, we strongly recommend that you read my previous article"Assist users in searching for your website {creating an opensearch}"To learn about opensearch.

We can see this feature in some toolbar before, and Google has recently enabled this feature, which is now built in IE8 beta2. This feature of IE8 beta2 not only provides a seamless search experience, but also supports some outstanding search service provider features:

The drop-down list includes the most popular search recommendations and browsing history records.

The data file of search suggestion is a URL element referenced in the opensearchdescription file through a new MIME type:Application/X-suggestions + XML, as follows:

<URL Type= "Application/X-suggestions + XML" Template= "Http: // localhost: 2192/suggestionsxml. aspx? K = {searchterms }" />

As mentioned above, the recommended data format for search can be JSON or XML. Here we will demonstrate the XML format. The XML search suggestions format specifications are as follows (from msdn ):

 <?  XML   Version  = "1.0" ? >  <  Searchsuggestion  >      <  Query  > Xbox </  Query  >      <  Section  >      < Separator   Title  = "My text suggestions"  />      <  Item  >          <  Text  > The Xbox 360 </  Text  >          <  Description  > The official Xbox website from Microsoft </  Description  >          <  URL  > Http://www.xbox.com </  URL  >      </  Item  >      <  Item  >          <  Text  > Xbox cheats </  Text  >          <  Description  > Codes and throughs </  Description  >          <  URL  > Http://www.example.com/xboxcheatcodes.aspx </  URL  >      </  Item  >      <  Item  >          <  Text  > Xbox 360 games </ Text  >          <  Description  > Games and accessories </  Description  >          <  URL  > Http://www.example.com/games </  URL  >      </  Item  >      <  Separator   /> ...
 
</Section></Searchsuggestion>
 
 

How to Create search autosuggestions

1. Create an opensearch description file (provider. XML)

 <?  XML   Version  = "1.0"   Encoding  = "UTF-8" ? >  <  Opensearchdescription   Xmlns  = "Http://a9.com/-/spec/opensearch/1.1"  >      <  Shortname  > My search autosuggestions </ Shortname  >      <  URL   Type  = "Text/html"   Template  = "Http://zzk.cnblogs.com/s? W = {searchterms }"   />      <  URL   Type  = "Application/X-suggestions + XML"   Template  = "Http: // localhost: 2192/suggestionsxml. aspx? K = {searchterms }"   />      <  Image   Height = "16"   Width  = "16"   Type  = "Image/icon"  > Http://www.cnblogs.com/favicon.ico </  Image  >      <  Inputencoding  > GBK </  Inputencoding  >  </  Opensearchdescription  > 
 
 

2. Add search suggestions to the opensearch description file. I have already written the following in Step 1:

<URL Type= "Application/X-suggestions + XML" Template= "Http: // localhost: 2192/suggestionsxml. aspx? K = {searchterms }" />

 

3. Note: Because the URL element'sThe template property value is in the form of getHttp: // localhost: 2192/suggestionsxml. aspx? K = {searchterms} transmits the querystring value ,,

What I want to say is to create a processingProgramSuggestionsxml. aspx, and this page file dynamically generates XML search suggestions data.

 Using System; Using System. text; Using System. Data; Namespace Searchautosuggestions { Public   Partial   Class Suggestionsxml: system. Web. UI. Page { Protected  Void Page_load ( Object Sender, eventargs e ){ If (! This . Page. ispostback ){ If (Request. querystring [ "K" ]! = Null ){ String Key = server. urldecode (request. querystring [ "K" ]. Tostring ()); // Int index;                      // If (Int. tryparse (request. querystring ["I"]. tostring (), Out index )){ Stringbuilder sb =New Stringbuilder (); sb. append ( "<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> " ); Sb. append ( "<Searchsuggestion version = \" 2.0 \ "xmlns = \" http://opensearch.org/searchsuggest2\ "> \ n" ); Sb. appendformat ( "<Query> {0} </query> \ n" , Key); sb. appendformat ( "<Section title = \" {0} \ "> \ n" , Key); datarow [] DRS = getonedatatable (). Select ( String . Format ( "Name like '% {0} % '" , Key )); If (Drs. length> 0 ){Foreach (Datarow Dr In Drs) {sb. append ( "<Item> \ n" ); Sb. appendformat ( "<Text> ID: {0} </text> \ n" , Dr [ "ID" ]. Tostring (); sb. appendformat ( "<Description> name: {0} </description> \ n" , Dr [ "Name" ]. Tostring (); sb. appendformat ( "<URL> http://www.cnblogs.com/default.aspx? Page = {0} & amp; paging = 10 </URL> \ n" , Dr [ "ID" ]. Tostring (); sb. append ( "<Image Source = \" http://news.cnblogs.com/images/logo/ie7.jpg\ "alt = \" alttip \ "width = \" 50 \ "Height = \" 50 \ "/> \ n" ); Sb. append ( "</Item> \ n" );}} Else {Sb. append ( "<Item> \ n" ); Sb. appendformat ( "<Title> {0} </title>" , Key); sb. appendformat ( "<Description> {0} </description>" , "No data is queried. " ); Sb. append ("</Item>" );} Sb. append ( "</Section> \ n" ); Sb. append ( "</Searchsuggestion>" ); Response. contenttype = "Text/XML" ; Response. Write (sb. tostring ()); //} }}} Private Datatable getonedatatable () {var dt = New Datatable (); DT. Columns. Add ( "ID" ); DT. Columns. Add ( "Name" );For ( Int I = 0; I <10; I ++) {DT. Rows. Add (Dt. newrow (); DT. Rows [I] [ "ID" ] = I + 1; DT. Rows [I] [ "Name" ] = I + 1 + "Name" ;} Return DT ;}}}

4. Create search providers

<A Href= "#" Onclick= "Window. External. addsearchprovider ('provider. xml ')">Add searchprovider</A>
 
 

5. test the effect.

 

When no data can be queried:

Download [175 K]

Share this article:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.