public static bool UpdateDocument(SearchDocument document) { var batch = IndexBatch.Merge(new[] { document }); return(PerformIndexOperation(batch)); }
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); }
public static bool InsertDocument(SearchDocument document) { var batch = IndexBatch.Upload(new[] { document }); return(PerformIndexOperation(batch)); }