概要
客户要求改善 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():
默认的有people和all sites两个scope。我们以All sites举例。
sharepoint Content sources 在爬虫时的流程图(方便理解scope()内容的来源)
以下是查询的流程图:
用户使用查询时,sharepoint对于用户的安全检查:
以上说明仅供读者了解sharepoint查询时要做的工作.
Microsoft.SharePoint.Search.Query 命名空间包括三个查询类:
为自定义搜索应用程序选择查询类
若要确定用于自定义搜索应用程序(FullTextSqlQuery 或 KeywordQuery)的适当的类,请考虑希望应用程序代码支持的搜索查询中的复杂级别。
下面的列表标识仅受使用 FullTextSqlQuery 类的 SQL 搜索语法支持的附加查询元素:FREETEXT、CONTAINS、LIKE和ORDER BY.
代码设计
01 |
public class SharePointSearch |
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}')" ; |
17 |
/// Default constructor. |
19 |
public SharePointSearch() |
25 |
#region Public Methods |
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) |
37 |
string query = string .Format( this .queryStr, scope, keyword); |
38 |
if (! string .IsNullOrEmpty(searchSource)) |
39 |
query += string .Format( this .conditionSearchSource, searchSource); |
41 |
return this .CustomSearch(query, rowLimit); |
47 |
/// <param name="query"></param> |
48 |
/// <param name="rowLimit"></param> |
49 |
/// <returns></returns> |
50 |
public DataTable GetSearchResultByQuery( string query, int rowLimit) |
52 |
return this .CustomSearch(query, rowLimit); |
57 |
#region Private Methods |
60 |
/// Excute search query |
62 |
/// <param name="query"></param> |
63 |
/// <param name="rowLimit"></param> |
64 |
/// <returns></returns> |
65 |
private DataTable CustomSearch( string query, int rowLimit) |
67 |
DataTable result = new DataTable(); |
68 |
string url = SPContext.Current.Web.Url; |
70 |
using (SPSite site = new SPSite(url)) |
72 |
ServerContext context = ServerContext.GetContext(site); |
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 ; |
82 |
fullQuery.KeywordInclusion = KeywordInclusion.AnyKeyword; |
83 |
fullQuery.RowLimit = rowLimit; |
84 |
if (SPSecurity.AuthenticationMode != System.Web.Configuration.AuthenticationMode.Windows) |
85 |
fullQuery.AuthenticationType = QueryAuthenticationType.PluggableAuthenticatedQuery; |
87 |
fullQuery.AuthenticationType = QueryAuthenticationType.NtAuthenticatedQuery; |
89 |
ResultTableCollection rt = fullQuery.Execute(); |
90 |
ResultTable resultTable = rt[ResultType.RelevantResults]; |
91 |
result.Load(resultTable, LoadOption.OverwriteChanges); |
而为什么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> |
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> |
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浏览器里设置:
把english[en]放到最上面。
作者:spring yang
出处:http://www.cnblogs.com/springyangwc/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。