如何创建IE8新增即时搜索建议功能Search AutoSuggestions

      昨天在美国的下午,微软发布了IE8 BETA2Tongue out在新版的IE8 Beta2中微软已经决定扩展OpenSearch一个新的搜索建议数据格式,用XMLjson表示 。新格式将显示实时搜索结果,摘要,图像,甚至搜索结果分类。而任何网站所有者只要提供适当的格式即可,本文将教你如何添加搜索建议,但在看本文之前,强烈建议请先看我的以前的一篇文章 “协助用户搜寻您的网站 { 创建一个OpenSearch }”来了解一下OpenSearch。

      我们之前可以在一些工具栏中见到此功能,Google 最近也启用了这个功能,现在 IE8 Beta2 中已经内置了这个。IE8 Beta2 的这个功能不仅提供了无缝搜索体验,还支持一些出色的搜索服务提供商的功能:

在下拉建议列表中,包括最流行的搜索建议,以及浏览历史中的记录。

Search suggestion的数据文件是通过一个新的MIME类型引用在OpenSearchDescription文件中的Url元素: application/x-suggestions+xml,如下:

<Url type="application/x-suggestions+xml" template="http://localhost:2192/SuggestionsXml.aspx?k={searchTerms}" />

前面已经说过 - 搜索建议数据格式可以是JSON或XML表示,这里演示XML格式,XML Search Suggestions 格式化规格如下(来自MSDN):

<?xml version="1.0"?>
<SearchSuggestion>
    <Query>xbox</Query>
    <Section>
    <Separator title="My Text Suggestions"/>
    <Item>
        <Text>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 walkthroughs</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>

如何创建Search AutoSuggestions

1.创建一个 OpenSearch Description 文件( provider.xml )

<?xml version="1.0" encoding="UTF-8" ?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
    <ShortName>我的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.添加Search Suggestions 到  OpenSearch Description 文件,,我已经在上面( 步骤1 )写过了,就是这句:

<Url type="application/x-suggestions+xml" template="http://localhost:2192/SuggestionsXml.aspx?k={searchTerms}" />

 

3.注意:因为 URL元素的template 属性值 是以GET的形式向 http://localhost:2192/SuggestionsXml.aspx?k={searchTerms}  传QueryString值,,

我想说的是现在要创建一个处理程序 SuggestionsXml.aspx,而这个页面文件就是动态生成 XML Search Suggestions  数据的。

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>", "查询无数据。" );
                        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.创建Search Providers

<a href="#" onclick="window.external.AddSearchProvider('provider.xml')">添加SearchProvider</a>

5.测试下效果。

 

没有可执行查询到数据时:

下载[175k]

Happy 共享此文 :
posted @ 2008-08-29 22:37  真见  阅读(2403)  评论(1编辑  收藏  举报