} // DeleteCustomer /// <summary> /// Search for a csutomer /// </summary> public List <Model.Search.SearchCustomerModel> CustomerSearch(Interface.GlobalEnum.IndexerIndexName indexName, string customerName) { // only check once per run if (!doesIndexExistsCheck.Contains(indexName.ToString().ToLower())) { CreateIndexIfNotExists(indexName, Interface.GlobalEnum.IndexerRepositoryIndexType.SystemDefined); doesIndexExistsCheck.Add(indexName.ToString().ToLower()); } SearchIndexClient indexClient = serviceClient.Indexes.GetClient(indexName.ToString().ToLower()); SearchParameters searchParameters = new SearchParameters(); List <Model.Search.SearchCustomerModel> resultList = new List <Model.Search.SearchCustomerModel>(); DocumentSearchResponse <Model.Search.SearchCustomerModel> response = indexClient.Documents.Search <Model.Search.SearchCustomerModel>(customerName, searchParameters); foreach (SearchResult <Model.Search.SearchCustomerModel> item in response) { Model.Search.SearchCustomerModel searchDocument = new Model.Search.SearchCustomerModel(); searchDocument.CustomerId = item.Document.CustomerId; searchDocument.CustomerName = item.Document.CustomerName; resultList.Add(searchDocument); } return(resultList); }
public void CustomerAzureSearch() { Model.Customer.CustomerModel customerModel = this.CreateCustomerModel(); Interface.Service.ICustomerService customerService = DI.Container.Resolve <Interface.Service.ICustomerService>(); customerService.Save(customerModel); Model.Search.SearchCustomerModel searchCustomerModel = new Model.Search.SearchCustomerModel(); searchCustomerModel.CustomerId = customerModel.CustomerId.ToString().ToLower(); searchCustomerModel.CustomerName = customerModel.CustomerName; Interface.Repository.IIndexerRepository azureSearch = DI.Container.Resolve <Interface.Repository.IIndexerRepository>(); azureSearch.UpsertCustomer(Interface.GlobalEnum.IndexerIndexName.Customer, searchCustomerModel); List <Model.Search.SearchCustomerModel> list = azureSearch.CustomerSearch(Interface.GlobalEnum.IndexerIndexName.Customer, customerModel.CustomerName); bool found = false; foreach (var item in list) { if (item.CustomerName == customerModel.CustomerName) { Assert.AreEqual(list[0].CustomerName, customerModel.CustomerName); found = true; break; } } if (!found) { Assert.Fail("Could not find customer in Azure Search"); } } // CustomerAzureSearch
/// <summary> /// Inserts/Updates a customer /// </summary> public void UpsertCustomer(Interface.GlobalEnum.IndexerIndexName indexName, Model.Search.SearchCustomerModel searchCustomerModel) { // only check once per run if (!doesIndexExistsCheck.Contains(indexName.ToString().ToLower())) { CreateIndexIfNotExists(indexName, Interface.GlobalEnum.IndexerRepositoryIndexType.SystemDefined); doesIndexExistsCheck.Add(indexName.ToString().ToLower()); } SearchIndexClient indexClient = serviceClient.Indexes.GetClient(indexName.ToString().ToLower()); // Can be done in batches, but since we are using batching we can do one by one for retries List <Model.Search.SearchCustomerModel> itemsToIndex = new List <Model.Search.SearchCustomerModel>(); itemsToIndex.Add(searchCustomerModel); indexClient.Documents.Index(IndexBatch.Create(itemsToIndex.Select(doc => IndexAction.Create(IndexActionType.MergeOrUpload, doc)))); } // UpsertCustomer
} // UpsertCustomer /// <summary> /// Removes a customer /// </summary> /// <param name="indexName"></param> /// <param name="searchDocument"></param> public void DeleteCustomer(Interface.GlobalEnum.IndexerIndexName indexName, Model.Search.SearchCustomerModel searchCustomerModel) { // only check once per run if (!doesIndexExistsCheck.Contains(indexName.ToString().ToLower())) { CreateIndexIfNotExists(indexName, Interface.GlobalEnum.IndexerRepositoryIndexType.SystemDefined); doesIndexExistsCheck.Add(indexName.ToString().ToLower()); } SearchIndexClient indexClient = serviceClient.Indexes.GetClient(indexName.ToString().ToLower()); // Can be done in batches, but since we are using batching we can do one by one for retries List <Model.Search.SearchCustomerModel> itemsToIndex = new List <Model.Search.SearchCustomerModel>(); itemsToIndex.Add(searchCustomerModel); indexClient.Documents.Index(IndexBatch.Create(itemsToIndex.Select(doc => IndexAction.Create(IndexActionType.Delete, doc)))); // Sometimes when your Search service is under load, indexing will fail for some of the documents in // the batch. Depending on your application, you can take compensating actions like delaying and // retrying. } // DeleteCustomer