コード例 #1
0
        private IEnumerable <AggregationResult <PlantInfo> > ProcessAggregations(SearchResponse <PlantInfo> response, PlantInfoFindParams plantInfoFindParams)
        {
            var aggregations = new List <AggregationResult <PlantInfo> >();

            foreach (var aggregation in response.Aggregations)
            {
                var bucketAggregations = new List <AggregationResult <PlantInfo> >();
                if (aggregation.Value is SingleBucketAggregate singleBucket)
                {
                    foreach (var bucket in singleBucket)
                    {
                        if (bucket.Value is BucketAggregate bucketValues)
                        {
                            var bucketResults = new Dictionary <string, long?>();
                            // Process values
                            foreach (var bucketValue in bucketValues.Items)
                            {
                                var keyedBucket = bucketValue as KeyedBucket <object>;

                                bucketResults.Add(keyedBucket.Key.ToString(), keyedBucket.DocCount);
                            }

                            if (bucketResults.Any())
                            {
                                bucketAggregations.Add(new AggregationResult <PlantInfo>
                                {
                                    Name   = aggregation.Key,
                                    Values = bucketResults.OrderBy(v => v.Key).ToDictionary(k => k.Key, v => v.Value)
                                });
                            }
                        }
                    }
                    if (!bucketAggregations.Any())
                    {
                        var filter = PlantInfoFindParams.GetFilter(aggregation.Key, plantInfoFindParams) as Filter <string>;
                        bucketAggregations.Add(new AggregationResult <PlantInfo>
                        {
                            Name   = aggregation.Key,
                            Values = new Dictionary <string, long?> {
                                { filter.Value ?? "0", singleBucket.DocCount }
                            }
                        });
                    }

                    aggregations.AddRange(bucketAggregations);
                }
                else if (aggregation.Value is BucketAggregate bucket)
                {
                    var bucketResults = new Dictionary <string, long?>();
                    // Process values
                    foreach (var bucketValue in bucket.Items)
                    {
                        if (bucketValue is RangeBucket rangeBucket)
                        {
                            bucketResults.Add(rangeBucket.Key.ToString(), rangeBucket.DocCount);
                        }
                        else
                        {
                            var keyedBucket = bucketValue as KeyedBucket <object>;
                            bucketResults.Add(keyedBucket.Key.ToString(), keyedBucket.DocCount);
                        }
                    }

                    bucketAggregations.Add(new AggregationResult <PlantInfo>
                    {
                        Name   = aggregation.Key,
                        Values = bucketResults.OrderBy(v => v.Key).ToDictionary(k => k.Key, v => v.Value)
                    });

                    aggregations.AddRange(bucketAggregations);
                }
            }

            return(aggregations);
        }
コード例 #2
0
        public async Task <PlantInfoFindResult> FindPlantInfos(PlantInfoFindParams findParams, Data.Shared.Models.User user)
        {
            if (findParams.Filters == null)
            {
                findParams.Filters = new PlantInfoFilters();
            }

            var plantInfoSearch = await _plantInfoIndex.SearchAsync(findParams, user);

            var plantInfoIds   = plantInfoSearch.Documents.Select(p => p.Id).ToArray();
            var plantInfoQuery = _plantInfoRepository.WhereWithIncludes(p => plantInfoIds.Contains(p.Id),
                                                                        false,
                                                                        p => p.Include(p => p.Lifeform)
                                                                        .Include(p => p.Taxon)
                                                                        .Include(p => p.Origin)
                                                                        .Include(p => p.User)
                                                                        .Include(p => p.MinimumZone).Include(p => p.MaximumZone));

            plantInfoQuery = plantInfoQuery.CanViewContent(user);

            var plantInfoResult = plantInfoQuery.GetSomeAsync(track: false);

            var plantInfos = new List <Data.Shared.Models.PlantInfo>();

            await foreach (var plantInfo in plantInfoResult)
            {
                plantInfos.Add(plantInfo.AsModel());
            }

            if (plantInfoSearch.Aggregations != null)
            {
                foreach (var aggregation in plantInfoSearch.AggregationResult)
                {
                    var filter = PlantInfoFindParams.GetFilter(aggregation.Name, findParams);

                    if (filter is SelectFilter <string> selectFilter)
                    {
                        var values = aggregation.Values;
                        if (!values.Any(v => v.Key == "0"))
                        {
                            values = values.Prepend(new KeyValuePair <string, long?>("0", null)).ToDictionary(k => k.Key, v => v.Value);
                        }
                        selectFilter.FacetValues = values;
                    }
                    if (filter is SelectRangeFilter <double> selectRangeFilter)
                    {
                        var values = aggregation.Values.ToDictionary(k => double.Parse(k.Key), v => v.Value).OrderBy(k => k.Key).ToDictionary(k => k.Key, v => v.Value);
                        if (aggregation.Name.Contains("Min"))
                        {
                            selectRangeFilter.MinFacetValues = values;
                        }
                        else
                        {
                            selectRangeFilter.MaxFacetValues = values;
                        }
                    }
                    if (filter is RangeFilter <string> rangeFilter)
                    {
                        var values = aggregation.Values;
                        if (aggregation.Name == "Bloom")
                        {
                            values = aggregation.Values.ToDictionary(k => int.Parse(k.Key), v => v.Value).OrderBy(k => k.Key).ToDictionary(k => k.Key.ToString(), v => v.Value);
                        }
                        if (!values.Any(v => v.Key == "0"))
                        {
                            values = values.Prepend(new KeyValuePair <string, long?>("0", null)).ToDictionary(k => k.Key, v => v.Value);
                        }
                        rangeFilter.FacetValues = values;
                    }
                }
            }

            return(new PlantInfoFindResult
            {
                Count = plantInfoSearch.Count,
                Results = plantInfoIds.Join(plantInfos, pid => pid, pi => pi.PlantInfoId, (id, p) => p).ToList(),
                Filters = findParams.Filters
            });
        }