https://github.com/thecogworks/cogworks.azuresearch
A wrapper to Azure Search allowing to easily setup Azure Search indexes, searchers.
https://github.com/thecogworks/cogworks.azuresearch
azure azure-cognitive-search azure-search azuresearch cogworks hacktoberfest search searchers
Last synced: 6 months ago
JSON representation
A wrapper to Azure Search allowing to easily setup Azure Search indexes, searchers.
- Host: GitHub
- URL: https://github.com/thecogworks/cogworks.azuresearch
- Owner: thecogworks
- License: apache-2.0
- Created: 2020-08-10T09:51:28.000Z (almost 5 years ago)
- Default Branch: develop
- Last Pushed: 2024-09-06T09:16:38.000Z (9 months ago)
- Last Synced: 2024-11-12T07:52:10.445Z (6 months ago)
- Topics: azure, azure-cognitive-search, azure-search, azuresearch, cogworks, hacktoberfest, search, searchers
- Language: C#
- Homepage:
- Size: 570 KB
- Stars: 8
- Watchers: 11
- Forks: 0
- Open Issues: 20
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Cogworks.AzureSearch · [](LICENSE.md) [](https://github.com/thecogworks/Cogworks.AzureSearch/actions?query=workflow%3A%22Changelog+generator+and+NuGet+Releasing%22) [](https://www.nuget.org/packages/Cogworks.AzureSearch/) [](undefined)
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).
## Extension libraries
| Package Name | Release (NuGet) |
|--------------------------------|-----------------|
| `Cogworks.AzureSearch.IoC.Autofac` | [](https://www.nuget.org/packages/Cogworks.AzureSearch.IoC.Autofac/) |
| `Cogworks.AzureSearch.IoC.LightInject` | [](https://www.nuget.org/packages/Cogworks.AzureSearch.IoC.LightInject/) |
| `Cogworks.AzureSearch.IoC.Microsoft` | [](https://www.nuget.org/packages/Cogworks.AzureSearch.IoC.Umbraco/) |
| `Cogworks.AzureSearch.IoC.Umbraco` | [](https://www.nuget.org/packages/Cogworks.AzureSearch.IoC.Umbraco/) |## Usage
#### Document Operations
```csharp
public class SomeDocumentService
{
private readonly IAzureDocumentOperation _documentOperation;public SomeDocumentService(IAzureDocumentOperation 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
{
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
{
new FirstDocumentModel
{
Id = "first-id"
},
new FirstDocumentModel
{
Id = "second-id"
}
};_ = await _documentOperation.TryRemoveDocumentsAsync(items);
}
}
```#### Search operations
##### Default search for index```csharp
public class SomeService
{
private readonly ISearcher _documentSearch;public SomeService(ISearcher 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
});
}
}
```##### Domain search
``` csharp
public interface ISomeDomainSearch
{
FirstDocumentModel GetLatestAuthorDocument(string author);
}public class SomeDomainSearch : BaseDomainSearch, ISomeDomainSearch
{
public SomeDomainSearch(ISearcher searcher) : base(searcher)
{
}FirstDocumentModel GetLatestAuthorDocument(string author)
=> base.Search("*", new AzureSearchParameters
{
Filter = nameof(FirstDocumentModel.Author).EqualsValue(author)
OrderBy = new List() { nameof(FirstDocumentModel.PublishedDate) + "desc" }
Top = 1
}).Results.FirstOrDefault()?.Document;
}
```#### Index Operations
```csharp
public class SomeIndexService
{
private readonly IAzureIndexOperation _indexOperation;
public SomeIndexService(IAzureIndexOperation indexOperation)
=> _indexOperation = indexOperation;public async Task Work()
{
_ = await _indexOperation.IndexExistsAsync();
_ = await _azureIndexOperation.IndexCreateOrUpdateAsync();
_ = await _azureIndexOperation.IndexClearAsync();
await _indexOperation.IndexDeleteAsync()
}
}
```#### Repository Operations
All operations (API) that are available for: Document Operations, Search Operations, Index Operations.
```csharp
public class SomeAdvanceService
{
private readonly IAzureSearchRepository _documentRepository;
public SomeAdvanceService(IAzureSearchRepository documentRepository)
=> _documentRepository = documentRepository;public async Task Work()
{
// some work here
}
}
```#### Initializer
```csharp
public class SomeStartupService
{
private readonly IAzureInitializer _initializer;public SomeStartupService(IAzureInitializer initializer)
=> _initializer = initializer;public async Task Initialize()
=> await initializer.InitializeAsync();
}
```## License
- Cogworks.AzureSearch is licensed under the [Apache License, Version 2.0](https://opensource.org/licenses/Apache-2.0)
## Code of Conduct
This project has adopted the code of conduct defined by the [Contributor Covenant](https://contributor-covenant.org/) to clarify expected behavior in our community.
For more information, see the [.NET Foundation Code of Conduct](https://dotnetfoundation.org/code-of-conduct).## Blogs
* [How to Simplify Azure Search Implementations](https://www.wearecogworks.com/blog/how-to-simplify-azure-search-implementations/)
## How can you help?
Please... Spread the word, contribute, submit improvements and issues, unit tests, no input is too little. Thank you in advance <3