zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    JS站点
    1011 World Cup Betting (20分)
    1007 Maximum Subsequence Sum (25分)(动态规划DP)
    1006 Sign In and Sign Out (25分)
    1005 Spell It Right (20分)
    1004 Counting Leaves (30分)(DFS)
    1003 Emergency (25分)(Dijkstra算法)
    1002 A+B for Polynomials (25分)
    1001 A+B Format (20分)
    canvas
  • 原文地址:https://www.cnblogs.com/daxiongblog/p/5984866.html
Copyright © 2011-2022 走看看