• 推荐
  • 评论
  • 收藏

SharePoint search 功能的定制开发

2022-12-10    932次浏览

概要

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

设计

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

1 using Microsoft.Office.Server;
2 using Microsoft.Office.Server.Search;
3 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 使用此类可执行关键字语法搜索查询。

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

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

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

代码设计

01 public class SharePointSearch
02 {
03  
04     #region Fields
05  
06     string queryStr = @"select hitHighlightedProperties,size,Write, author, path,
07                         hitHighlightedSummary,title,contentclass
08                         from scope() where " +
09                         "\"scope\" = '{0}' AND FREETEXT(defaultproperties, '{1}') ";
10     string conditionSearchSource = " AND CONTAINS(path,'{0}')";
11  
12     #endregion
13  
14     #region Constructor
15  
16     /// <summary>
17     /// Default constructor.
18     /// </summary>
19     public SharePointSearch()
20     {
21     }
22  
23     #endregion
24  
25     #region Public Methods
26  
27     /// <summary>
28     ///
29     /// </summary>
30     /// <param name="scope"></param>
31     /// <param name="searchSource"></param>
32     /// <param name="keyword"></param>
33     /// <param name="rowLimit"></param>
34     /// <returns></returns>
35     public DataTable GetSearchResultByKeyWord(string scope, string searchSource, string keyword, int rowLimit)
36     {
37         string query = string.Format(this.queryStr, scope, keyword);
38         if (!string.IsNullOrEmpty(searchSource))
39             query += string.Format(this.conditionSearchSource, searchSource);
40  
41         return this.CustomSearch(query, rowLimit);
42     }
43  
44     /// <summary>
45     ///
46     /// </summary>
47     /// <param name="query"></param>
48     /// <param name="rowLimit"></param>
49     /// <returns></returns>
50     public DataTable GetSearchResultByQuery(string query, int rowLimit)
51     {
52         return this.CustomSearch(query, rowLimit);
53     }
54  
55     #endregion
56  
57     #region Private Methods
58  
59     /// <summary>
60     /// Excute search query
61     /// </summary>
62     /// <param name="query"></param>
63     /// <param name="rowLimit"></param>
64     /// <returns></returns>
65     private DataTable CustomSearch(string query, int rowLimit)
66     {
67         DataTable result = new DataTable();
68         string url = SPContext.Current.Web.Url;
69  
70         using (SPSite site = new SPSite(url))
71         {
72             ServerContext context = ServerContext.GetContext(site);
73  
74             FullTextSqlQuery fullQuery = new FullTextSqlQuery(site);
75             fullQuery.Culture = System.Globalization.CultureInfo.InvariantCulture;
76             fullQuery.QueryText = query;
77             fullQuery.ResultTypes = ResultType.RelevantResults;
78             fullQuery.EnableStemming = false;
79             fullQuery.IgnoreAllNoiseQuery = true;
80             fullQuery.TrimDuplicates = true;
81  
82             fullQuery.KeywordInclusion = KeywordInclusion.AnyKeyword;
83             fullQuery.RowLimit = rowLimit;
84             if (SPSecurity.AuthenticationMode != System.Web.Configuration.AuthenticationMode.Windows)
85                 fullQuery.AuthenticationType = QueryAuthenticationType.PluggableAuthenticatedQuery;
86             else
87                 fullQuery.AuthenticationType = QueryAuthenticationType.NtAuthenticatedQuery;
88  
89             ResultTableCollection rt = fullQuery.Execute();
90             ResultTable resultTable = rt[ResultType.RelevantResults];
91             result.Load(resultTable, LoadOption.OverwriteChanges);
92         }
93  
94         return result;
95     }
96  
97     #endregion
98 }

 

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

01 <?xml version="1.0" encoding="utf-8" ?>
02 <QueryPacket xmlns="urn:Microsoft.Search.Query" Revision="1000">
03 <Query domain="QDomain">
04  <SupportedFormats><Format>urn:Microsoft.Search.Response.Document.Document</Format></SupportedFormats>
05  <Context>
06    <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>
07  </Context>
08  <Range><StartAt>1</StartAt><Count>20</Count></Range>
09  <EnableStemming>false</EnableStemming>
10  <TrimDuplicates>true</TrimDuplicates>
11  <IgnoreAllNoiseQuery>true</IgnoreAllNoiseQuery>
12  <ImplicitAndBehavior>false</ImplicitAndBehavior>
13  <IncludeRelevanceResults>true</IncludeRelevanceResults>
14  <IncludeSpecialTermResults>false</IncludeSpecialTermResults>
15  <IncludeHighConfidenceResults>false</IncludeHighConfidenceResults>
16 </Query></QueryPacket>

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

image

 

把english[en]放到最上面。

作者:spring yang

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

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

原文地址:https://www.cnblogs.com/Leo_wl/p/2102601.html