Пример #1
0
        private static void AddRangeAggregationRequest(Dictionary <string, AggregationContainer> container, string aggregationId, string fieldName, IEnumerable <RangeAggregationRequestValue> values)
        {
            if (values == null)
            {
                return;
            }

            foreach (var value in values)
            {
                var aggregationValueId = $"{aggregationId}-{value.Id}";
                var query = CreateTermRangeQuery(fieldName, value.Lower, value.Upper, value.IncludeLower, value.IncludeUpper);

                var filterAggregation = new FilterAggregation(aggregationValueId)
                {
                    Filter = new BoolQuery
                    {
                        Must = new List <QueryContainer> {
                            query
                        }
                    }
                };

                container.Add(aggregationValueId, filterAggregation);
            }
        }
		public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			if (reader.TokenType != JsonToken.StartObject) return null;
			var container = new QueryContainer();
			serializer.Populate(reader, container);
			var agg = new FilterAggregation();
			agg.Filter = container;
			return agg;
		}
        public override SearchRequest <PolicyDocument> BuildQuery()
        {
            var filters = new List <QueryContainer>();

            if (!string.IsNullOrWhiteSpace(query.FilterByProductCode))
            {
                filters.Add(new TermQuery
                {
                    Field = new Field("productCode.keyword"),
                    Value = query.FilterByProductCode
                });
            }

            if (query.FilterBySalesDateStart != default || query.FilterBySalesDateEnd != default)
            {
                filters.Add(new DateRangeQuery
                {
                    Field = new Field("from"),
                    GreaterThanOrEqualTo = query.FilterBySalesDateStart,
                    LessThanOrEqualTo    = query.FilterBySalesDateEnd
                });
            }


            if (filters.Count == 0)
            {
                filters.Add(new MatchAllQuery());
            }

            var filter = new BoolQuery
            {
                Must = filters
            };


            var termAgg = new TermsAggregation("count_by_product")
            {
                Field        = new Field("productCode.keyword"),
                Aggregations = new SumAggregation("total_premium", new Field("totalPremium"))
            };

            var filteredAgg = new FilterAggregation("agg_filter")
            {
                Filter       = filter,
                Aggregations = termAgg
            };

            return(new SearchRequest <PolicyDocument>
            {
                Aggregations = filteredAgg
            });
        }
Пример #4
0
        private static void AddTermAggregationRequest(IDictionary <string, AggregationContainer> container, string aggregationId, string field, QueryContainer filter, TermAggregationRequest termAggregationRequest)
        {
            var facetSize = termAggregationRequest.Size;

            TermsAggregation termsAggregation = null;

            if (!string.IsNullOrEmpty(field))
            {
                termsAggregation = new TermsAggregation(aggregationId)
                {
                    Field = field,
                    Size  = facetSize == null ? null : facetSize > 0 ? facetSize : int.MaxValue,
                };

                if (termAggregationRequest.Values?.Any() == true)
                {
                    termsAggregation.Include = new TermsIncludeExclude
                    {
                        Values = termAggregationRequest.Values
                    };
                }
            }

            if (filter == null)
            {
                if (termsAggregation != null)
                {
                    container.Add(aggregationId, termsAggregation);
                }
            }
            else
            {
                var filterAggregation = new FilterAggregation(aggregationId)
                {
                    Filter = filter,
                };

                if (termsAggregation != null)
                {
                    filterAggregation.Aggregations = termsAggregation;
                }

                container.Add(aggregationId, filterAggregation);
            }
        }
