ElasticSearch.net分组查询

来源:互联网 发布:球球大作战刷魔盒软件 编辑:程序博客网 时间:2024/06/08 10:08
public PUB_StockSearchResult SearchByKeyword(PUB_StockSearch param)        {            PUB_StockSearchResult result = new PUB_StockSearchResult();            result.ResultList = new List<ES_PUB_StockResult>();            QueryContainer query = null;            QueryContainer postfilter = null;            if (!string.IsNullOrEmpty(param.Model))            {                QueryContainer query_model = new WildcardQuery() { Field = ES_PUB_StockField.Model, Value = param.Model.Trim().ToUpper() + "*" };                query = query && query_model;                postfilter = new TermQuery() { Field = ES_PUB_StockField.Model, Value = "sd" };            }            #region            /*if (!string.IsNullOrEmpty(param.Brand))            {                QueryContainer query_brand = new TermQuery() { Field = "brand", Value = param.Brand.Trim() };                query = query || query_brand;                postfilter = postfilter && query_brand;            }            if (!string.IsNullOrEmpty(param.Encapsulation))            {                QueryContainer query_encapsulation = new TermQuery() { Field = "encapsulation", Value = param.Encapsulation.Trim() };                query = query || query_encapsulation;            }            if (!string.IsNullOrEmpty(param.BatchNo))            {                QueryContainer query_batchNo = new TermQuery() { Field = "batchNo", Value = param.BatchNo.Trim() };                query = query || query_batchNo;            }            if (!string.IsNullOrEmpty(param.CategoryNo))            {                QueryContainer query_categoryNo = new WildcardQuery() { Field = "categoryNo", Value = param.CategoryNo.Trim() + "*" };                query = query || query_categoryNo;            }            if (param.UpbyMemeberID > 0)            {                QueryContainer query_upbyMemeberID = new TermQuery() { Field = "upbyMemeberID", Value = param.UpbyMemeberID };                query = query || query_upbyMemeberID;            }*/            #endregion            QueryContainer query_miss = new MissingQuery() { Field = ES_PUB_StockField.SecurityDeposit };            QueryContainer query_notmiss = new TermQuery() { Field = ES_PUB_StockField.SecurityDeposit, Value = 0 };            QueryContainer query_deposit = query_miss || query_notmiss;            query = query && query_deposit;/**/            int start = param.PageIndex * param.PageSize.GetValueOrDefault();            List<ISort> sortlist = new List<ISort>()            {                new SortField{Field=ES_PUB_StockField.ModelLength,Order=SortOrder.Ascending}            };            TermsAggregation aggs = new TermsAggregation("top_tag_hits")                {                    Field = ES_PUB_StockField.UpbyMemberID,                    Aggregations = new TopHitsAggregation("top_hits")                    {                        Sort = new List<ISort>()                        {                            new SortField{Field=ES_PUB_StockField.ModelLength,Order=SortOrder.Ascending},                            new SortField{Field=ES_PUB_StockField.UpdateTime,Order=SortOrder.Descending}                        },                        Size = 10                    },                    Size = param.PageSize                };            var searchRequest = new SearchRequest<ES_PUB_Stock>();            searchRequest.Query = query;            searchRequest.PostFilter = postfilter;            searchRequest.Sort = sortlist;            searchRequest.From = param.PageIndex * param.PageSize;            searchRequest.Size = param.PageSize;            searchRequest.Aggregations = aggs;            var response = client.Search<ES_PUB_Stock>(searchRequest);            var states = response.Aggs.Terms("top_tag_hits");            foreach (var state in states.Buckets)            {                var topStateHits = state.TopHits("top_hits");                var hits = topStateHits.Hits<ES_PUB_Stock>();                foreach (var item in hits.Select(p => p.Source))                {                    applyparam.MEMID = item.upByMemberID;                    ES_PUB_StockResult resultItem = FacetsHelper.FillPicSearchTempEntity(applyparam);                    if (resultItem == null)                    {                        continue;                    }                    resultItem = GetOneStockResult(resultItem, item);                    result.ResultList.Add(resultItem);                }            }            result.Total = result.ResultList.Count;            return result;        }

0 0