public async Task <IReadOnlyCollection <Employee> > ExecuteQueryAsync(EmployeeGetAllQuery query, CancellationToken cancellationToken) { ReadModelDescription readModelDescription = _readModelDescriptionProvider.GetReadModelDescription <EmployeeReadModel>(); string indexName = "eventflow-" + readModelDescription.IndexName.Value; await _elasticClient.FlushAsync(indexName, d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)), cancellationToken) .ConfigureAwait(false); await _elasticClient.RefreshAsync(indexName, d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)), cancellationToken) .ConfigureAwait(false); var searchResponse = await _elasticClient.SearchAsync <EmployeeReadModel>(d => d .RequestConfiguration(c => c .AllowedStatusCodes((int)HttpStatusCode.NotFound)) .Index(readModelDescription.IndexName.Value) .Query(q => q), cancellationToken) .ConfigureAwait(false); var retList = new List <Employee>(); foreach (var doc in searchResponse?.Documents) { var dataEmployee = new Employee(new EmployeeId(doc.Id), doc.FullName ?? string.Empty, doc.Department ?? string.Empty, doc.TenantId); retList.Add(dataEmployee); } return(retList); }
public async Task <IReadOnlyCollection <ThingyMessage> > ExecuteQueryAsync(ThingyGetMessagesQuery query, CancellationToken cancellationToken) { var readModelDescription = _readModelDescriptionProvider.GetReadModelDescription <ElasticsearchThingyMessageReadModel>(); var indexName = readModelDescription.IndexName.Value; // Never do this await _elasticClient.FlushAsync( indexName, d => d .RequestConfiguration(c => c .AllowedStatusCodes((int)HttpStatusCode.NotFound)), cancellationToken) .ConfigureAwait(false); await _elasticClient.RefreshAsync( indexName, d => d .RequestConfiguration(c => c .AllowedStatusCodes((int)HttpStatusCode.NotFound)), cancellationToken) .ConfigureAwait(false); var searchResponse = await _elasticClient.SearchAsync <ElasticsearchThingyMessageReadModel>(d => d .RequestConfiguration(c => c .AllowedStatusCodes((int)HttpStatusCode.NotFound)) .Index(indexName) .Query(q => q.Term(m => m.ThingyId.Suffix("keyword"), query.ThingyId.Value)), cancellationToken) .ConfigureAwait(false); return(searchResponse.Documents .Select(d => d.ToThingyMessage()) .ToList()); }
public async Task <IPagedResponse <Company> > ExecuteQueryAsync(CompanySearchByNameQuery query, CancellationToken cancellationToken) { ReadModelDescription readModelDescription = _readModelDescriptionProvider.GetReadModelDescription <CompanyReadModel>(); string indexName = readModelDescription.IndexName.Value; await _elasticClient.FlushAsync(indexName, d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)), cancellationToken) .ConfigureAwait(false); await _elasticClient.RefreshAsync(indexName, d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)), cancellationToken) .ConfigureAwait(false); var querySearch = !string.IsNullOrEmpty(query.Name) ? new WildcardQuery() { Field = Infer.Field <CompanyReadModel>(f => f.Name), Value = query.Name.ToLower() + "*", } : null; var searchRequest = new SearchRequest(indexName) { Query = new BoolQuery() { Must = new QueryContainer[] { querySearch }, //MustNot = new QueryContainer[] //{ // new ExistsQuery() // { // Field = Infer.Field<CompanyReadModel>(f => f.IsDeleted) // } //} }, From = 1, Size = 10, }; var companyRecords = await _elasticClient.SearchAsync <CompanyReadModel>(searchRequest, cancellationToken); IPagedResponse <Company> response = new PagedResponse <Company>() { Records = (companyRecords?.Documents?.Count > 0) ? companyRecords.Documents.Select(collection => collection.ToCompany()).ToList() : new List <Company>(), TotalCount = (companyRecords?.Documents?.Count > 0) ? companyRecords.Total : 0 }; return(response); }
public async Task <Employee> ExecuteQueryAsync(EmployeeGetQuery query, CancellationToken cancellationToken) { ReadModelDescription readModelDescription = _readModelDescriptionProvider.GetReadModelDescription <EmployeeReadModel>(); string indexName = "eventflow-" + readModelDescription.IndexName.Value; await _elasticClient.FlushAsync(indexName, d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)), cancellationToken) .ConfigureAwait(false); await _elasticClient.RefreshAsync(indexName, d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)), cancellationToken) .ConfigureAwait(false); IGetResponse <EmployeeReadModel> searchResponse = await _elasticClient.GetAsync <EmployeeReadModel>(query.EmployeeId.Value, d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)).Index(readModelDescription.IndexName.Value), cancellationToken) .ConfigureAwait(false); return(searchResponse.Source.ToEmployee()); }
public async Task <IReadOnlyCollection <ThingyMessage> > ExecuteQueryAsync(ThingyGetMessagesQuery query, CancellationToken cancellationToken) { var readModelDescription = _readModelDescriptionProvider.GetReadModelDescription <ElasticsearchThingyMessageReadModel>(); var indexName = readModelDescription.IndexName.Value; // Never do this await _elasticClient.FlushAsync(d => d.Index(indexName)).ConfigureAwait(false); await _elasticClient.RefreshAsync(d => d.Index(indexName)).ConfigureAwait(false); var searchResponse = await _elasticClient.SearchAsync <ElasticsearchThingyMessageReadModel>(d => d .Index(indexName) .Query(q => q.Term(m => m.ThingyId, query.ThingyId.Value))) .ConfigureAwait(false); return(searchResponse.Documents .Select(d => d.ToThingyMessage()) .ToList()); }
public async Task <Company> ExecuteQueryAsync(CompanyGetQuery query, CancellationToken cancellationToken) { ReadModelDescription readModelDescription = _readModelDescriptionProvider.GetReadModelDescription <CompanyReadModel>(); string indexName = readModelDescription.IndexName.Value; await _elasticClient.FlushAsync(indexName, d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)), cancellationToken) .ConfigureAwait(false); await _elasticClient.RefreshAsync(indexName, d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)), cancellationToken) .ConfigureAwait(false); IGetResponse <CompanyReadModel> response = await _elasticClient.GetAsync <CompanyReadModel>( query.CompanyId.Value.ToString(), d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)) .Index(indexName), cancellationToken) .ConfigureAwait(false); return(response.Source.ToCompany()); }
public async Task <int> ExecuteQueryAsync(CompanyGetByAddressQuery query, CancellationToken cancellationToken) { ReadModelDescription readModelDescription = _readModelDescriptionProvider.GetReadModelDescription <CompanyReadModel>(); string indexName = readModelDescription.IndexName.Value; await _elasticClient.FlushAsync(indexName, d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)), cancellationToken) .ConfigureAwait(false); await _elasticClient.RefreshAsync(indexName, d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)), cancellationToken) .ConfigureAwait(false); ISearchResponse <CompanyReadModel> searchResponse; if (query.Id != null) { searchResponse = await _elasticClient.SearchAsync <CompanyReadModel>( d => d .RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)) .Index(indexName) .Query(q => q.Match(m => m.Field(f => f.Address).Query(query.Address).MinimumShouldMatch("100%")) && !q.Match(m => m.Field(f => f.Id).Query(query.Id.Value.ToString()))), cancellationToken); } else { searchResponse = await _elasticClient.SearchAsync <CompanyReadModel>( d => d .RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)) .Index(indexName) .Query(q => q.Match(m => m.Field(f => f.Address).Query(query.Address).MinimumShouldMatch("100%"))), cancellationToken); } var count = searchResponse?.Documents?.Count; return(count ?? 0); }