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;
            }
  • 相关阅读:
    sql2slack alash3al 开源的又个轻量级工具
    pgspider fetchq 扩展docker镜像
    godns 集成coredns 的demo
    godns 简单dnsmasq 的dns 替换方案
    aviary.sh 一个基于bash的分布式配置管理工具
    使用coredns 的template plugin实现一个xip 服务
    nginx 代理 coredns dns 服务
    基于nginx proxy dns server
    几个不错的geodns server
    spring boot rest api 最好添加servlet.context-path
  • 原文地址:https://www.cnblogs.com/daxiongblog/p/5984866.html
Copyright © 2011-2022 走看看