FullTextSQLQuery - Keyword search of just documents on one site
I was researching a problem with our search mechanism, which would not return results for certain keywords. It ended up that the number of results being returned for all sites was hitting the RowLimit before the result I wanted appeared. I needed some better filtering. Along the way I discovered a couple tips, one of which appears to be undocumented, to help restrict a search to only documents in a library and only on a given site collection. (I'm also using forms authentication.)
I created the test.aspx page (shown below) to diagnose a search problem and to experiment with search results on a live server without having to build/deploy. Just place it under C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/12/TEMPLATE/LAYOUTS (or inside another sub folder.)
Note the IsDocument=1 in the query text. This will restrict this search to only return documents in the results, not the other pages that get indexed, such as: http://xx.xx.xx.xx/My Documents/Forms/DispForm.aspx?ID=29
You can also filter the results to just the site you are currently searching (assuming you want to). If the current user has rights to other sites, those results would be returned as well unless you include the SITE= clause.
SearchTest.aspx:
<%@ Page Language="C#" %> <%@ Import Namespace="Microsoft.SharePoint" %> <%@ Import Namespace="Microsoft.SharePoint.Search" %> <%@ Import Namespace="Microsoft.SharePoint.Search.Query" %> <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="Search" Namespace="Microsoft.SharePoint.Search" Assembly="Microsoft.SharePoint.Search, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Search Test</title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Search" /> <% if (!HttpContext.Current.User.Identity.IsAuthenticated) { Response.Write("<br />Please login.<br />"); } else { if (this.IsPostBack) { string keywords = this.TextBox1.Text;
SPSite sitecol = SPContext.Current.Site; SPWeb site = SPContext.Current.Web;
// create a new full text query
FullTextSqlQuery ftQuery = new FullTextSqlQuery(sitecol); ftQuery.QueryText = @"SELECT Path FROM Scope() WHERE FREETEXT(*, '" + keywords + "') AND IsDocument=1 AND SITE='" + site.Url.ToString() + "'";
ftQuery.ResultTypes |= ResultType.RelevantResults; ftQuery.Hint = QueryHint.OptimizeWithFullTextIndex; ftQuery.KeywordInclusion = KeywordInclusion.AllKeywords; ftQuery.SiteContext = new Uri(site.Url.ToString()); ftQuery.RowLimit = 100; ftQuery.TrimDuplicates = true; ftQuery.AuthenticationType = QueryAuthenticationType.PluggableAuthenticatedQuery;
ResultTableCollection resultTables = ftQuery.Execute(); ResultTable resultTable = resultTables[ResultType.RelevantResults];
Response.Write("<br />" + "Site Collection: " + sitecol.Url.ToString() + "<br />"); Response.Write("Site: " + site.Url.ToString() + "<br />"); Response.Write("Search Keyword: " + keywords + "<br />"); Response.Write("Search Results:" + "<br />");
int count = 0;
while (true == resultTable.Read()) { if (resultTable.FieldCount > 0) { string columnName = resultTable.GetName(0); Object columnValue = resultTable.GetValue(0);
Response.Write(columnValue.ToString() + "<br />"); count++; } } Response.Write(count.ToString() + " results.<br />");
} else { this.TextBox1.Text = "Imported"; } }
%></div> </form> </body> </html>
The IsDocument=1 tip (which does not appear in the WSS SDK documentation) comes from this question:
http://www.developermania.com/newsgroups/item/316475/FullTextSqlQuery_contains_problem.aspx
I don't recall where I saw the SITE= being used (sorry.)
本文介绍了一种使用FullTextSQLQuery进行SharePoint搜索的方法,通过设置IsDocument=1和SITE=等参数来限制搜索范围,仅返回指定站点集合内的文档结果。

748

被折叠的 条评论
为什么被折叠?



