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.IndexName) { PageIndex = 1, PageSize = 20 }; var searchRequest = elasticsearchPage.InitSearchRequest(); var predicateList = new List { Predicates.Field(x => x.ToProvince, ExpressOperator.Like, "河南省"), Predicates.Field(x => x.ToCity, ExpressOperator.Like, "信阳市"), Predicates.Field(x => x.ToCity, ExpressOperator.Like, "新县") }; // 前面3个是 and 关系 var predicate = Predicates.Group(GroupOperator.And, predicateList.ToArray()); // FromCity要不包含商丘市,要不就为空值 var predicateListExists = new List { Predicates.Field(x => x.FromCity, ExpressOperator.MustNotExists, null) }; var predicateExists = Predicates.Group(GroupOperator.Or, predicateListExists.ToArray()); var predicateListTerm = new List { Predicates.Field(x => x.FromCity, ExpressOperator.Like, "商丘市") }; var predicateOr = Predicates.Group(GroupOperator.Or, predicateListTerm.ToArray()); var predicatecCombinationChild = new List { predicateExists, predicateOr }; var pgCombinationChild = Predicates.Group(GroupOperator.Or, predicatecCombinationChild.ToArray()); // 查询 var predicatecCombination = new List { 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}"); }
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~