public static void ClearProductPropertyInSearchIndex(string accountNamekey, string searchPartitionName, string searchIndexName, string documentKey, string searchFieldName, bool isCollection) { //Get from cache first var accountSearchIndex = Common.MemoryCacheManager.SearchIndexCache[searchIndexName] as ISearchIndexClient; if (accountSearchIndex == null) { //Not in cache, create: ---------------------------- //SearchServiceClient searchServiceClient = Settings.Azure.Search.AccountsSearchServiceClient; SearchServiceClient searchServiceClient = Settings.Azure.Search.GetSearchPartitionClient(searchPartitionName); accountSearchIndex = searchServiceClient.Indexes.GetClient(searchIndexName); //Store in cache: --------------------- Common.MemoryCacheManager.SearchIndexCache.Set(searchIndexName, accountSearchIndex, new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(Common.MemoryCacheManager.SearchIndexClientCacheTimeInMinutes) }); } //SearchServiceClient searchServiceClient = Settings.Azure.Search.AccountsSearchServiceClient; ///SearchServiceClient searchServiceClient = Settings.Azure.Search.GetSearchPartitionClient(searchPartitionName); ///var searchIndexClient = searchServiceClient.Indexes.GetClient(searchIndexName); //Microsoft.Azure.Search.Models.DocumentIndexResult documentIndexResult = null; var searchDocument = accountSearchIndex.Documents.Get(documentKey); if (!isCollection) { searchDocument[searchFieldName] = null; } else { searchDocument[searchFieldName] = new string[0]; } var searchUpdateBatch = new List <dynamic>(); searchUpdateBatch.Add(searchDocument); var uploadOrMergeAction = IndexBatch.MergeOrUpload(searchUpdateBatch.ToArray()); Microsoft.Azure.Search.Models.DocumentIndexResult documentIndexResult = null; documentIndexResult = accountSearchIndex.Documents.Index(uploadOrMergeAction); //Invalidate Facets Cache Internal.SearchCaching.InvalidateProductSearchFacetsCache(accountNamekey); }
public static void RemoveProductPropertyCollectionItemInSearchIndex(string accountNamekey, string searchPartitionName, string searchIndexName, string documentKey, string searchFieldName, int collectionItemIndex) { //Get from cache first var accountSearchIndex = Common.MemoryCacheManager.SearchIndexCache[searchIndexName] as ISearchIndexClient; if (accountSearchIndex == null) { //Not in cache, create: ---------------------------- //SearchServiceClient searchServiceClient = Settings.Azure.Search.AccountsSearchServiceClient; SearchServiceClient searchServiceClient = Settings.Azure.Search.GetSearchPartitionClient(searchPartitionName); accountSearchIndex = searchServiceClient.Indexes.GetClient(searchIndexName); //Store in cache: --------------------- Common.MemoryCacheManager.SearchIndexCache.Set(searchIndexName, accountSearchIndex, new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(Common.MemoryCacheManager.SearchIndexClientCacheTimeInMinutes) }); } //SearchServiceClient searchServiceClient = Settings.Azure.Search.AccountsSearchServiceClient; ///SearchServiceClient searchServiceClient = Settings.Azure.Search.GetSearchPartitionClient(searchPartitionName); ///var searchIndexClient = searchServiceClient.Indexes.GetClient(searchIndexName); //Microsoft.Azure.Search.Models.DocumentIndexResult documentIndexResult = null; var searchDocument = accountSearchIndex.Documents.Get(documentKey); //Get array, make local version, remove item, re-assign to searchfield: string[] arrayIn = (string[])searchDocument[searchFieldName]; var arrayOut = arrayIn.ToList(); arrayOut.RemoveAt(collectionItemIndex); searchDocument[searchFieldName] = arrayOut.ToArray(); //Update the document in search: var searchUpdateBatch = new List <dynamic>(); searchUpdateBatch.Add(searchDocument); var uploadOrMergeAction = IndexBatch.MergeOrUpload(searchUpdateBatch.ToArray()); Microsoft.Azure.Search.Models.DocumentIndexResult documentIndexResult = null; documentIndexResult = accountSearchIndex.Documents.Index(uploadOrMergeAction); //Invalidate Facets Cache Internal.SearchCaching.InvalidateProductSearchFacetsCache(accountNamekey); }
// REMOVE public static void UpdateProductDocumentsInSearchIndex(string accountNameKey, string searchPartitionName, string searchIndexName, List <ProductDocumentModel> documents, ProductSearchIndexAction action) { //Get from cache first var accountSearchIndex = Common.MemoryCacheManager.SearchIndexCache[searchIndexName] as ISearchIndexClient; if (accountSearchIndex == null) { //Not in cache, create: ---------------------------- //SearchServiceClient searchServiceClient = Settings.Azure.Search.AccountsSearchServiceClient; SearchServiceClient searchServiceClient = Settings.Azure.Search.GetSearchPartitionClient(searchPartitionName); accountSearchIndex = searchServiceClient.Indexes.GetClient(searchIndexName); //Store in cache: --------------------- Common.MemoryCacheManager.SearchIndexCache.Set(searchIndexName, accountSearchIndex, new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(Common.MemoryCacheManager.SearchIndexClientCacheTimeInMinutes) }); } //try (Removed try catch block in favor of rollbacks in parent/caller method //{ //SearchServiceClient searchServiceClient = Settings.Azure.Search.AccountsSearchServiceClient; ///SearchServiceClient searchServiceClient = Settings.Azure.Search.GetSearchPartitionClient(searchPartitionName); ///var searchIndexClient = searchServiceClient.Indexes.GetClient(searchIndexName); Microsoft.Azure.Search.Models.DocumentIndexResult documentIndexResult = null; switch (action) { case ProductSearchIndexAction.Add: case ProductSearchIndexAction.Update: #region Add/Update to Search Index var searchUpdateBatch = new List <dynamic>(); #region Convert to list of search documents to add/update foreach (ProductDocumentModel document in documents) { dynamic productSearchModel = new ProductSearchModel() { id = document.Id, //AccountID = document.AccountID, //AccountNameKey = document.AccountNameKey, //DocumentType = document.DocumentType, fullyQualifiedName = document.FullyQualifiedName, locationPath = document.LocationPath, categoryName = document.CategoryName, subcategoryName = document.SubcategoryName, subsubcategoryName = document.SubsubcategoryName, subsubsubcategoryName = document.SubsubsubcategoryName, categoryNameKey = document.CategoryNameKey, subcategoryNameKey = document.SubcategoryNameKey, subsubcategoryNameKey = document.SubsubcategoryNameKey, subsubsubcategoryNameKey = document.SubsubsubcategoryNameKey, dateCreated = document.DateCreated, orderId = document.OrderID, name = document.Name, nameKey = document.NameKey, //OrderID = document.OrderID, //title = document.Title, visible = document.Visible, }; if (document.Tags != null) { productSearchModel.tags = document.Tags.ToArray(); } /* REMOVED, properties are handled individually * * if (document.Properties != null) * { * foreach (KeyValuePair<string, string> entry in document.Properties) * { * //(productSearchModel as IDictionary<String, string>)[entry.Key] = entry.Value; * * //ExpandoObject * //(object)productSearchModel.GetType().GetProperty(entry.Key) = entry.Value; * //(dynamic)productSearchModel.GetType().GetProperty("PropertyName") ..[entry.Key] = entry.Value;// .Add(entry.Key + ":" + entry.Value); * } * * //productSearchModel.properties = StringifyProductProperties(document.Properties).ToArray(); * } */ //if (document.xxx != null) //{ //productSearchModel.Thumbnails = GenerateIndexedThumbnails(document.); //} searchUpdateBatch.Add(productSearchModel); } #endregion var uploadOrMergeAction = IndexBatch.MergeOrUpload(searchUpdateBatch.ToArray()); documentIndexResult = accountSearchIndex.Documents.Index(uploadOrMergeAction); #endregion break; case ProductSearchIndexAction.Delete: #region Delete from Search Index var keyValues = new List <string>(); #region Generate list of keys to delete foreach (ProductDocumentModel document in documents) { keyValues.Add(document.Id); } #endregion var deleteAction = Microsoft.Azure.Search.Models.IndexBatch.Delete("id", keyValues); documentIndexResult = accountSearchIndex.Documents.Index(deleteAction); #endregion break; default: break; } if (documentIndexResult == null) { } //Invalidate Facets Cache Internal.SearchCaching.InvalidateProductSearchFacetsCache(accountNameKey); //} /* * catch (IndexBatchException ibe) * { #region Handle Exception * * string Ids = String.Empty; * #region Generate list of document id(s) at issue * * foreach (ProductDocumentModel document in documents) * { * Ids += " [" + document.Id + "]"; * } * #endregion * * PlatformExceptionsHelper.LogExceptionAndAlertAdmins( * ibe, * "attempting to " + action.ToString().ToLower() + " document(s) to/from search index '" + searchIndexName + "' for documents:" + Ids, * System.Reflection.MethodBase.GetCurrentMethod() * ); * * PlatformLogManager.LogActivity( * CategoryType.ManualTask, * ActivityType.ManualTask_Search, * "Search index " + action.ToString().ToLower() + " action on search index '" + searchIndexName + "' for documents:" + Ids, * "You may need to manually " + action.ToString().ToLower() + " for document Id's :" + Ids + " for index '" + searchIndexName + "'", * null, * null, * null, * null, * null, * null, * System.Reflection.MethodBase.GetCurrentMethod().ToString() * ); * #endregion * } * catch (Exception e) * { #region Handle Exception * * string Ids = String.Empty; * #region Generate list of document id(s) at issue * * foreach (ProductDocumentModel document in documents) * { * Ids += " [" + document.Id + "]"; * } * #endregion * * PlatformExceptionsHelper.LogExceptionAndAlertAdmins( * e, * "attempting to " + action.ToString().ToLower() + " document(s) to/from search index '" + searchIndexName + "' for documents:" + Ids, * System.Reflection.MethodBase.GetCurrentMethod() * ); * * PlatformLogManager.LogActivity( * CategoryType.ManualTask, * ActivityType.ManualTask_Search, * "Search index " + action.ToString().ToLower() + " action on search index '" + searchIndexName + "' for documents:" + Ids, * "You may need to manually " + action.ToString().ToLower() + " for document Id's :" + Ids + " for index '" + searchIndexName + "'", * null, * null, * null, * null, * null, * null, * System.Reflection.MethodBase.GetCurrentMethod().ToString() * ); * #endregion * }*/ }
// 'additionalMetadata' is used for fields such as 'location/geograpypoint' types that require an additional searchable string field to increase search vectors public static void UpdateProductPropertyInSearchIndex(string accountNamekey, string searchPartitionName, string searchIndexName, string documentKey, string searchFieldName, string propertyValue, ProductPropertyUpdateType updateType, ProductPropertySearchFieldType propertySearchFieldType, string additionalMetadata = null, GeographyPoint geographyPoint = null) { //Get from cache first var accountSearchIndex = Common.MemoryCacheManager.SearchIndexCache[searchIndexName] as ISearchIndexClient; if (accountSearchIndex == null) { //Not in cache, create: ---------------------------- //SearchServiceClient searchServiceClient = Settings.Azure.Search.AccountsSearchServiceClient; SearchServiceClient searchServiceClient = Settings.Azure.Search.GetSearchPartitionClient(searchPartitionName); accountSearchIndex = searchServiceClient.Indexes.GetClient(searchIndexName); //Store in cache: --------------------- Common.MemoryCacheManager.SearchIndexCache.Set(searchIndexName, accountSearchIndex, new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(Common.MemoryCacheManager.SearchIndexClientCacheTimeInMinutes) }); } // SearchServiceClient searchServiceClient = Settings.Azure.Search.AccountsSearchServiceClient; ///SearchServiceClient searchServiceClient = Settings.Azure.Search.GetSearchPartitionClient(searchPartitionName); ///var searchIndexClient = searchServiceClient.Indexes.GetClient(searchIndexName); //Microsoft.Azure.Search.Models.DocumentIndexResult documentIndexResult = null; var searchDocument = accountSearchIndex.Documents.Get(documentKey); if (updateType == ProductPropertyUpdateType.Replace) { switch (propertySearchFieldType) { case ProductPropertySearchFieldType.String: searchDocument[searchFieldName] = propertyValue; break; case ProductPropertySearchFieldType.Collection: List <string> arrayList = new List <string>(); arrayList.Add(propertyValue); searchDocument[searchFieldName] = arrayList.ToArray(); break; case ProductPropertySearchFieldType.Location: searchDocument[searchFieldName] = geographyPoint; searchDocument[searchFieldName + "LocationMetadata"] = additionalMetadata; break; } } else if (updateType == ProductPropertyUpdateType.Append) { string[] arrayIn = (string[])searchDocument[searchFieldName]; var arrayOut = arrayIn.ToList(); arrayOut.Add(propertyValue); searchDocument[searchFieldName] = arrayOut.ToArray(); } //else if (updateType == ProductPropertyUpdateType.Clear) //{ //searchDocument[propertyName] = null; //} var searchUpdateBatch = new List <dynamic>(); searchUpdateBatch.Add(searchDocument); var uploadOrMergeAction = IndexBatch.MergeOrUpload(searchUpdateBatch.ToArray()); Microsoft.Azure.Search.Models.DocumentIndexResult documentIndexResult = null; documentIndexResult = accountSearchIndex.Documents.Index(uploadOrMergeAction); //Invalidate Facets Cache Internal.SearchCaching.InvalidateProductSearchFacetsCache(accountNamekey); }
public static void CreateProductDocumentInSearchIndex(string searchPartitionName, string searchIndexName, ProductDocumentModel productDocumentModel) { //Get from cache first var accountSearchIndex = Common.MemoryCacheManager.SearchIndexCache[searchIndexName] as ISearchIndexClient; if (accountSearchIndex == null) { //Not in cache, create: ---------------------------- //SearchServiceClient searchServiceClient = Settings.Azure.Search.AccountsSearchServiceClient; SearchServiceClient searchServiceClient = Settings.Azure.Search.GetSearchPartitionClient(searchPartitionName); accountSearchIndex = searchServiceClient.Indexes.GetClient(searchIndexName); //Store in cache: --------------------- Common.MemoryCacheManager.SearchIndexCache.Set(searchIndexName, accountSearchIndex, new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(Common.MemoryCacheManager.SearchIndexClientCacheTimeInMinutes) }); } //SearchServiceClient searchServiceClient = Settings.Azure.Search.AccountsSearchServiceClient; //var searchServiceClient = Settings.Azure.Search.GetSearchPartitionClient(searchPartitionName); //var searchIndexClient = searchServiceClient.Indexes.GetClient(searchIndexName); //Microsoft.Azure.Search.Models.DocumentIndexResult documentIndexResult = null; #region Convert to list of search documents to add/update dynamic productSearchModel = new ProductSearchModel() { id = productDocumentModel.Id, //AccountID = document.AccountID, //AccountNameKey = document.AccountNameKey, //DocumentType = document.DocumentType, fullyQualifiedName = productDocumentModel.FullyQualifiedName, locationPath = productDocumentModel.LocationPath, categoryName = productDocumentModel.CategoryName, subcategoryName = productDocumentModel.SubcategoryName, subsubcategoryName = productDocumentModel.SubsubcategoryName, subsubsubcategoryName = productDocumentModel.SubsubsubcategoryName, categoryNameKey = productDocumentModel.CategoryNameKey, subcategoryNameKey = productDocumentModel.SubcategoryNameKey, subsubcategoryNameKey = productDocumentModel.SubsubcategoryNameKey, subsubsubcategoryNameKey = productDocumentModel.SubsubsubcategoryNameKey, name = productDocumentModel.Name, nameKey = productDocumentModel.NameKey, orderId = productDocumentModel.OrderID, //OrderID = document.OrderID, //title = productDocumentModel.Title, visible = productDocumentModel.Visible, dateCreated = productDocumentModel.DateCreated }; #endregion var searchUpdateBatch = new List <dynamic>(); searchUpdateBatch.Add(productSearchModel); var uploadOrMergeAction = IndexBatch.MergeOrUpload(searchUpdateBatch.ToArray()); Microsoft.Azure.Search.Models.DocumentIndexResult documentIndexResult = null; documentIndexResult = accountSearchIndex.Documents.Index(uploadOrMergeAction); }