打赏

Elasticsearch .net 客户端条件拼接查询

 private Func<SearchDescriptor<BaseTest>, ISearchRequest> ConstructWhere(TestCondition where)
        {
            Func<SearchDescriptor<BaseTest>, ISearchRequest> result;
            List<Func<QueryContainerDescriptor<BaseTest>, QueryContainer>> querys = new List<Func<QueryContainerDescriptor<BaseTest>, QueryContainer>>();

            if (where.EduLevel > 0)
            {
                querys.Add(o => o.Term(f => f.EduLevel, where.EduLevel));
            }

            if (where.Subject > 0)
            {
                querys.Add(o => o.Term(f => f.Subject, where.Subject));
            }
            if (where.TestLevel > 0)
            {
                querys.Add(o => o.Term(f => f.DifficultyLevel, where.TestLevel));
            }
            if (where.TestType > 0)
            {
                querys.Add(o => o.Term(f => f.ExternalType, where.TestType));
            }
            if (where.Kdgs != null && where.Kdgs.Count > 0)
            {
                querys.Add(o => o.Bool(b => b.Should(s => s.Terms(ts => ts.Field(f => f.KnowledgePoints).Terms(where.Kdgs)))));
            }
            if (where.Chps != null && where.Chps.Count > 0)
            {
                querys.Add(o => o.Bool(b => b.Should(s => s.Terms(ts => ts.Field(f => f.Chapters.First().ChapterId).Terms(where.Chps)))));
            }
            if (where.Year > 0)
            {
                querys.Add(o => o.Term(f => f.Year, where.Year));
            }
            if (!where.SearchStr.IsNullOrEmpty())
            {
                querys.Add(o => o.Match(m => m.Field(f => f.TextTitle).Query(where.SearchStr)));
            }
            Func<QueryContainerDescriptor<BaseTest>, QueryContainer> condition =
                o => o.Bool(b => b.Must(
                 querys.ToArray()
                 ));
            if (where.SearchStr.IsNullOrEmpty() && !where.order.IsNullOrEmpty())
            {
                if (where.IsDesc)
                {
                    result =
             o => o.Type(_typeName).Query(condition)
             .From(where.PageIndex).Size(where.PageSize).Sort(s => s.Descending(where.order));
                }
                else
                {
                    result =
             o => o.Type(_typeName).Query(condition)
             .From(where.PageIndex).Size(where.PageSize).Sort(s => s.Ascending(where.order));
                }
            }
            else if(!where.SearchStr.IsNullOrEmpty())
            {
                result =
               o => o.Type(_typeName).Query(condition)
               .From(where.PageIndex).Size(where.PageSize);
            }
            else
            {
                result =
               o => o.Type(_typeName).Query(condition)
               .From(where.PageIndex).Size(where.PageSize);
            }
           
            result += o => o.Source(sc => sc.Include(ic => ic
                .Fields(
                    fd => fd.Oid,
                    fd => fd.TId,
                    fd => fd.ExternalTypeName,
                    fd => fd.ExternalType,
                    fd => fd.DataVersion,
                    fd => fd.Year,
                    fd => fd.DifficultyLevel,
                    fd => fd.Subject,
                    fd => fd.EduLevel,
                    fd => fd.HtmlTitle,
                    fd => fd.BuildPaperCount,
                    fd => fd.TrainCount,
                    fd => fd.CorrectCount,
                    fd => fd.ModifyDateTime,
                    fd => fd.LastReferenceTitle,
                    fd => fd.LastReferencePaperId,
                    fd => fd.ReferenceCount
                    )));
            return result;
        }

 

posted @ 2016-10-21 15:43  Bear.Tirisfal  阅读(1581)  评论(7编辑  收藏  举报