Esempio n. 1
0
        /// <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));
        }