/// <summary>根据聚合条件获取统计信息</summary> public virtual async Task <SearchStatisticsResult> StatisticsAsync(SearchStatisticsRequest request) { var provider = new SearchProvider <Metadata>(); SearchGroupRules groups = new SearchGroupRules(); groups.Top = int.MaxValue / 2; groups.Fields = request.Groups; var aggs = provider.BuildAggs(groups); var searchresponse = await _esAccess.PagingQueryAsync(null, null, null, aggs, 0, 0); SearchStatisticsResult result = new SearchStatisticsResult(); var groupResult = new Dictionary <string, Dictionary <string, long?> >(); foreach (var agg in searchresponse.Aggregations) { var aggregates = agg.Value.As <BucketAggregate>().Items; Dictionary <string, long?> dic = new Dictionary <string, long?>(); foreach (var aggregate in aggregates) { var keyedBucket = aggregate as Nest.KeyedBucket <object>; if (keyedBucket != null) { dic.Add(keyedBucket.Key.ToString(), keyedBucket.DocCount); } } groupResult.Add(agg.Key, dic); } result.Groups = groupResult; return(result); }
/// <summary>按短语搜索</summary> public virtual async Task <SearchResult> SearchAsync(SearchRequest request) { var _provider = new SearchProvider <Metadata>(); var filterQuery = _provider.BuildFilterQuery(request.Filter); // var fulltextQuery = _provider.BuildFullTextQuery(request.Sentence, request.Ranks); var fulltextQuery = _provider.BuildCustomScoreQuery(request.Sentence, request.Boost); var query = _provider.CombineMustQuery(filterQuery, fulltextQuery); var sort = _provider.BuildSort(request.Sort); var aggs = _provider.BuildAggs(request.Group); var fields = _provider.BuildFields(request.Fields); var from = request.From; var size = request.Size; var searchresponse = await _esAccess.PagingQueryAsync(query, sort, fields, aggs, from, size); var response = searchresponse.ToMetadataCollection(); if (searchresponse.Aggregations.Count <= 0) { return(await Task.FromResult(response)); } var groups = new Dictionary <string, Dictionary <string, long?> >(); foreach (var agg in searchresponse.Aggregations) { var aggregates = agg.Value.As <BucketAggregate>().Items; Dictionary <string, long?> dic = new Dictionary <string, long?>(); foreach (var aggregate in aggregates) { var keyedBucket = aggregate as Nest.KeyedBucket <object>; if (keyedBucket != null) { dic.Add(keyedBucket.Key.ToString(), keyedBucket.DocCount); } } groups.Add(agg.Key, dic); } response.Groups = groups; return(await Task.FromResult(response)); }