SharePoint search 功能的定制开发
概要
客户要求改善 SharePoint 2007的搜索功能,我们要自定义开发搜索功能,一翻调查和研究,sharepoint 的搜索用到KeywordSearchQuery和FullTextSearchQuery 两种方式,因为我们要用到自定义的sharepoint list字段做为过虑条件,所以采用FullTextSearchQuery 来开发。
设计
FullTextSearchQuery 做sharepoint 2007二次开发要用到以下组件:
1 2 3 | using Microsoft.Office.Server; using Microsoft.Office.Server.Search; using Microsoft.Office.Server.Search.Query; |
在sharepoint 2007中,我们如下scope():
默认的有people和all sites两个scope。我们以All sites举例。
sharepoint Content sources 在爬虫时的流程图(方便理解scope()内容的来源)
以下是查询的流程图:
用户使用查询时,sharepoint对于用户的安全检查:
以上说明仅供读者了解sharepoint查询时要做的工作.
Microsoft.SharePoint.Search.Query 命名空间包括三个查询类:
-
FullTextSqlQuery 使用此类可执行 SQL 语法搜索查询。
-
KeywordQuery 使用此类可执行关键字语法搜索查询。
为自定义搜索应用程序选择查询类
若要确定用于自定义搜索应用程序(FullTextSqlQuery 或 KeywordQuery)的适当的类,请考虑希望应用程序代码支持的搜索查询中的复杂级别。
下面的列表标识仅受使用 FullTextSqlQuery 类的 SQL 搜索语法支持的附加查询元素:FREETEXT、CONTAINS、LIKE和ORDER BY.
代码设计
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | 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结果图如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?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浏览器里设置:
把english[en]放到最上面。
作者:spring yang
出处:http://www.cnblogs.com/springyangwc/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架