Elasticsearch.Net 多层嵌套的逻辑实现
作者:互联网
原文:Elasticsearch.Net 多层嵌套的逻辑实现
{ "query": { "bool": { "must": [ { "match_phrase": { "to_province": "河南" } }, { "match_phrase": { "to_city": "信阳市" } }, { "match_phrase": { "to_district": "新县" } }, { "bool": { "should": [ { "match_phrase": { "from_city": "商丘市" } }, { "bool": { "must_not": { "exists": { "field": "from_city" } } } } ] } } ], "must_not": [], "should": [] } }, "from": 0, "size": 10, "sort": [], "aggs": {} }
查询条件:前面3个是and关系,后面4和5是或者关系,
ElasticsearchProvider provider = new ElasticsearchProvider(); ElasticsearchProvider es = new ElasticsearchProvider(); var elasticsearchPage = new ElasticsearchPage<AreaJiBao>(AreaJiBao.IndexName) { PageIndex = 1, PageSize = 20 }; var searchRequest = elasticsearchPage.InitSearchRequest(); var predicateList = new List<IPredicate> { Predicates.Field<AreaJiBao>(x => x.ToProvince, ExpressOperator.Like, "河南省"), Predicates.Field<AreaJiBao>(x => x.ToCity, ExpressOperator.Like, "信阳市"), Predicates.Field<AreaJiBao>(x => x.ToCity, ExpressOperator.Like, "新县") }; // 前面3个是 and 关系 var predicate = Predicates.Group(GroupOperator.And, predicateList.ToArray()); // FromCity要不包含商丘市,要不就为空值 var predicateListExists = new List<IPredicate> { Predicates.Field<AreaJiBao>(x => x.FromCity, ExpressOperator.MustNotExists, null) }; var predicateExists = Predicates.Group(GroupOperator.Or, predicateListExists.ToArray()); var predicateListTerm = new List<IPredicate> { Predicates.Field<AreaJiBao>(x => x.FromCity, ExpressOperator.Like, "商丘市") }; var predicateOr = Predicates.Group(GroupOperator.Or, predicateListTerm.ToArray()); var predicatecCombinationChild = new List<IPredicate> { predicateExists, predicateOr }; var pgCombinationChild = Predicates.Group(GroupOperator.Or, predicatecCombinationChild.ToArray()); // 查询 var predicatecCombination = new List<IPredicate> { predicate, pgCombinationChild }; var pgCombination = Predicates.Group(GroupOperator.And, predicatecCombination.ToArray()); searchRequest.InitQueryContainer(pgCombination); var data = provider.SearchPage(searchRequest); foreach (var item in data.Documents) { Console.WriteLine($"{item.Id}"); }
标签:ToArray,Group,List,Predicates,嵌套,Elasticsearch,var,new,Net 来源: https://www.cnblogs.com/lonelyxmas/p/10767438.html