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); }
private static IAggregationContainer GetAgg_fn_FamilyName(AggregationContainerDescriptor <familyname> agg, FamilyNameSearchParam ci) { 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)); } agg.Terms("fn", t => t .Field("fn_" + ci.ptype.ToString()) .Size(ci.familyname_num_max)); return(agg); }
private static IMultiSearchRequest GetMultiSearch_FamilyName(MultiSearchDescriptor ms, List <string> names, FamilyNameSearchParam fi) { var flag = string.IsNullOrWhiteSpace(fi.areacode); foreach (var n in names) { ms.Search <familyname>(n, s => s.Index(fi.ESIndex).Type(fi.ESType) .Size(2) .Sort(st => st.Descending(d => d.cweight)) .Source(src => src.Includes(inc => inc.Fields("cname", "ccode", "cmarea", "carea"))) .Query(q => flag ? q.Term(t => t.Field(fi.ptype.ToString()).Value(n)) : (q.Term(t => t.Field(fi.ptype.ToString()).Value(n)) & q.Term(t => t.Field(fi.areacode.Length == 2 ? "cmarea" : "carea").Value(fi.areacode)))) ); } return(ms); }
private static QueryContainer GetContainer_FamilyName(QueryContainerDescriptor <familyname> q, FamilyNameSearchParam ci) { QueryContainer qc = new QueryContainer(); if (!string.IsNullOrWhiteSpace(ci.Familyname)) { qc = q.Term(t => t.Field("fn_" + ci.ptype.ToString()).Value(ci.Familyname)); } if (!string.IsNullOrWhiteSpace(ci.areacode)) { if (ci.areacode.Length == 2) { qc &= q.Term(t => t.Field("cmarea").Value(ci.areacode)); } else if (ci.areacode.Length == 4) { qc &= q.Term(t => t.Field("carea").Value(ci.areacode)); } } return(qc); }
public static FamilyNamePack FamilyNameSearch(FamilyNameSearchParam fi) { if (DateTime.Now.Hour >= 0 && DateTime.Now.Hour < 6) { fi.ESIndex = "familyname_v1"; } else { fi.ESIndex = "familyname_v0"; } var res = new FamilyNamePack(); var s = new SearchDescriptor <familyname>().Index(fi.ESIndex).Type(fi.ESType).Size(0); if (!string.IsNullOrWhiteSpace(fi.areacode)) { s.Query(q => q.Bool(b => b.Filter(f => f.Term(t => t.Field(fi.areacode.Length == 2 ? "cmarea" : "carea").Value(fi.areacode))))); } var fns = new List <string>(); ISearchResponse <familyname> r; bool fn_flag = false; if (string.IsNullOrWhiteSpace(fi.Familyname)) { s.Aggregations(agg => GetAgg_fn_FamilyName(agg, fi)); r = ESClientInst.Client.Search <familyname>(s); var count = r.Aggs.Terms("fn").Buckets.Count; res.totalpage = (count % fi.pg_size == 0) ? count / fi.pg_size : count / fi.pg_size + 1; if (r.Aggregations.ContainsKey("carea")) { var areas = r.Aggs.Terms("carea"); res.Area2Num = areas.Buckets.ToDictionary(b => b.Key, b => (int)b.DocCount); } foreach (var aa in r.Aggs.Terms("fn").Buckets.Skip(fi.pg_index * fi.pg_size).Take(fi.pg_size)) { fns.Add(aa.Key); var fnl = new FamilyName_List() { Familyname = aa.Key, total = (int)(aa.DocCount) }; fnl.list = new List <FN_Name_List>(); res.list.Add(fnl); } //fns.AddRange(r.Aggs.Terms("fn").Buckets.Skip(fi.pg_index * fi.pg_size).Take(fi.pg_size).Select(b => b.Key)); s = new SearchDescriptor <familyname>().Index(fi.ESIndex).Type(fi.ESType).Size(0); //if (!string.IsNullOrWhiteSpace(fi.areacode)) //{ // s.Query(q => q.Bool(b => b.Filter(f => f.Term(t => t.Field(fi.areacode.Length == 2 ? "cmarea" : "carea").Value(fi.areacode))))); //} } else { fn_flag = true; fns.Add(fi.Familyname); res.list.Add(new FamilyName_List() { Familyname = fi.Familyname, list = new List <FN_Name_List>() }); } s.Query(q => { QueryContainer qc = new QueryContainer(); if (!string.IsNullOrWhiteSpace(fi.areacode)) { qc &= q.Bool(b => b.Filter(f => f.Term(t => t.Field(fi.areacode.Length == 2 ? "cmarea" : "carea").Value(fi.areacode)))); } if (fns.Count > 0) { qc &= q.Terms(t => t.Field("fn_" + fi.ptype.ToString()).Terms(fns)); } return(qc); }); s.Aggregations(agg => GetAgg_name_FamilyName(agg, fi, fns)); r = ESClientInst.Client.Search <familyname>(s); if (r.Aggregations.ContainsKey("carea")) { var areas = r.Aggregations.Terms("carea"); res.Area2Num = areas.Buckets.ToDictionary(b => b.Key, b => (int)b.DocCount); } var names = new List <string>(30); var fnr = r.Aggregations.Filters("fn"); var rank_head_flag = fi.pg_index == 0 && !fn_flag; for (var i = 0; i < res.list.Count; ++i) { var l = res.list[i]; if (fnr.ContainsKey(l.Familyname)) { var agg = (Nest.SingleBucketAggregate)fnr.NamedBucket(l.Familyname); var buck = (BucketAggregate)agg.Aggregations["name"]; var items = fn_flag ? buck.Items.Skip(fi.pg_index * fi.pg_size).Take(fi.pg_size) : buck.Items; if (i < fi.rank_head_num && rank_head_flag || fn_flag) { foreach (KeyedBucket <object> item in items) { var nm = (string)item.Key; var fn_name = new FN_Name_List() { name = nm, total = (int)(item.DocCount) }; l.list.Add(fn_name); names.Add(nm); } } else { var item = items.FirstOrDefault() as KeyedBucket <object>; if (item != null) { var nm = (string)item.Key; var fn_name = new FN_Name_List() { name = nm, total = (int)(item.DocCount) }; l.list.Add(fn_name); names.Add(nm); } } if (fn_flag) { l.total = buck.Items.Sum(it => (int)((KeyedBucket <object>)it).DocCount); } } } var mr = ESClientInst.Client.MultiSearch(ms => GetMultiSearch_FamilyName(ms, names, fi)); foreach (var p in res.list) { foreach (var k in p.list) { var rr = mr.GetResponse <familyname>(k.name); k.list = rr.Documents.Select(d => new ComMini() { code = d.ccode, name = d.cname, area = string.IsNullOrWhiteSpace(d.carea) ? d.cmarea : d.carea }).ToList(); } } return(res); }