public static AggregationContainerDescriptor <T> SortedTopHits <T>(this AggregationContainerDescriptor <T> agg, int size, Expression <Func <T, object> > fieldSort, SortType sorttype, params Expression <Func <T, object> >[] fieldGetter) where T : class { var aggName = sorttype + fieldSort.GetAggName(AggType.TopHits); var sortFieldDescriptor = new SortFieldDescriptor <T>(); var fieldSortName = Names.GetNameFromGetFieldNamed(fieldSort.Body); sortFieldDescriptor = fieldSortName != null?sortFieldDescriptor.Field(fieldSortName) : sortFieldDescriptor.Field(fieldSort); sortFieldDescriptor = sorttype == SortType.Ascending ? sortFieldDescriptor.Ascending() : sortFieldDescriptor.Descending(); var fieldNames = fieldGetter.Select(x => Names.GetNameFromGetFieldNamed(x.Body)).Where(x => x != null); var fieldGetters = fieldGetter.Where(x => Names.GetNameFromGetFieldNamed(x.Body) == null); var allFields = fieldNames.Select(x => new Field(x)).Concat(fieldGetters.Select(x => new Field(x))); return(agg.TopHits( aggName, x => x .Size(size) .Source(i => i.Includes(f => f.Fields(allFields))) .Sort(s => new PromiseValue <IList <ISort> >(new List <ISort> { sortFieldDescriptor })))); }
/// <summary> /// Groups on the list of provided fields, returns multiple nested groups /// </summary> public static AggregationContainerDescriptor <T> GroupBy <T>(this AggregationContainerDescriptor <T> innerAggregation, IEnumerable <string> keys) where T : class { var reversedAndLowered = keys.Select(x => x.FirstCharacterToLower()).Reverse().ToList(); var aggregations = reversedAndLowered.Aggregate(innerAggregation, (s, i) => s.GroupBy(i)); return(aggregations); }
/// <summary> /// Get the Aggregated token count /// </summary> /// <param name="interpretedFields">DON'T PUT THE DOCUMENTELASTIC->TEXT FIELD HERE! RATHER THE INTERPRETEDFIELDS</param> /// <param name="ngramCount"></param> /// <param name="tagId">if it's null then the Aggregate will be run on all the DocumentElastic</param> /// <returns></returns> public int GetAllWordsOccurences(List <string> interpretedFields, int ngramCount, string tagField = null) { var desc = new SearchDescriptor <DocumentElastic>(); var aggDesc = new AggregationContainerDescriptor <DocumentElastic>(); foreach (var interpretedField in interpretedFields) { aggDesc.Sum( interpretedField, ad => ad.Field($"{interpretedField}.1{_tokenCountSuffix}")); } desc.Size(0); desc.Aggregations(a => aggDesc); desc.Query(q => q.MatchAll()); var response = Client.Search <DocumentElastic>(desc); ResponseValidator(response); // calculate the count for the current n-gram from the unigrams count var allCount = response.Aggregations.Sum(a => Convert.ToInt32(((ValueAggregate)a.Value).Value)) - (ngramCount - 1); return(allCount); }
private static IAggregationContainer GetAgg_name_FamilyName(AggregationContainerDescriptor <familyname> agg, FamilyNameSearchParam ci, List <string> fns = null) { var size = ci.pg_index == 0 ? ci.name_num_max : 1; if (!string.IsNullOrWhiteSpace(ci.Familyname)) { size = ci.familyname_num_max * ci.name_num_max * ci.com_num_max; } agg.Filters("fn", fs => fs .NamedFilters(nf => GetNamedFilters_FamilyName(nf, "fn_" + ci.ptype.ToString(), fns)) .Aggregations(ca => ca.Terms("name", t => t.Field(ci.ptype.ToString()).Size(size)))); if (!string.IsNullOrWhiteSpace(ci.Familyname)) { if (string.IsNullOrWhiteSpace(ci.areacode)) { agg.Terms("carea", t => t .Field("cmarea").Size(32)); } else if (ci.areacode.Length == 2) { agg.Terms("carea", t => t .Field("carea").Size(24)); } } return(agg); }
/// <summary> /// 获取聚合 /// </summary> /// <param name="agg"></param> /// <param name="ci"></param> /// <returns></returns> private IAggregationContainer Get_Agg(AggregationContainerDescriptor <T> agg, QueryDes qd) { var ls = qd.Aggs; if (ls == null) { return(agg); } foreach (var s in ls) { switch (s) { case 1: agg.Terms("m_area", t => t.Field("m_area").Size(33)); break; case 2: agg.Range("od_regm", r => r.Field("od_regm").Ranges(rs => rs.To(100), rs => rs.From(100).To(500), rs => rs.From(500).To(1000), rs => rs.From(1000))); break; case 3: agg.DateHistogram("od_regdate", t => t.Field("od_regdate").Interval(DateInterval.Year) .MinimumDocumentCount(1).ExtendedBounds(new DateTime(1900, 1, 1), DateTime.Now)); break; } } return(agg); }
public static SmlouvaSearchResult Search(QueryContainer query, int page, int pageSize, OrderResult order, AggregationContainerDescriptor <Lib.Data.Smlouva> anyAggregation = null, int?platnyZaznam = null, bool includeNeplatne = false, bool logError = true, bool fixQuery = true) { var result = new SmlouvaSearchResult() { Page = page, PageSize = pageSize, OrigQuery = "", Q = "", Order = ((int)order).ToString() }; ISearchResponse <Lib.Data.Smlouva> res = _coreSearch(query, page, pageSize, order, anyAggregation, platnyZaznam, includeNeplatne, logError); if (res.IsValid == false && logError) { Lib.ES.Manager.LogQueryError <Lib.Data.Smlouva>(res, query.ToString()); } result.Total = res?.Total ?? 0; result.IsValid = res?.IsValid ?? false; result.ElasticResults = res; return(result); }
public static ReportDataSource TopListPerSum(bool platce) { string field = "prijemce.ico"; if (platce) { field = "platce.ico"; } int size = 300; AggregationContainerDescriptor <HlidacStatu.Lib.Data.Smlouva> aggs = new AggregationContainerDescriptor <HlidacStatu.Lib.Data.Smlouva>() .Terms("perPrice", m => m .Order(o => o.Descending("sumincome")) .Field(field) .Size(size) .Aggregations(agg => agg .Sum("sumincome", s => s .Field(ff => ff.CalculatedPriceWithVATinCZK) ) ) ); var res = HlidacStatu.Lib.Data.Smlouva.Search.RawSearch("{\"query_string\": { \"query\": \"-id:pre*\" } }", 1, 0, anyAggregation: aggs); ReportDataSource rdsPerPrice = new ReportDataSource(new ReportDataSource.Column[] { new ReportDataSource.Column() { Name = "IČO", HtmlRender = (s) => { System.Tuple <string, string> data = (System.Tuple <string, string>)s; return(string.Format("<a href='/subjekt/{0}'>{1}</a>", data.Item2, data.Item1.Replace("&", "&"))); }, TextRender = (s) => { return(((System.Tuple <string, string>)s).Item1.ToString()); } }, new ReportDataSource.Column() { Name = "Součet cen", HtmlRender = (s) => { return(HlidacStatu.Lib.Data.Smlouva.NicePrice((double?)s, html: true, shortFormat: true)); }, OrderValueRender = (s) => { return(HlidacStatu.Util.RenderData.OrderValueFormat((double?)s)); } }, } );;; foreach (Nest.KeyedBucket <object> val in ((BucketAggregate)res.Aggregations["perPrice"]).Items) { HlidacStatu.Lib.Data.Firma f = HlidacStatu.Lib.Data.Firmy.Get((string)val.Key); if (f != null && (!f.PatrimStatu() || platce)) { rdsPerPrice.AddRow( new Tuple <string, string>(HlidacStatu.Lib.Data.Firmy.GetJmeno((string)val.Key), (string)val.Key), val.Sum("sumincome").Value ); } } return(rdsPerPrice); }
/// <summary> /// 分页查询(异步) /// </summary> /// <param name="query">查询条件</param> /// <param name="sort">排序规则</param> /// <param name="fields">返回字段</param> /// <param name="aggs">聚合条件</param> /// <param name="from">记录开始</param> /// <param name="size">记录数</param> /// <returns></returns> public async Task <ISearchResponse <T> > PagingQueryAsync(QueryContainer query, SortDescriptor <T> sort, SourceFilterDescriptor <T> fields, AggregationContainerDescriptor <T> aggs, int from = 0, int size = 10) { var result = await Client.SearchAsync <T>(s => BuildSearchDescriptor(query, sort, fields, aggs, from, size)); result.ThrowIfIsNotValid(); return(result); }
public DotaceSearchResult SimpleSearch(string query, int page, int pagesize, DotaceSearchResult.DotaceOrderResult order, bool withHighlighting = false, AggregationContainerDescriptor <Dotace> anyAggregation = null, bool exactNumOfResults = false) { return(SimpleSearch(query, page, pagesize, ((int)order).ToString(), withHighlighting, anyAggregation, exactNumOfResults)); }
public static Nest.ISearchResponse <BankovniPolozka> SearchPolozkyForOsoba(int osobaInternalId, BankovniUcet bu = null, int size = 500, AggregationContainerDescriptor <BankovniPolozka> anyAggregation = null) { return(SearchPolozkyRaw( string.Format("comments.valueInt:{0} AND comments.typeId:1", osobaInternalId), bu, size)); }
public static Nest.ISearchResponse <Lib.Data.Smlouva> RawSearch(Nest.QueryContainer query, int page, int pageSize, OrderResult order = OrderResult.Relevance, AggregationContainerDescriptor <Lib.Data.Smlouva> anyAggregation = null, int?platnyZaznam = null, bool includeNeplatne = false ) { var res = _coreSearch(query, page, pageSize, order, anyAggregation, platnyZaznam: platnyZaznam, includeNeplatne: includeNeplatne, logError: true); return(res); }
public static Nest.ISearchResponse <Lib.Data.Smlouva> RawSearch(string jsonQuery, int page, int pageSize, OrderResult order = OrderResult.Relevance, AggregationContainerDescriptor <Lib.Data.Smlouva> anyAggregation = null, bool?platnyZaznam = null, bool includeNeplatne = false, bool exactNumOfResults = false ) { return(RawSearch(Searching.Tools.GetRawQuery(jsonQuery), page, pageSize, order, anyAggregation, platnyZaznam, includeNeplatne, exactNumOfResults: exactNumOfResults)); }
private AggregationContainerDescriptor <TEntity> GetAggregation(List <AggregationItem> aggregations) { AggregationContainerDescriptor <TEntity> aggregation = new AggregationContainerDescriptor <TEntity>(); if (aggregations != null) { foreach (var item in aggregations) { string name = item.Name; switch (item.Type) { case AggregationExpressionType.Count: aggregation.Terms(name, aggr => { aggr.Field(item.Name) .Size(item.Size) //.Exclude(item.ExcludedItems) .Aggregations(child => GetAggregation(item.Children)); if (!string.IsNullOrEmpty(item.SortField)) { aggr.Order(new TermsOrder() { Key = item.SortField, Order = item.SortType == "asc" ? SortOrder.Ascending : SortOrder.Descending }); } return(aggr); }); break; case AggregationExpressionType.Range: SetRangeAggregationContainer(item, aggregation); break; case AggregationExpressionType.Sum: aggregation.Sum(name, d => d.Field(item.Name)); break; case AggregationExpressionType.Avg: aggregation.Average(name, d => d.Field(item.Name)); break; case AggregationExpressionType.Min: aggregation.Min(name, d => d.Field(item.Name)); break; case AggregationExpressionType.Max: aggregation.Max(name, d => d.Field(item.Name)); break; default: break; } } } return(aggregation); }
public PagedResponse<ProductDoc> SearchByKeyword(PagedRequest<ProductSearchRequest> request) { //QueryContainer filter = null; var matchFields = new string[] { "name^20", "description^5", "instruction" }; //var categoryLevelMatch = request.Data.FilterCategoryLevel.HasValue ? request.Data.FilterCategoryLevel == 1 ? "categoryLevel1" : request.Data.FilterCategoryLevel == 2 ? "categoryLevel2" : "categoryLevel3" : "categoryLevel3"; var categoryLevelMatch = "categoryLevel3"; var query = new QueryBuilder() .AndMultiMatch(matchFields, request.Data.Keyword) .Build(); var filterBuilder = new QueryBuilder(); foreach (var c in request.Data.CategoryName) { filterBuilder = filterBuilder.OrMatch(categoryLevelMatch, request.Data.CategoryName); } foreach (var bname in request.Data.BrandName) { filterBuilder = filterBuilder.OrMatch("brandName", bname); } foreach (var t in request.Data.Tag) { filterBuilder = filterBuilder.OrMatch("tags", t); } var filter = filterBuilder.Build(); //var query = new QueryBuilder() // .OrMultiMatch(matchFields, request.Data.Keyword) // //.AndMatch("brandName", request.Data.BrandName) // .AndMatch(categoryLevelMatch, request.Data.CategoryName) // .Build(); AggregationContainerDescriptor<ProductDoc> categoryAgg = null; if (request.Data.NeedAggregation) { categoryAgg = new AggregationContainerDescriptor<ProductDoc>() .Terms("agg_brand", f => f.Field("brandName")) .Terms("agg_category_level_3", f => f.Field("categoryLevel3")) .Terms("agg_tag", f => f.Field("tags")); } //switch (request.Data.FilterCategoryLevel) //{ // case 1: // categoryAgg.Terms("agg_category_level_1", f => f.Field("categoryLevel1")); // break; // case 2: // categoryAgg.Terms("agg_category_level_2", f => f.Field("categoryLevel2")); // break; // default: // case 3: // categoryAgg.Terms("agg_category_level_3", f => f.Field("categoryLevel3")); // break; //} var result = Client.SetPageNum(request.PageNumber).SetPageSize(request.PageSize).SearchResponse(query, filter, a => categoryAgg); return Client.WrapResult(result); }
public static ReportDataSource SmlouvyPodleCeny(string query) { DateTime minDate = new DateTime(2012, 1, 1); DateTime maxDate = DateTime.Now.Date.AddDays(1); double[] ranks = new double[] { 0, 50000, 100000, 500000, 1000000, 5000000, 10000000 }; AggregationContainerDescriptor <HlidacStatu.Lib.Data.Smlouva> aggs = new AggregationContainerDescriptor <HlidacStatu.Lib.Data.Smlouva>() .PercentileRanks("x-agg", h => h .Field(f => f.CalculatedPriceWithVATinCZK) .Values(ranks) ); //var res = HlidacStatu.Lib.ES.SearchTools.RawSearch("{\"query_string\": { \"query\": \"-id:pre*\" } }", 1, 0, anyAggregation: aggs); var res = HlidacStatu.Lib.ES.SearchTools.SimpleSearch("(" + query + ") AND datumUzavreni:{" + HlidacStatu.Util.RenderData.ToElasticDate(minDate) + " TO " + HlidacStatu.Util.RenderData.ToElasticDate(maxDate) + "}", 1, 0, HlidacStatu.Lib.ES.SearchTools.OrderResult.FastestForScroll, anyAggregation: aggs); ReportDataSource rds = new ReportDataSource(new ReportDataSource.Column[] { new ReportDataSource.Column() { Name = "Hodnota smlouvy", TextRender = (s) => { return(s.ToString()); } }, new ReportDataSource.Column() { Name = "% smluv", TextRender = (s) => { return(((double)s).ToString("N2") + "%"); }, HtmlRender = (s) => { return(((double)s).ToString("N2") + "%"); }, OrderValueRender = (s) => { return(((double)s).ToString("N2")); } }, } ); rds.Title = "Smlouvy podle hodnoty"; var data = ((PercentilesAggregate)res.ElasticResults.Aggregations["x-agg"]).Items.ToArray(); double prevVal = 0; double prevPercent = 0; for (int i = 0; i < data.Count(); i++) { string x = data[i].Percentile.ToString("N0") + " Kč"; if (i > 0) { x = data[i - 1].Percentile.ToString("N0") + " Kč -" + x; } else { x = "Bez ceny"; } rds.AddRow(x, data[i].Value - prevVal); prevVal = data[i].Value; } rds.AddRow("nad " + data[data.Count() - 1].Percentile.ToString("N0") + " Kč", 100 - prevVal); return(rds); }
public static Dictionary <int, BasicData> SmlouvyPerYear(string query, int[] interestedInYearsOnly) { AggregationContainerDescriptor <HlidacStatu.Lib.Data.Smlouva> aggY = new AggregationContainerDescriptor <HlidacStatu.Lib.Data.Smlouva>() .DateHistogram("x-agg", h => h .CalendarInterval(DateInterval.Year) .Field(f => f.datumUzavreni) ); AggregationContainerDescriptor <HlidacStatu.Lib.Data.Smlouva> aggYSum = new AggregationContainerDescriptor <HlidacStatu.Lib.Data.Smlouva>() .DateHistogram("x-agg", h => h .Field(f => f.datumUzavreni) .CalendarInterval(Nest.DateInterval.Year) .Aggregations(agg => agg .Sum("sumincome", s => s .Field(ff => ff.CalculatedPriceWithVATinCZK) ) ) ); var res = HlidacStatu.Lib.Data.Smlouva.Search.SimpleSearch(query, 1, 0, HlidacStatu.Lib.Data.Smlouva.Search.OrderResult.FastestForScroll, aggYSum, exactNumOfResults: true); Dictionary <int, BasicData> result = new Dictionary <int, BasicData>(); if (interestedInYearsOnly != null) { foreach (int year in interestedInYearsOnly) { result.Add(year, BasicData.Empty()); } foreach (Nest.DateHistogramBucket val in ((BucketAggregate)res.ElasticResults.Aggregations["x-agg"]).Items) { if (result.ContainsKey(val.Date.Year)) { result[val.Date.Year].Pocet = val.DocCount ?? 0; result[val.Date.Year].CelkemCena = (decimal)(((Nest.DateHistogramBucket)val).Sum("sumincome").Value ?? 0); } } } else { foreach (Nest.DateHistogramBucket val in ((BucketAggregate)res.ElasticResults.Aggregations["x-agg"]).Items) { result.Add(val.Date.Year, BasicData.Empty()); result[val.Date.Year].Pocet = val.DocCount ?? 0; result[val.Date.Year].CelkemCena = (decimal)(((Nest.DateHistogramBucket)val).Sum("sumincome").Value ?? 0); } } return(result); }
public static Nest.ISearchResponse <BankovniPolozka> SearchPolozkyForOsoba(string nameId, BankovniUcet bu = null, int size = 500, AggregationContainerDescriptor <BankovniPolozka> anyAggregation = null) { HlidacStatu.Lib.Data.Osoba o = HlidacStatu.Lib.Data.Osoby.GetByNameId.Get(nameId); if (o == null) { return(null); } return(SearchPolozkyForOsoba(o.InternalId)); }
public static ReportDataSource HodnotaSmluvPerUzavreni(string query, DateInterval interval, DateTime?minDate = null, DateTime?maxDate = null) { minDate = minDate ?? new DateTime(2012, 1, 1); maxDate = maxDate ?? DateTime.Now.Date.AddDays(1); AggregationContainerDescriptor <HlidacStatu.Lib.Data.Smlouva> aggs = new AggregationContainerDescriptor <HlidacStatu.Lib.Data.Smlouva>() .DateHistogram("x-agg", h => h .Field(f => f.datumUzavreni) .Interval(interval) .Format("yyyy-MM-dd") .Aggregations(agg => agg .Sum("sumincome", s => s .Field(ff => ff.CalculatedPriceWithVATinCZK) ) ) ); ReportDataSource rdsPerIntervalSumPrice = new ReportDataSource(new ReportDataSource.Column[] { new ReportDataSource.Column() { Name = "Měsíc", TextRender = (s) => { DateTime dt = ((DateTime)s); return(string.Format("Date.UTC({0}, {1}, {2})", dt.Year, dt.Month - 1, dt.Day)); }, OrderValueRender = (s) => { return(HlidacStatu.Util.RenderData.OrderValueFormat((DateTime)s)); }, }, new ReportDataSource.Column() { Name = "Součet cen", HtmlRender = (s) => { return(HlidacStatu.Lib.Data.Smlouva.NicePrice((double?)s, html: true, shortFormat: true)); }, OrderValueRender = (s) => { return(HlidacStatu.Util.RenderData.OrderValueFormat((double?)s)); }, }, }); var res = HlidacStatu.Lib.Data.Smlouva.Search.SimpleSearch("( " + query + " ) AND datumUzavreni:{" + HlidacStatu.Util.RenderData.ToElasticDate(minDate.Value) + " TO " + HlidacStatu.Util.RenderData.ToElasticDate(maxDate.Value) + "}", 1, 0, HlidacStatu.Lib.Data.Smlouva.Search.OrderResult.FastestForScroll, anyAggregation: aggs, exactNumOfResults: true); foreach (Nest.DateHistogramBucket val in ((BucketAggregate)res.ElasticResults.Aggregations["x-agg"]).Items ) { if (val.Date >= minDate && val.Date <= maxDate) { rdsPerIntervalSumPrice.AddRow( new DateTime(val.Date.Ticks, DateTimeKind.Utc).ToLocalTime(), ((Nest.DateHistogramBucket)val).Sum("sumincome").Value ); } } return(rdsPerIntervalSumPrice); }
public static ReportDataSource TopListPerCount(bool platce) { string field = "prijemce.ico"; if (platce) { field = "platce.ico"; } int size = 300; AggregationContainerDescriptor <HlidacStatu.Lib.Data.Smlouva> aggs = new AggregationContainerDescriptor <HlidacStatu.Lib.Data.Smlouva>() .Terms("perIco", m => m .Field(field) .Size(size) ); var res = HlidacStatu.Lib.Data.Smlouva.Search.RawSearch("{\"query_string\": { \"query\": \"-id:pre*\" } }", 1, 0, anyAggregation: aggs); ReportDataSource rdsPerIco = new ReportDataSource(new ReportDataSource.Column[] { new ReportDataSource.Column() { Name = "IČO", HtmlRender = (s) => { System.Tuple <string, string> data = (System.Tuple <string, string>)s; return(string.Format("<a href='/subjekt/{0}'>{1}</a>", data.Item2, data.Item1.Replace("&", "&"))); }, TextRender = (s) => { return(((System.Tuple <string, string>)s).Item1.ToString()); } }, new ReportDataSource.Column() { Name = "Počet smluv", HtmlRender = (s) => { return(HlidacStatu.Util.RenderData.NiceNumber(HlidacStatu.Util.ParseTools.ToDecimal((string)s) ?? 0, html: true)); }, OrderValueRender = (s) => { return(HlidacStatu.Util.RenderData.OrderValueFormat(HlidacStatu.Util.ParseTools.ToDecimal((string)s) ?? 0)); } }, } ); foreach (Nest.KeyedBucket <object> val in ((BucketAggregate)res.Aggregations["perIco"]).Items) { HlidacStatu.Lib.Data.Firma f = HlidacStatu.Lib.Data.Firmy.Get((string)val.Key); if (f != null && (!f.PatrimStatu() || platce)) { rdsPerIco.AddRow( new Tuple <string, string>(HlidacStatu.Lib.Data.Firmy.GetJmeno((string)val.Key), (string)val.Key), val.DocCount.ToString() ); } } return(rdsPerIco); }
public DotaceSearchResult SimpleSearch(string query, int page, int pagesize, string order, bool withHighlighting = false, AggregationContainerDescriptor <Dotace> anyAggregation = null, bool exactNumOfResults = false) { return(SimpleSearch(new DotaceSearchResult() { Q = query, Page = page, PageSize = pagesize, Order = Devmasters.TextUtil.NormalizeToNumbersOnly(order), ExactNumOfResults = exactNumOfResults }, withHighlighting, anyAggregation));; }
//public RetailShopDoc GetRetailShopDocByCity(int cityId, int? retailerId = null) //public ISearchResponse<RetailShopDoc> GetRetailShopDocByCity(int cityId, int? retailerId = null) public PagedResponse<RetailShopDoc> GetRetailShopDocByCity(int cityId, int? retailerId = null) { var fb = new FilterBuilder().AndTerm("cityId", cityId); if (retailerId.HasValue) { fb.AndTerm("retailerId", retailerId.Value); } var filter = fb.Build(); // maybe add size var cityAgg = new AggregationContainerDescriptor<RetailShopDoc>().Terms("agg_city", f => f.Field("cityId")); var result = Client.SearchResponse(filter: filter, aggregation: a => cityAgg); return Client.WrapResult(result); }
/// <summary> /// Creates a nested aggregation. Wraps the aggregation on which it is called by a new Terms aggregation, using the provided fieldName. /// </summary> public static AggregationContainerDescriptor <T> GroupBy <T>(this AggregationContainerDescriptor <T> innerAggregation, string fieldName) where T : class { var v = new AggregationContainerDescriptor <T>(); v.Terms(fieldName, tr => { var trmAggDescriptor = new TermsAggregationDescriptor <T>(); trmAggDescriptor.Field(fieldName); trmAggDescriptor.Size(int.MaxValue); return(trmAggDescriptor.Aggregations(x => innerAggregation)); }); return(v); }
public static AggregationContainerDescriptor <T> GetStatsDescriptor <T>(this AggregationContainerDescriptor <T> agg, Expression <Func <T, object> > fieldGetter, AggType aggType, Expression <Func <T, bool> > filterRule = null) where T : class { var aggFunc = GetAggregationFunc <T>(fieldGetter, aggType); if (filterRule == null) { return(aggFunc(agg)); } var filterName = filterRule.GenerateFilterName(); agg.Filter(filterName, f => f.Filter(fd => filterRule.Body.GenerateFilterDescription <T>()).Aggregations(aggFunc)); return(agg); }
public static AggregationContainerDescriptor <T> GroupBy <T>(this AggregationContainerDescriptor <T> innerAggregation, String key) where T : class { AggregationContainerDescriptor <T> v = new AggregationContainerDescriptor <T>(); v.Terms(key, tr => { TermsAggregationDescriptor <T> trmAggDescriptor = new TermsAggregationDescriptor <T>(); trmAggDescriptor.Field(key); trmAggDescriptor.Size(int.MaxValue); return(trmAggDescriptor.Aggregations(x => innerAggregation)); }); return(v); }
///// <summary> ///// 获得排序脚本 ///// </summary> ///// <param name="sort">排序规则</param> ///// <param name="field">排序字段</param> ///// <returns></returns> //private static string GetOrderScript(Sort sort, string field) //{ // var value = sort.Promotion; // var sb = new StringBuilder(); // for (int i = 0; i < value.Count + 1; i++) // { // string scriptText; // string itemValue; // if (i == 0) // { // itemValue = value[i].Trim(); // // scriptText = $"if('{itemValue}' == doc['{field.KeyWord()}'].value)return 0;"; // scriptText = $"'{itemValue}'==doc['{field.KeyWord()}'].value?0"; // } // else if (i == value.Count) // { // // scriptText = $"else return {value.Count}"; // scriptText = $":{i}"; // for (var j = 0; j < value.Count - 1; j++) // { // scriptText += ")"; // } // } // else // { // itemValue = value[i]; // //scriptText = $"else if('{itemValue}' == doc['{field.KeyWord()}'].value)return {i};"; // scriptText = $":('{itemValue}'==doc['{field.KeyWord()}'].value?{i}"; // } // sb.Append(scriptText); // } // return sb.ToString(); //} /// <summary> /// 根据聚合条件构建聚合器 /// </summary> /// <param name="groups">聚合条件</param> /// <returns></returns> public AggregationContainerDescriptor <T> BuildAggs(SearchGroupRules groups) { if (groups == null || groups.Fields.Count == 0) { return(null); } var aggsDesc = new AggregationContainerDescriptor <T>(); foreach (var gfield in groups.Fields) { aggsDesc.Terms(gfield, s => s.Field(KeyWord(gfield)).Size(groups.Top).OrderDescending("_count")); } return(aggsDesc); }
public static InsolvenceSearchResult SimpleSearch(string query, int page, int pagesize, int order, bool withHighlighting = false, bool limitedView = true, AggregationContainerDescriptor <Lib.Data.Insolvence.Rizeni> anyAggregation = null, bool exactNumOfResults = false) { return(SimpleSearch(new InsolvenceSearchResult() { Q = query, Page = page, PageSize = pagesize, LimitedView = limitedView, Order = order.ToString(), ExactNumOfResults = exactNumOfResults }, withHighlighting, anyAggregation));; }
public static AggregationContainerDescriptor <T> IntoHistogram <T>(this AggregationContainerDescriptor <T> innerAggregation, Expression <Func <T, object> > fieldGetter, int interval) where T : class { AggregationContainerDescriptor <T> v = new AggregationContainerDescriptor <T>(); var fieldName = GetName(fieldGetter); v.Histogram(fieldName, dr => { HistogramAggregationDescriptor <T> dateAggDesc = new HistogramAggregationDescriptor <T>(); dateAggDesc.Field(fieldGetter).Interval(interval); return(dateAggDesc.Aggregations(x => innerAggregation)); }); return(v); }
public static SmlouvaSearchResult SimpleSearch(string query, int page, int pageSize, string order, AggregationContainerDescriptor <Lib.Data.Smlouva> anyAggregation = null, bool?platnyZaznam = null, bool includeNeplatne = false, bool logError = true, bool fixQuery = true, bool withHighlighting = false, bool exactNumOfResults = false) { order = Devmasters.TextUtil.NormalizeToNumbersOnly(order); OrderResult eorder = OrderResult.Relevance; System.Enum.TryParse <OrderResult>(order, out eorder); return(SimpleSearch(query, page, pageSize, eorder, anyAggregation, platnyZaznam, includeNeplatne, logError, fixQuery, withHighlighting, exactNumOfResults )); }
/// <summary> /// Creates a nested aggregation. Wraps the aggregation on which it is called by a new Terms aggregation, using the provided fieldGetter function to terms on the field. /// </summary> public static AggregationContainerDescriptor <T> GroupBy <T>(this AggregationContainerDescriptor <T> innerAggregation, Expression <Func <T, object> > fieldGetter) where T : class { var fieldName = fieldGetter.GetAggName(AggType.GroupBy); var v = new AggregationContainerDescriptor <T>(); v.Terms(fieldName, tr => { var trmAggDescriptor = new TermsAggregationDescriptor <T>(); trmAggDescriptor.Field(fieldGetter); trmAggDescriptor.Size(int.MaxValue); return(trmAggDescriptor.Aggregations(x => innerAggregation)); }); return(v); }
public static Nest.ISearchResponse <BankovniPolozka> SearchPolozkyRaw(string queryString, BankovniUcet bu = null, int size = 500, AggregationContainerDescriptor <BankovniPolozka> anyAggregation = null) { AggregationContainerDescriptor <BankovniPolozka> baseAggrDesc = null; baseAggrDesc = anyAggregation == null ? new AggregationContainerDescriptor <BankovniPolozka>().Sum("sumKc", m => m.Field(f => f.Castka)) : anyAggregation; Func <AggregationContainerDescriptor <BankovniPolozka>, AggregationContainerDescriptor <BankovniPolozka> > aggrFunc = (aggr) => { return(baseAggrDesc); }; Nest.ISearchResponse <BankovniPolozka> res = null; if (bu != null) { res = HlidacStatu.Lib.ES.Manager.GetESClient_Ucty() .Search <BankovniPolozka>(a => a .Size(500) .Aggregations(aggrFunc) .Query(qq => qq.Bool(b => b .Must( m => m.Term(t => t.Field(f => f.CisloUctu).Value(bu.Id)), m1 => m1.QueryString(qs => qs .Query(queryString) .DefaultOperator(Nest.Operator.And) )) ) ) ); } else { res = HlidacStatu.Lib.ES.Manager.GetESClient_Ucty() .Search <HlidacStatu.Lib.Data.TransparentniUcty.BankovniPolozka>(a => a .Size(500) .Aggregations(aggrFunc) .Query(qq => qq.QueryString(qs => qs .Query(queryString) .DefaultOperator(Nest.Operator.And) ) ) ); } return(res); }
public static AggregationContainerDescriptor <T> SortedTopHits <T>(this AggregationContainerDescriptor <T> agg, int size, Expression <Func <T, object> > fieldSort, SortType sorttype, params Expression <Func <T, object> >[] fieldGetter) where T : class { var aggName = sorttype + fieldSort.GetAggName(AggType.TopHits); var sortFieldDescriptor = new SortFieldDescriptor <T>(); sortFieldDescriptor = sortFieldDescriptor.Field(fieldSort); if (sorttype == SortType.Ascending) { sortFieldDescriptor = sortFieldDescriptor.Ascending(); } else { sortFieldDescriptor = sortFieldDescriptor.Descending(); } return(agg.TopHits(aggName, x => x.Size(size).Source(i => i.Include(f => f.Fields(fieldGetter))).Sort(s => sortFieldDescriptor))); }
public PagedResponse<RetailShopDoc> SearchByKeyword(PagedRequest<RetailShopSearchRequest> request) { QueryContainer filter = null; if (request.Data.RetailerId.HasValue && request.Data.RetailerId.Value != 0) { filter = new FilterBuilder().AndTerm("retailerId", request.Data.RetailerId).Build(); } var matchFields = new string[] { "address", "name", "displayName" }; var query = new QueryBuilder() .OrMultiMatch(matchFields, request.Data.Keyword) .Build(); var cityAgg = new AggregationContainerDescriptor<RetailShopDoc>().Terms("agg_city", f => f.Field("cityId")); var result = Client.SetPageNum(request.PageNumber).SetPageSize(request.PageSize).SearchResponse(query, filter, a => cityAgg); return Client.WrapResult(result); }
public (decimal Sum, int Count) GetStatisticsForIco(string ico) { var dotaceAggs = new AggregationContainerDescriptor <Dotace>() .Sum("souhrn", s => s .Field(f => f.DotaceCelkem) ); var dotaceSearch = this.SimpleSearch($"ico:{ico}", 1, 1, Searching.DotaceSearchResult.DotaceOrderResult.FastestForScroll, false, dotaceAggs, exactNumOfResults: true); decimal sum = (decimal)dotaceSearch.ElasticResults.Aggregations.Sum("souhrn").Value; int count = (int)dotaceSearch.Total; return(sum, count); }
public List<string> SearchByPrefixKeyword(string keyword) { QueryContainer filter = null; var query = new QueryBuilder() .AndPrefixMatch("keyword", keyword) .Build(); var categoryAgg = new AggregationContainerDescriptor<SearchHistoryDoc>() .Terms("agg_name", f => f.Field("keyword").Size(5)); var searchResult = Client.SetPageNum(0).SetPageSize(10).SearchResponse(query, filter, a => categoryAgg); var doc = Client.WrapResult(searchResult); var agg = doc.Aggregation.Aggregations["agg_name"]; var result = new List<string>(); foreach (KeyedBucket item in ((BucketAggregate)agg).Items) { result.Add(item.Key); } return result; }
public PagedResponse<ProductDoc> SearchByPrefixKeyword(ProductSearchRequest request) { QueryContainer filter = null; var query = new QueryBuilder() .AndPrefixMatch("name_NA", request.Keyword) .Build(); var categoryAgg = new AggregationContainerDescriptor<ProductDoc>() .Terms("agg_name", f => f.Field("name_NA").Size(5)); var result = Client.SetPageNum(1).SetPageSize(0).SearchResponse(query, filter, a => categoryAgg); return Client.WrapResult(result); }