ElasticSearch7.x:C#的Nest客户端分组,多字段排序-Tems-TopHis

var mustQuerys = new List<Func<QueryContainerDescriptor<toutiao>, QueryContainer>>();//筛选
//多组排序
//List<Func<SortDescriptor<toutiao>, IPromise<IList<ISort>>>> sort = new List<Func<SortDescriptor<toutiao>, IPromise<IList<ISort>>>>();
//sort.Add(srt => srt.Field(sf => sf.Field(p => p.addtime).Order(SortOrder.Descending)));
//query.Add(q => q.Match(t => t.Field("title").Query("生产")));
mustQuerys.Add(q => q.Match(m => m.Field(f => f.title).Query("旋转"))); //筛选
//List<Func<FieldSortDescriptor<toutiao>, IFieldSort>> sort = new List<Func<FieldSortDescriptor<toutiao>, IFieldSort>>();

//////排序
//Func<SortDescriptor<toutiao>, IPromise<IList<ISort>>> sortDesc = sd =>
//{
// //根据分值排序
// // sd.Descending(SortSpecialField.Score);

// //排序
// if (1 == 1)
// sd.Descending(d => d.cid);
// else
// sd.Descending(d => d.cid);
// return sd;
//};
Func<MaxAggregationDescriptor<toutiao>, IMaxAggregation> maxagg = sd1 => sd1.Field("addtime");//排序字段
Func<MaxAggregationDescriptor<toutiao>, IMaxAggregation> maxrankid = sd2 => sd2.Field("rankid");//排序字段
//List<Func<MaxAggregationDescriptor<toutiao>, IMaxAggregation>> maxagg1 = new List<Func<MaxAggregationDescriptor<toutiao>, IMaxAggregation>>();
//maxagg1.Add(m=>m.Field("rankid"));
//maxagg1.Add(m => m.Field("addtime"));
//sd => sd.Field("addtime");
//权限=》时间
Func<TermsOrderDescriptor<toutiao>, IPromise<IList<TermsOrder>>> orderra = or => or.Descending("order_rankid_key").Descending("order_addtime_key");
//时间
Func<TermsOrderDescriptor<toutiao>, IPromise<IList<TermsOrder>>> orderdt = or => or.Descending("order_addtime_key");
//List<Func<FieldSortDescriptor<toutiao>, IFieldSort>> sort1 = new List<Func<FieldSortDescriptor<toutiao>, IFieldSort>>();
//sort.Add(sf => sf.Field(p => p.addtime).Order(SortOrder.Descending));
//List<Func<QueryContainerDescriptor<toutiao>, QueryContainer>> query = new List<Func<QueryContainerDescriptor<toutiao>, QueryContainer>>();
////一个月之内的搜索记录
//query.Add(q => q.Term(t => t.Field("title").Value("阶段")));

var searchResponse = client.Search<toutiao>(s => s
.Query(q => q.Bool(b => b.Must(mustQuerys.ToArray())))
.Aggregations(aggs => aggs
.Terms("group_by_cid", t => t //分组
.Order(orderra)//排序,对应max
.Field(p => p.cid)//分组字段

.Aggregations(aa => aa
.Max("order_addtime_key", maxagg)//构建分组排序字段
.Max("order_rankid_key", maxrankid)//构建分组排序字段
.TopHits("top_cid_hits", th => th //返回前几行
//.Sort(sortDesc)
.Size(1)
//.Sort(s=>s.Descending(f=>f.rankid))
.Source(src => src.Includes(fs => fs //返回字段
.Field(p => p.title)
.Field(p => p.addtime)
.Field(p => p.cid)
.Field(p => p.rankid)
))
.Highlight(h => h
.Fields(hf => hf.Field(p => p.title)
.PreTags("<b style='color:red'>")
.PostTags("</b>")))

)

))));
var results = new List<toutiao>();
var terms = searchResponse.Aggregations.Terms("group_by_cid"); //获取分组结果集

foreach (var bucket in terms.Buckets)
{
var hitcid = bucket.TopHits("top_cid_hits");//获取分组子集结果集
//关键词高亮显示
foreach (var hi in hitcid.Hits<toutiao>())
{
foreach (var highlightField in hi.Highlight)
{
if (highlightField.Key == "title")
{
foreach (var highlight in highlightField.Value)
{
hi.Source.Htitle = highlight.ToString();
}
}
}
results.Add(hi.Source);

}
//var device = hitcid.Hits<toutiao>().First();// hitcid.Documents<toutiao>().First();

//results.Add(device.Source);
//var device1 = hitcid.Documents<toutiao>().ToList<toutiao>();
//foreach (var item in device1)
//{
// results.Add(item);
//}
}

posted @ 2020-09-19 22:41  梦飞翔鱼  阅读(1738)  评论(0编辑  收藏  举报