Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elastic updates #3623

Merged
merged 11 commits into from
Jan 13, 2023
Prev Previous commit
Next Next commit
Fix listing with no query
  • Loading branch information
sfmskywalker committed Jan 13, 2023
commit dde064047b774c2936a0107ef2e1ecd2758ca77e
13 changes: 6 additions & 7 deletions src/modules/Elsa.Elasticsearch/Common/ElasticStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,17 @@ public ElasticStore(ElasticsearchClient elasticClient, ILogger<ElasticStore<T>>
/// </summary>
public async Task<Page<T>> SearchAsync(Action<SearchRequestDescriptor<T>> search, PageArgs? pageArgs, CancellationToken cancellationToken)
{
if (pageArgs != default)
if (pageArgs?.Page != null && pageArgs?.PageSize != null)
{
search += s => s.From(pageArgs.Offset).Size(pageArgs.Limit);
}

var response = await _elasticClient.SearchAsync(search, cancellationToken);

if (response.IsSuccess())
return new Page<T>(response.Hits.Select(hit => hit.Source).ToList()!, response.Total);

_logger.LogError("Failed to search data in Elasticsearch: {Message}", response.ElasticsearchServerError.ToString());
return new Page<T>(new Collection<T>(), 0);

if (!response.IsSuccess())
throw new Exception(response.DebugInformation);

return new Page<T>(response.Hits.Select(hit => hit.Source).ToList()!, response.Total);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,10 @@ public async Task<int> DeleteManyAsync(IEnumerable<string> ids, CancellationToke
public async Task<Page<WorkflowInstanceSummary>> FindManyAsync(FindWorkflowInstancesArgs args, CancellationToken cancellationToken = default)
{
var sortDescriptor = new SortOptionsDescriptor<WorkflowInstance>();
var query = new QueryDescriptor<WorkflowInstance>();

var (searchTerm, definitionId, version, correlationId, workflowStatus, workflowSubStatus, pageArgs, orderBy,
orderDirection) = args;

if (!string.IsNullOrWhiteSpace(definitionId))
query = query.Match(m => m.Field(f => f.DefinitionId).Query(definitionId));

if (version != null)
query = query.Match(m => m.Field(f => f.Version).Query(version.ToString()!));

if (!string.IsNullOrWhiteSpace(correlationId))
query = query.Match(m => m.Field(f => f.CorrelationId).Query(correlationId));

if (workflowStatus != null)
query = query.Match(m => m.Field(f => f.Status).Query(workflowStatus.ToString()!));

if (workflowSubStatus != null)
query = query.Match(m => m.Field(f => f.SubStatus).Query(workflowSubStatus.ToString()!));

if(!string.IsNullOrWhiteSpace(searchTerm))
{
query = query
.QueryString(c => c
.Query(searchTerm));
}

sortDescriptor = orderBy switch
{
OrderBy.Finished => orderDirection == OrderDirection.Ascending
Expand All @@ -124,7 +101,30 @@ public async Task<Page<WorkflowInstanceSummary>> FindManyAsync(FindWorkflowInsta
_ => sortDescriptor
};

var result = await _store.SearchAsync(s => s.Sort(sortDescriptor).Query(query), args.PageArgs, cancellationToken);
var result = await _store.SearchAsync(s =>
{
if (!string.IsNullOrWhiteSpace(definitionId))
s.Query(q => q.Match(m => m.Field(f => f.DefinitionId).Query(definitionId)));

if (version != null)
s.Query(q => q.Match(m => m.Field(f => f.Version).Query(version.ToString()!)));

if (!string.IsNullOrWhiteSpace(correlationId))
s.Query(q => q.Match(m => m.Field(f => f.CorrelationId).Query(correlationId)));

if (workflowStatus != null)
s.Query(q => q.Match(m => m.Field(f => f.Status).Query(workflowStatus.ToString()!)));

if (workflowSubStatus != null)
s.Query(q => q.Match(m => m.Field(f => f.SubStatus).Query(workflowSubStatus.ToString()!)));

if(!string.IsNullOrWhiteSpace(searchTerm))
s.Query(q => q
.QueryString(c => c
.Query(searchTerm)));

s.Sort(sortDescriptor);
}, args.PageArgs, cancellationToken);
return new Page<WorkflowInstanceSummary>(result.Items.Select(WorkflowInstanceSummary.FromInstance).ToList(),
result.TotalCount);
}
Expand Down