Advertisement
loonerz

Code for searcher

May 16th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1.         public object Post(SearchIndex request)
  2.         {          
  3.  
  4.             List<Product> products = new List<Product>();
  5.  
  6.             var pageSize = -1;
  7.             var totalpages = -1;
  8.             int.TryParse(ConfigurationManager.AppSettings["PageSize"], out pageSize);
  9.  
  10.             if (request.Page.Equals(0))
  11.             {
  12.                 request.Page = 1;
  13.             }
  14.  
  15.             // Get Azure settings
  16.             AzureDirectory azureDirectory ;
  17.  
  18.             try
  19.             {
  20.                 // This is the line where we get the Access denied exception thrown at us
  21.                 azureDirectory = new AzureDirectory(Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(ConfigurationManager.AppSettings["ConnectionStringAzureSearch"]), "indexsearch");
  22.  
  23.                 IndexSearcher searcher;
  24.                 using (new AutoStopWatch("Creating searcher"))
  25.                 {
  26.                     searcher = new IndexSearcher(azureDirectory);
  27.                 }
  28.  
  29.                 using (new AutoStopWatch(string.Format("Search for {0}", request.SearchString)))
  30.                 {
  31.                     string[] searchfields = new string[] { "Id", "Name", "Description" };
  32.  
  33.                     var hits = searcher.Search(QueryMaker(request.SearchString, searchfields), request.Page * pageSize);
  34.  
  35.                     int count = hits.ScoreDocs.Count();
  36.                     float temp_totalpages = 0;
  37.                     temp_totalpages = (float)hits.ScoreDocs.Count() / (float)pageSize;
  38.  
  39.                     if (temp_totalpages > (int)temp_totalpages)
  40.                     {
  41.                         totalpages = (int)temp_totalpages + 1;
  42.                     }
  43.                     else
  44.                     {
  45.                         totalpages = (int)temp_totalpages;
  46.                     }
  47.  
  48.                     foreach (ScoreDoc match in hits.ScoreDocs)
  49.                     {
  50.                         Document doc = searcher.Doc(match.Doc);
  51.  
  52.                         int producId = int.Parse(doc.Get("Id"));
  53.  
  54.                         Product product = Db.Select<Product>("Id={0}", producId).FirstOrDefault();
  55.  
  56.                         products.Add(product);
  57.                     }
  58.  
  59.                 }
  60.  
  61.                 return new SearchIndexResult { result = products.Skip((int)((request.Page - 1) * 10)).Take(pageSize).ToList(), PageSize = pageSize, TotalPages = totalpages };
  62.             }
  63.             catch (Exception e)
  64.             {
  65.  
  66.                 return new HttpResult(HttpStatusCode.NoContent, "azureDirectory. Parameter: " + request.SearchString + ". e: " + e.Message);
  67.             }
  68.  
  69.            
  70.  
  71.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement