A wrapper to Azure Search allowing to easily setup Azure Search indexes, searchers and using it with DI/IoC approach (currently with support for Umbraco, LightInject and Autofac).
Package Name | Release (NuGet) |
---|---|
Cogworks.AzureSearch.IoC.Autofac |
|
Cogworks.AzureSearch.IoC.LightInject |
|
Cogworks.AzureSearch.IoC.Microsoft |
|
Cogworks.AzureSearch.IoC.Umbraco |
public class SomeDocumentService
{
private readonly IAzureDocumentOperation<FirstDocumentModel> _documentOperation;
public SomeDocumentService(IAzureDocumentOperation<FirstDocumentModel> documentOperation)
=> _documentOperation = documentOperation;
public async Task AddOrUpdateItem()
=> await _documentOperation.AddOrUpdateDocumentAsync(new FirstDocumentModel
{
Id = "some-id",
Content = "Some content"
});
public async Task AddOrUpdateItems()
{
var items = new List<FirstDocumentModel>
{
new FirstDocumentModel
{
Id = "first-id",
Content = "First content"
},
new FirstDocumentModel
{
Id = "second-id",
Content = "Second content"
}
};
_ = await _documentOperation.AddOrUpdateDocumentsAsync(items);
}
public async Task RemoveItem()
=> await _documentOperation.TryRemoveDocumentAsync(new FirstDocumentModel
{
Id = "some-id"
});
public async Task RemoveItems()
{
var items = new List<FirstDocumentModel>
{
new FirstDocumentModel
{
Id = "first-id"
},
new FirstDocumentModel
{
Id = "second-id"
}
};
_ = await _documentOperation.TryRemoveDocumentsAsync(items);
}
}
public class SomeService
{
private readonly ISearcher<FirstDocumentModel> _documentSearch;
public SomeService(ISearcher<FirstDocumentModel> documentSearch)
=> _documentSearch = documentSearch;
public void Search()
{
_ = _documentSearch.Search("Some text", new AzureSearchParameters
{
Top = 3
});
}
public async Task SearchAsync()
{
_ = await _documentSearch.SearchAsync("Some text", new AzureSearchParameters
{
Top = 3
});
}
}
public interface ISomeDomainSearch
{
FirstDocumentModel GetLatestAuthorDocument(string author);
}
public class SomeDomainSearch : BaseDomainSearch<FirstDocumentModel>, ISomeDomainSearch
{
public SomeDomainSearch(ISearcher<FirstDocumentModel> searcher) : base(searcher)
{
}
FirstDocumentModel GetLatestAuthorDocument(string author)
=> base.Search("*", new AzureSearchParameters
{
Filter = nameof(FirstDocumentModel.Author).EqualsValue(author)
OrderBy = new List<string>() { nameof(FirstDocumentModel.PublishedDate) + "desc" }
Top = 1
}).Results.FirstOrDefault()?.Document;
}
public class SomeIndexService
{
private readonly IAzureIndexOperation<FirstDocumentModel> _indexOperation;
public SomeIndexService(IAzureIndexOperation<FirstDocumentModel> indexOperation)
=> _indexOperation = indexOperation;
public async Task Work()
{
_ = await _indexOperation.IndexExistsAsync();
_ = await _azureIndexOperation.IndexCreateOrUpdateAsync();
_ = await _azureIndexOperation.IndexClearAsync();
await _indexOperation.IndexDeleteAsync()
}
}
All operations (API) that are available for: Document Operations, Search Operations, Index Operations.
public class SomeAdvanceService
{
private readonly IAzureSearchRepository<FirstDocumentModel> _documentRepository;
public SomeAdvanceService(IAzureSearchRepository<FirstDocumentModel> documentRepository)
=> _documentRepository = documentRepository;
public async Task Work()
{
// some work here
}
}
public class SomeStartupService
{
private readonly IAzureInitializer<FirstDocumentModel> _initializer;
public SomeStartupService(IAzureInitializer<FirstDocumentModel> initializer)
=> _initializer = initializer;
public async Task Initialize()
=> await initializer.InitializeAsync();
}
- Cogworks.AzureSearch is licensed under the Apache License, Version 2.0
This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community. For more information, see the .NET Foundation Code of Conduct.
Please... Spread the word, contribute, submit improvements and issues, unit tests, no input is too little. Thank you in advance <3