利用Sharepoint2010进行全文搜索的一个Demo

注意:要注释掉一些没必要的代码。

public class SharepointSearch
    {
        public static DataTable Search(List<string> column, List<string> scope, string searchWords, IndexServiceSearchOption searchOption)
        {
            try
            {
                QueryWebServiceProxy.QueryService queryService = new QueryWebServiceProxy.QueryService();
                queryService.Url = APJ.Util.Utility.GetAppSetting("SharepointServer");
                
                //queryService.Credentials = System.Net.CredentialCache.DefaultCredentials;
                queryService.Credentials = new System.Net.NetworkCredential("姓名", "密码", "域");
                
                System.Data.DataSet queryResults = queryService.QueryEx(BuildQueryText(column, scope, searchWords, searchOption));
                DataTable indexResult = queryResults.Tables[0];
                string rootPath = APJ.Util.Utility.GetAppSetting("SaveFilePath").Trim().ToLower();
                for (int i = 0; i < indexResult.Rows.Count; i++)
                {
                    indexResult.Rows[i][0] = indexResult.Rows[i][0].ToString().Replace("/", "\\");
                    indexResult.Rows[i][0] = indexResult.Rows[i][0].ToString().Replace("file:" + rootPath, "");
                }
                
                //add by zjy 20140620, log sharepoint service infomation
                StringBuilder msg = new StringBuilder();
                msg.Append("Sharepoint Serch Log\n");
                string scopes = "";
                foreach (string s in scope)
                {
                    scopes += s + "|";
                }
                msg.Append(string.Format("RootPath:{0} , SharepointServer:{1} , Scope:{2} , Keywords:{3} \n", rootPath, "url", scopes, searchWords));
                msg.Append(string.Format("Result count:{0}\n", indexResult.Rows.Count));
                msg.Append("Result count:\n");
                for (int x = 0; x < indexResult.Rows.Count; x++)
                {
                    msg.Append(indexResult.Rows[x][0] + "\n");
                }
                APJ.Util.Log.WindowLog.Log(msg.ToString(), APJ.Util.Log.LogType.Message, "SysLog");
                return indexResult;
            }
            catch (Exception ex)
            {
                DataTable dt = new DataTable();
                return dt;
            }
        }
        private static string ParseKeywords(string keyWords, IndexServiceSearchOption searchOption)
        {
            string[] strArray = keyWords.Split((char[])new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries);
            if (searchOption.Equals(IndexServiceSearchOption.And))
            {
                return "FREETEXT(DEFAULTPROPERTIES,\'" + keyWords + "\')";
            }
            else if (searchOption.Equals(IndexServiceSearchOption.Or))
            {
                for (int i = 0; i < strArray.Length; i = (int)(i + 1))
                {
                    strArray[i] = "FREETEXT(DEFAULTPROPERTIES,\'" + strArray[i] + "\')";
                }
                string str = " Or ";
                return string.Join(str, strArray);
            }
            else
            {
                return "FREETEXT(DEFAULTPROPERTIES,\'" + keyWords + "\')";
            }
        }
        private static string ParseColumn(List<string> column)
        {
            //Join Column  as: Column1 , Column2...
            //if  the column.count <1 ,then select   all  column
            string returnParseColumn = string.Empty;
            StringBuilder queryColumn = new StringBuilder(250);
            if (column == null || column.Count < 1)
            {
                queryColumn.Append("Title, Rank, Size, Description, Write, Path");
                returnParseColumn = queryColumn.ToString();
            }
            else
            {
                foreach (string _column in column)
                {
                    queryColumn.Append(_column + ",");
                }
                if (queryColumn.Length > 1)
                    returnParseColumn = queryColumn.ToString().Substring(0, queryColumn.ToString().Length - 1);
            }
            return returnParseColumn;
        }
        private static string ParseScope(List<string> scope)
        {
            //Join Scope  as:    CONTAINS(Path,Scope1) or CONTAINS(Path,Scope2)...
            string queryScope = string.Empty;
            if (scope != null && scope.Count > 0)
            {
                string[] queryScopeArray = new string[scope.Count];
                for (int i = 0; i < scope.Count; i++)
                {
                    queryScopeArray[i] = "CONTAINS(Path,\'" + scope[i] + "\')";
                }
                queryScope = string.Join(" or ", queryScopeArray);
            }
            return queryScope;
        }
        private static string BuildQueryText(List<string> column, List<string> scope, string searchWords, IndexServiceSearchOption searchOption)
        {
            string queryColumn = ParseColumn(column);
            string queryScope = ParseScope(scope);
            string queryKeyWord = ParseKeywords(searchWords, searchOption);
            
            //Title, Rank, Size, Description, Write, Path
            //type='MSSQLFT'
            StringBuilder queryText = new StringBuilder(200);
            queryText.Append("<QueryPacket xmlns='urn:Microsoft.Search.Query' Revision='1000'>");
            queryText.Append("<Query>");
            queryText.Append(" <SupportedFormats>");
            queryText.Append(" <Format revision='1'> urn:Microsoft.Search.Response.Document:Document");
            queryText.Append(" </Format> ");
            queryText.Append(" </SupportedFormats>");
            queryText.Append(@"  <Range>
                                 <StartAt>1</StartAt>
                                 <Count>100</Count>
                                 </Range> ");
            queryText.Append("<Context>");
            queryText.Append("<QueryText language='en-US' type='MSSQLFT'>");
            queryText.Append(" SELECT ");
            queryText.Append(queryColumn);
            queryText.Append(" FROM scope() ");
            queryText.Append(" WHERE  ");
            queryText.Append(" (" + queryKeyWord + ")");
            if (!string.IsNullOrEmpty(queryScope))
                queryText.Append(" AND  (" + queryScope + ")");
            queryText.Append(" ORDER BY \"Rank\" DESC");
            queryText.Append("</QueryText>");
            queryText.Append("</Context>");
            queryText.Append("</Query>");
            queryText.Append("</QueryPacket>");
            return queryText.ToString();
        }
        public enum IndexServiceSearchOption
        {
            And,
            Or
        }
    }


转载于:https://my.oschina.net/zengjyxx/blog/284160

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值