Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public object Post(SearchIndex request)
- {
- List<Product> products = new List<Product>();
- var pageSize = -1;
- var totalpages = -1;
- int.TryParse(ConfigurationManager.AppSettings["PageSize"], out pageSize);
- if (request.Page.Equals(0))
- {
- request.Page = 1;
- }
- // Get Azure settings
- AzureDirectory azureDirectory ;
- try
- {
- // This is the line where we get the Access denied exception thrown at us
- azureDirectory = new AzureDirectory(Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(ConfigurationManager.AppSettings["ConnectionStringAzureSearch"]), "indexsearch");
- IndexSearcher searcher;
- using (new AutoStopWatch("Creating searcher"))
- {
- searcher = new IndexSearcher(azureDirectory);
- }
- using (new AutoStopWatch(string.Format("Search for {0}", request.SearchString)))
- {
- string[] searchfields = new string[] { "Id", "Name", "Description" };
- var hits = searcher.Search(QueryMaker(request.SearchString, searchfields), request.Page * pageSize);
- int count = hits.ScoreDocs.Count();
- float temp_totalpages = 0;
- temp_totalpages = (float)hits.ScoreDocs.Count() / (float)pageSize;
- if (temp_totalpages > (int)temp_totalpages)
- {
- totalpages = (int)temp_totalpages + 1;
- }
- else
- {
- totalpages = (int)temp_totalpages;
- }
- foreach (ScoreDoc match in hits.ScoreDocs)
- {
- Document doc = searcher.Doc(match.Doc);
- int producId = int.Parse(doc.Get("Id"));
- Product product = Db.Select<Product>("Id={0}", producId).FirstOrDefault();
- products.Add(product);
- }
- }
- return new SearchIndexResult { result = products.Skip((int)((request.Page - 1) * 10)).Take(pageSize).ToList(), PageSize = pageSize, TotalPages = totalpages };
- }
- catch (Exception e)
- {
- return new HttpResult(HttpStatusCode.NoContent, "azureDirectory. Parameter: " + request.SearchString + ". e: " + e.Message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement