71 lines
2.5 KiB
C#
71 lines
2.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Gos.Core.Search.Aggregations;
|
|
using Gos.Core.Search.Queries;
|
|
using Gos.Infrastructure.Search.Dtos;
|
|
using Gos.Infrastructure.Search.Indexes;
|
|
using Gos.Infrastructure.Search.QueryBuilders;
|
|
using Nest;
|
|
|
|
namespace Gos.Infrastructure.Search.Aggregations
|
|
{
|
|
public abstract class BaseAggregator : IAggregator
|
|
{
|
|
private readonly IElasticClient elasticClient;
|
|
private readonly IIndexProviderFactory indexProviderFactory;
|
|
private readonly IQueryBuilderFactory queryBuilderFactory;
|
|
|
|
protected BaseAggregator(IElasticClient elasticClient, IIndexProviderFactory indexProviderFactory, IQueryBuilderFactory queryBuilderFactory)
|
|
{
|
|
this.elasticClient = elasticClient;
|
|
this.indexProviderFactory = indexProviderFactory;
|
|
this.queryBuilderFactory = queryBuilderFactory;
|
|
}
|
|
|
|
protected abstract string FieldName { get; }
|
|
|
|
public IDictionary<string, int> Get<TQuery>(TQuery query)
|
|
where TQuery : Query
|
|
{
|
|
// Build query for elastic
|
|
var queryBuilder = queryBuilderFactory.GetBuilder<TQuery>();
|
|
var elasticQuery = queryBuilder.Build(query);
|
|
|
|
// Get and execute search request
|
|
var request = GetSearchRequest(elasticQuery);
|
|
var response = elasticClient.Search<EsConcordanceDto>(request);
|
|
|
|
// Read response
|
|
return ReadAggregation(response);
|
|
}
|
|
|
|
private SearchRequest GetSearchRequest(QueryContainer query)
|
|
{
|
|
var indexProvider = indexProviderFactory.GetProvider<EsConcordanceDto>();
|
|
var indexName = indexProvider.IndexName;
|
|
|
|
return new SearchRequest(indexName)
|
|
{
|
|
From = 0,
|
|
Size = 0,
|
|
Query = query,
|
|
Aggregations = new AggregationDictionary()
|
|
{
|
|
{
|
|
"gos_agg", new TermsAggregation("terms")
|
|
{
|
|
Field = FieldName,
|
|
Size = 100,
|
|
}
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
private static IDictionary<string, int> ReadAggregation(ISearchResponse<EsConcordanceDto> response)
|
|
{
|
|
var terms = response.Aggregations.Terms("gos_agg");
|
|
return terms?.Buckets?.ToDictionary(x => x.Key, x => x.DocCount.HasValue ? (int)x.DocCount.Value : 0);
|
|
}
|
|
}
|
|
} |