Пример #5
0
        /// <summary>
        /// Gets the simple aggregation "query"
        /// </summary>
        /// <returns>An AggregationDictionary containing the "query"</returns>
        /// <param name="facetConfig">Configuration for the field to aggregate</param>
        /// <param name="query"></param>
        private AggregationDictionary GetAggQuery(R4RAPIOptions.FacetConfig facetConfig, ResourceQuery query)
        {
            // If we *really* need the parentKey for this facet, then we must add it to the aggregation.
            // however, we may not really need it.
            var keyLabelAggregate = new TermsAggregation($"{facetConfig.FilterName}_key")
            {
                Field = new Field($"{facetConfig.FilterName}.key"), //Set the field to rollup
                Size  = 999,                                        //Use a large number to indicate unlimted (def is 10)
                                                                    // Now, we need to get the labels for the keys and thus
                                                                    // we need to add a sub aggregate for this term.
                                                                    // Normally you would do this for something like city/state rollups
                Aggregations = new TermsAggregation($"{facetConfig.FilterName}_label")
                {
                    Field = new Field($"{facetConfig.FilterName}.label")
                }
            };

            AggregationDictionary aggBody = keyLabelAggregate;

            // This facet requires a parent and thus needs a filter aggregate
            // to wrap the keyLabelAggregate
            if (!String.IsNullOrWhiteSpace(facetConfig.RequiresFilter))
            {
                aggBody = new FilterAggregation($"{facetConfig.FilterName}_filter")
                {
                    Filter       = this.GetQueryForFilterField($"{facetConfig.FilterName}.parentKey", query.Filters[facetConfig.RequiresFilter]),
                    Aggregations = keyLabelAggregate
                };
            }

            //Start with a nested aggregation
            var agg = new NestedAggregation($"{facetConfig.FilterName}_agg")
            {
                Path = new Field(facetConfig.FilterName), //Set the path of the nested agg

                // Add the sub aggregates (bucket keys)
                Aggregations = aggBody
            };

            return(agg);
        }
        public static IAggregation FilterAggregation <T>(this IAggregation agg, IFilter filter,
                                                         params IAggregation[] aggs)
        {
            var incr = 0;

            if (agg.Aggregations == null)
            {
                agg.Aggregations = new Dictionary <string, IAggregation>();
            }
            while (agg.Aggregations.ContainsKey($"agg_filter_{incr}"))
            {
                incr++;
            }

            var filterAgg = new FilterAggregation
            {
                Filter = filter
            };

            var fAggs = new Dictionary <string, IAggregation>();

            foreach (var sa in aggs)
            {
                if (sa.IsSubclassOfRawGeneric(typeof(RangeAggregation <>)) || sa is TermsAggregation)
                {
                    fAggs["nested"] = sa;
                }
                else
                {
                    fAggs[sa.GetType().Name.Replace("Aggregation", "")] = sa;
                }
            }

            filterAgg.Aggregations = fAggs;
            agg.Aggregations[$"agg_filter_{incr}"] = filterAgg;
            return(agg);
        }
Пример #7
0
        private static void AddAggregations(SearchRequest <ElasticArchiveRecord> searchRequest, FacetFilters[] facetsFilters)
        {
            var aggregations = CreateFacet(new TermsAggregation("level")
            {
                Field = "level.keyword", Size = int.MaxValue
            }, facetsFilters);

            aggregations &=
                CreateFacet(new TermsAggregation("customFields.zugänglichkeitGemässBga")
            {
                Field = "customFields.zugänglichkeitGemässBga", Size = 25
            }, facetsFilters);

            aggregations &= CreateFacet(
                new TermsAggregation("aggregationFields.ordnungskomponenten")
            {
                Field = "aggregationFields.ordnungskomponenten",
                Size  = facetsFilters != null && facetsFilters.Any(fac => fac.Facet.Equals("aggregationFields.ordnungskomponenten") && fac.ShowAll) ? int.MaxValue : 25
            }, facetsFilters);
            aggregations &= CreateFacet(new TermsAggregation("aggregationFields.bestand")
            {
                Field = "aggregationFields.bestand",
                Size  = facetsFilters != null && facetsFilters.Any(fac => fac.Facet.Equals("aggregationFields.bestand") && fac.ShowAll) ? int.MaxValue : 25
            }, facetsFilters);                                                                                                                                                // Performance: Limit to 25
            aggregations &=
                CreateFacet(new TermsAggregation("aggregationFields.hasPrimaryData")
            {
                Field = "aggregationFields.hasPrimaryData", Missing = "false"
            },
                            facetsFilters);

            // Zeitraum Filter
            // Für die feinen Filter reicht, wenn wir maximal 10 Stück zurückliefern. Da wir am Ende nur die Facette zurückliefern, die  weniger als 10 Buckets haben
            var order = new List <TermsOrder> {
                new TermsOrder {
                    Key = "_term"
                }
            };

            aggregations &=
                CreateFacet(
                    new TermsAggregation("aggregationFields.creationPeriodYears001")
            {
                Field = "aggregationFields.creationPeriodYears001", Size = 10, Missing = "0", Order = order
            }, facetsFilters);
            aggregations &=
                CreateFacet(
                    new TermsAggregation("aggregationFields.creationPeriodYears005")
            {
                Field = "aggregationFields.creationPeriodYears005", Size = 10, Missing = "0", Order = order
            }, facetsFilters);
            aggregations &=
                CreateFacet(
                    new TermsAggregation("aggregationFields.creationPeriodYears010")
            {
                Field = "aggregationFields.creationPeriodYears005", Size = 10, Missing = "0", Order = order
            }, facetsFilters);
            aggregations &=
                CreateFacet(
                    new TermsAggregation("aggregationFields.creationPeriodYears025")
            {
                Field = "aggregationFields.creationPeriodYears025", Size = 10, Missing = "0", Order = order
            }, facetsFilters);
            aggregations &=
                CreateFacet(
                    new TermsAggregation("aggregationFields.creationPeriodYears100")
            {
                Field = "aggregationFields.creationPeriodYears100", Size = int.MaxValue, Order = order, Missing = "0"
            }, facetsFilters);


            aggregations &= new FilterAggregation("bestellbare_einheiten")
            {
                Filter = new TermQuery {
                    Field = "canBeOrdered", Value = "true"
                },
                Aggregations = new TermsAggregation("nach_level")
                {
                    Field = "level.keyword"
                }
            };

            searchRequest.Aggregations = aggregations;
        }