Total Pageviews

11 Jan 2013

Testing SharePoint Server Search with KeywordQuery


Code:
   class Program
    {
        static void Main(string[] args)
        {

            SearchQueryAndSiteSettingsServiceProxy settingsProxy = SPFarm.Local.ServiceProxies.GetValue<SearchQueryAndSiteSettingsServiceProxy>();

            SearchServiceApplicationProxy searchProxy = settingsProxy.ApplicationProxies.GetValue<SearchServiceApplicationProxy>("Enterprise Search Service");

            KeywordQuery keywordQuery = new KeywordQuery(searchProxy);

            keywordQuery.QueryText = "Region:b2a48ce5-31e0-40f7-873b-f4fbceacc45f";

            keywordQuery.SelectProperties.Add("Title");
            keywordQuery.SelectProperties.Add("Region");
            keywordQuery.SelectProperties.Add("Регион");
            keywordQuery.SelectProperties.Add("ItemSection");

            //The creator of this fault did not specify a Reason:
           // keywordQuery.SelectProperties.Add("owsMetadataFacetInfo");
            
           // keywordQuery.SelectProperties.Add("owstaxIdRegion");

            keywordQuery.ResultsProvider = SearchProvider.Default;

            keywordQuery.ResultTypes = ResultType.RelevantResults;

            keywordQuery.RowLimit = 50;

            ResultTableCollection resultsTableCollection = keywordQuery.Execute();

            ResultTable searchResultsTable = resultsTableCollection[ResultType.RelevantResults];

            DataTable resultsDataTable = new DataTable();

            resultsDataTable.TableName = "Results";

            resultsDataTable.Load(searchResultsTable, LoadOption.OverwriteChanges);

            Console.WriteLine(resultsDataTable.Rows.Count);

            for (int j = 0; j < resultsDataTable.Rows.Count; j++)
            {

                DataRow row = resultsDataTable.Rows[j];
                foreach (DataColumn column in resultsDataTable.Columns)
                {
                    Console.Write("Item: ");
                    Console.Write(column.ColumnName);
                    Console.Write(" ");
                    Console.WriteLine(row[column]);
                }
            } 
        }
    }


Code:
     using(SPSite site = new SPSite("http://portal.com"))
            {
                SearchServiceApplicationProxy ssa = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(site));

                KeywordQuery kq = new KeywordQuery(ssa);
                kq.ResultsProvider = SearchProvider.Default;
                kq.QueryText = string.Format(CultureInfo.InvariantCulture, "scope:\"{0}\"; title:\"Ivanov Ivan\"", "people");
                kq.ResultTypes = ResultType.RelevantResults;
                kq.StartRow = 0;
                kq.EnablePhonetic = true;
                kq.EnableStemming = true;
                kq.RowLimit = 1000;
                kq.KeywordInclusion = KeywordInclusion.AllKeywords;

                ResultTableCollection res = kq.Execute();

                bool exists = res.Exists(ResultType.RelevantResults);

                if (exists)
                {
                    ResultTable searchResult = res[ResultType.RelevantResults];
                    Console.WriteLine(searchResult.TotalRows);
                }

                
            }
            Console.ReadLine();


No comments:

Post a Comment