public static SearchResult[] DoSearch(SearchRequest request)
        {
            var indexClient = GetSearchIndexClient();

            // Set up search parameters
            var sp = new SearchParameters();

            // Set up highlights
            var hlFields = new List <string> {
                "content"
            };

            sp.HighlightFields  = hlFields;
            sp.HighlightPreTag  = "<b>";
            sp.HighlightPostTag = "</b>";

            // Set mode to 'all' (must match all terms/clauses). Default is 'any'
            sp.SearchMode = SearchMode.All;

            // Use the lucerne query engine
            sp.QueryType = QueryType.Full;

            // Filter by docid if requested
            if (!string.IsNullOrEmpty(request.DocId))
            {
                sp.Filter = "docId eq '" + request.DocId + "'";
            }

            // Filter by clientid if requested
            if (!string.IsNullOrEmpty(request.ClientId))
            {
                // add the and clause if needed
                if (!string.IsNullOrEmpty(sp.Filter))
                {
                    sp.Filter += " and ";
                }
                sp.Filter = "clientId eq '" + request.ClientId + "'";
            }

            // Filter by category(s) if requested
            if (request.Categories != null)
            {
                for (int i = 0; i < request.Categories.Length; i++)
                {
                    // start the filter expression
                    if (i == 0)
                    {
                        // add the and clause if needed
                        if (!string.IsNullOrEmpty(sp.Filter))
                        {
                            sp.Filter += " and ";
                        }
                        sp.Filter += "(";
                    }

                    // Add category filter
                    sp.Filter += "category eq '" + request.Categories[i];

                    // end expression or add 'and'
                    if (i == request.Categories.Length - 1)
                    {
                        // end expression
                        sp.Filter += ")";
                    }
                    else
                    {
                        sp.Filter += " and ";
                    }
                }
            }

            // Do not return the content
            sp.Select = new List <string> {
                "docId", "docFileName", "docUrl", "categories", "insertDate"
            };

            // Perform the search
            DocumentSearchResult <Document> response = null;

            try
            {
                response = indexClient.Documents.Search <Document>(request.SearchTerm, sp);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error doing search. " + ex.Message);
            }

            var ret = new List <SearchResult>();

            if (response != null)
            {
                foreach (SearchResult <Document> result in response.Results)
                {
                    var sResult = new SearchResult
                    {
                        Score      = result.Score,
                        DocId      = (string)result.Document["docId"],
                        FileName   = (string)result.Document["docFileName"],
                        FileUrl    = (string)result.Document["docUrl"],
                        Categories = (IEnumerable <string>)result.Document["categories"],
                        LastUpdate = ((DateTimeOffset)result.Document["insertDate"]).DateTime
                    };
                    if (result.Highlights != null && result.Highlights.ContainsKey("content"))
                    {
                        sResult.Highlights = result.Highlights["content"].ToArray();
                    }

                    sResult.FileUrl += AzureStorageHelper.GenerateSasUrl(sResult.FileName);
                    ret.Add(sResult);
                }
            }

            return(ret.ToArray());
        }
        public static SearchResult[] CategorizeDocument(string docId)
        {
            string catScoreThreshold = ConfigurationManager.AppSettings["CategorizationScoreThreshold"];
            double catTheshold;

            if (!double.TryParse(catScoreThreshold, out catTheshold))
            {
                throw new Exception("Bad or missing Categorization Score Threshold value.");
            }

            var categoryList = AzureStorageHelper.GetTableRows();

            if (categoryList != null && categoryList.Length > 0)
            {
                // Get the doc
                var            indexClient = GetSearchIndexClient();
                SearchDocument document    = indexClient.Documents.Get <SearchDocument>(docId);

                if (document != null)
                {
                    // Create list of terms per category
                    Dictionary <string, List <string> > catTerms = new Dictionary <string, List <string> >();
                    foreach (var categoryPoco in categoryList)
                    {
                        if (catTerms.ContainsKey(categoryPoco.CategoryName))
                        {
                            catTerms[categoryPoco.CategoryName].Add(categoryPoco.SearchTerm);
                        }
                        else
                        {
                            catTerms.Add(categoryPoco.CategoryName, new List <string> {
                                categoryPoco.SearchTerm
                            });
                        }
                    }

                    List <SearchResult> resultList           = new List <SearchResult>();
                    List <string>       documentCategoryList = new List <string>();

                    // Do the search for each category
                    foreach (var catTerm in catTerms)
                    {
                        // create each phrase and separate by an OR clause
                        SearchRequest request = new SearchRequest
                        {
                            DocId      = docId,
                            SearchTerm = "(" + string.Join(")|(", catTerm.Value) + ")"
                        };

                        // Do the search
                        var results = DoSearch(request);
                        if (results != null && results.Length > 0)
                        {
                            // Found a match, but we should only get one result b/c we filtered on the unique docId
                            if (results.Length > 1)
                            {
                                throw new Exception("More than one result found for document id: " + docId);
                            }

                            var firstResult = results[0];

                            // add the category to the item if the score is high enough
                            bool addedToCategory = false;
                            if (firstResult.Score >= catTheshold)
                            {
                                documentCategoryList.Add(catTerm.Key);
                                addedToCategory = true;
                            }

                            // Put the category and it's search term in the categories field (a bit of a hack)
                            firstResult.Categories = new[] { catTerm.Key, request.SearchTerm, addedToCategory.ToString().ToLower() };
                            resultList.Add(firstResult);
                        }
                    }

                    // Set category for item
                    document.Categories = documentCategoryList.ToArray();

                    // Resave item
                    UpdateDocument(document);

                    // return results
                    return(resultList.OrderByDescending(r => r.Score).ToArray());
                }
            }
            return(null);
        }