public void Delete(string id)
        {
            var result = new AzureSearchIndexResult();

            var serviceClient = GetClient();

            var actions = new List <IndexAction>();
            var d       = new Document();

            d.Add("Id", id);

            actions.Add(IndexAction.Delete(d));

            var batch       = IndexBatch.New(actions);
            var indexClient = serviceClient.Indexes.GetClient(_config.IndexName);

            try
            {
                indexClient.Documents.Index(batch);
            }
            catch (IndexBatchException e)
            {
                // 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. For this simple demo, we just log the failed document keys and continue.
                var error =
                    "Failed to index some of the documents: {0}" + String.Join(", ", e.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key));

                result.Success = false;
                result.Message = error;
            }

            result.Success = true;
        }
Example #2
0
        public static AzureSearchIndexResult IndexContentBatch(this SearchServiceClient serviceClient, string indexName, IEnumerable <Document> contents)
        {
            var result  = new AzureSearchIndexResult();
            var actions = new List <IndexAction>();

            foreach (var content in contents)
            {
                actions.Add(IndexAction.Upload(content));
            }

            var batch       = IndexBatch.New(actions);
            var indexClient = serviceClient.Indexes.GetClient(indexName);

            try
            {
                indexClient.Documents.Index(batch);
            }
            catch (IndexBatchException e)
            {
                // 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. For this simple demo, we just log the failed document keys and continue.
                var error =
                    "Failed to index some of the documents: {0}" + String.Join(", ", e.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key));

                result.Success = false;
                result.Message = error;

                return(result);
            }

            result.Success = true;
            return(result);
        }