Skip to content

Latest commit

 

History

History
200 lines (156 loc) · 6.57 KB

README.md

File metadata and controls

200 lines (156 loc) · 6.57 KB

Cogworks.AzureSearch · GitHub license Github Build NuGet Version codecov

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 NuGet Version
Cogworks.AzureSearch.IoC.LightInject NuGet Version
Cogworks.AzureSearch.IoC.Microsoft NuGet Version
Cogworks.AzureSearch.IoC.Umbraco NuGet Version

Usage

Document Operations

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);
    }
}

Search operations

Default search for index
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
        });
    }
}
Domain search
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;
}

Index Operations

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()
    }
}

Repository Operations

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
    }
}

Initializer

public class SomeStartupService
{
    private readonly IAzureInitializer<FirstDocumentModel> _initializer;

    public SomeStartupService(IAzureInitializer<FirstDocumentModel> initializer)
        => _initializer = initializer;

    public async Task Initialize()
        => await initializer.InitializeAsync();
}

License

Code of Conduct

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.

Blogs

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