SharePoint search 功能的定制开发

概要

  客户要求改善 SharePoint 2007的搜索功能,我们要自定义开发搜索功能,一翻调查和研究,sharepoint 的搜索用到KeywordSearchQuery和FullTextSearchQuery 两种方式,因为我们要用到自定义的sharepoint list字段做为过虑条件,所以采用FullTextSearchQuery 来开发。

 

设计

FullTextSearchQuery 做sharepoint 2007二次开发要用到以下组件:

using Microsoft.Office.Server;
using Microsoft.Office.Server.Search;
using Microsoft.Office.Server.Search.Query;

 

 

在sharepoint 2007中,我们如下scope():

image

 

默认的有people和all sites两个scope。我们以All sites举例。

 

sharepoint Content sources 在爬虫时的流程图(方便理解scope()内容的来源)

2

 

 

以下是查询的流程图:

image

 

 

用户使用查询时,sharepoint对于用户的安全检查:

3

 

以上说明仅供读者了解sharepoint查询时要做的工作.

Microsoft.SharePoint.Search.Query 命名空间包括三个查询类:

  • FullTextSqlQuery 使用此类可执行 SQL 语法搜索查询。

  • KeywordQuery 使用此类可执行关键字语法搜索查询。

为自定义搜索应用程序选择查询类

若要确定用于自定义搜索应用程序(FullTextSqlQueryKeywordQuery)的适当的类,请考虑希望应用程序代码支持的搜索查询中的复杂级别。

    下面的列表标识仅受使用 FullTextSqlQuery 类的 SQL 搜索语法支持的附加查询元素:FREETEXT、CONTAINS、LIKE和ORDER BY.

代码设计

    public class SharePointSearch
    {

        #region Fields

        string queryStr = @"select hitHighlightedProperties,size,Write, author, path,
                            hitHighlightedSummary,title,contentclass
                            from scope() where " +
                            "\"scope\" = '{0}' AND FREETEXT(defaultproperties, '{1}') ";
        string conditionSearchSource = " AND CONTAINS(path,'{0}')";

        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor.
        /// </summary>
        public SharePointSearch()
        {
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// 
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="searchSource"></param>
        /// <param name="keyword"></param>
        /// <param name="rowLimit"></param>
        /// <returns></returns>
        public DataTable GetSearchResultByKeyWord(string scope, string searchSource, string keyword, int rowLimit)
        {
            string query = string.Format(this.queryStr, scope, keyword);
            if (!string.IsNullOrEmpty(searchSource))
                query += string.Format(this.conditionSearchSource, searchSource);

            return this.CustomSearch(query, rowLimit);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="query"></param>
        /// <param name="rowLimit"></param>
        /// <returns></returns>
        public DataTable GetSearchResultByQuery(string query, int rowLimit)
        {
            return this.CustomSearch(query, rowLimit);
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Excute search query
        /// </summary>
        /// <param name="query"></param>
        /// <param name="rowLimit"></param>
        /// <returns></returns>
        private DataTable CustomSearch(string query, int rowLimit)
        {
            DataTable result = new DataTable();
            string url = SPContext.Current.Web.Url;

            using (SPSite site = new SPSite(url))
            {
                ServerContext context = ServerContext.GetContext(site);

                FullTextSqlQuery fullQuery = new FullTextSqlQuery(site);
                fullQuery.Culture = System.Globalization.CultureInfo.InvariantCulture;
                fullQuery.QueryText = query;
                fullQuery.ResultTypes = ResultType.RelevantResults;
                fullQuery.EnableStemming = false;
                fullQuery.IgnoreAllNoiseQuery = true;
                fullQuery.TrimDuplicates = true;

                fullQuery.KeywordInclusion = KeywordInclusion.AnyKeyword;
                fullQuery.RowLimit = rowLimit;
                if (SPSecurity.AuthenticationMode != System.Web.Configuration.AuthenticationMode.Windows)
                    fullQuery.AuthenticationType = QueryAuthenticationType.PluggableAuthenticatedQuery;
                else
                    fullQuery.AuthenticationType = QueryAuthenticationType.NtAuthenticatedQuery;

                ResultTableCollection rt = fullQuery.Execute();
                ResultTable resultTable = rt[ResultType.RelevantResults];
                result.Load(resultTable, LoadOption.OverwriteChanges);
            }

            return result;
        }

        #endregion
    }

 

而为什么FullTextSqlQuery 的属性是这样设置的,因为能过moss工具产生的xml结果图如下:

<?xml version="1.0" encoding="utf-8" ?>
<QueryPacket xmlns="urn:Microsoft.Search.Query" Revision="1000">
<Query domain="QDomain">
 <SupportedFormats><Format>urn:Microsoft.Search.Response.Document.Document</Format></SupportedFormats>
 <Context>
   <QueryText language="en-US" type="MSSQLFT"><![CDATA[ SELECT Title, Rank, Size, Description, Write, Path FROM portal..scope() WHERE FREETEXT(DefaultProperties, 'test') AND  ( ("SCOPE" = 'All Sites') )   ORDER BY "Rank" DESC ]]></QueryText>
 </Context>
 <Range><StartAt>1</StartAt><Count>20</Count></Range>
 <EnableStemming>false</EnableStemming>
 <TrimDuplicates>true</TrimDuplicates>
 <IgnoreAllNoiseQuery>true</IgnoreAllNoiseQuery>
 <ImplicitAndBehavior>false</ImplicitAndBehavior>
 <IncludeRelevanceResults>true</IncludeRelevanceResults>
 <IncludeSpecialTermResults>false</IncludeSpecialTermResults>
 <IncludeHighConfidenceResults>false</IncludeHighConfidenceResults>
</Query></QueryPacket>

 

 

如果sharepoint里item内容语言是en-us的话,一定要在IE浏览器里设置:

image

把english[en]放到最上面。

 

 

 

作者:spring yang

出处:http://www.cnblogs.com/springyangwc/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

posted @ 2011-07-08 19:42  spring yang  阅读(2883)  评论(0编辑  收藏  举报