Es查询,解构类似: A & (a & b & c) or (c & e)
JavaAPI写法:
BoolQueryBuilder query = QueryBuilders.boolQuery(); BoolQueryBuilder q1 = QueryBuilders.boolQuery(); q1.must(QueryBuilders.termQuery("flag_best", 1)); q1.must(QueryBuilders.termQuery("flag_fornew", 1));
q1.must(QueryBuilders.termQuery("flag_off",0));
BoolQueryBuilder q2 = QueryBuilders.boolQuery(); q2.must(QueryBuilders.prefixQuery("sub_category", "sub")); q2.must(QueryBuilders.termQuery("flag_reviewed", 1));
q2.must(QueryBuilders.termQuery("flag_off",0));
query.should(q1);
query.should(q2);
注意:如果此处存在一个共同的查询值,如flag_off,那么一定要在两个query中分别写出
写成【query.must(QueryBuilders.termQuery("flag_off",0))】将会产生错误
错误写法:(写法本身有问题,并且不能在后面连续进行must会溢出。应参照文章最后JSON结构来写Java代码)
query.should(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("flag_best",1)).must(QueryBuilders.termQuery("flag_fornew",1)).must(QueryBuilders.termQuery("flag_off",0)));
query.should(QueryBuilders.boolQuery().must((QueryBuilders.prefixQuery("sub_category","sub"))).must(query.must(QueryBuilders.termQuery("flag_reviewed", 1)).must(QueryBuilders.termQuery("flag_off",0))));
JSON正确格式:
{ "from": 0, "size": 50, "query": { "bool": { "should": [ { "bool": { "must": [ { "term": { "flag_best": { "value": 1, "boost": 1 } } }, { "term": { "flag_fornew": { "value": 1, "boost": 1 } } }, { "term": { "flag_off": { "value": 0, "boost": 1 } } }, { "range": { "add_time": { "from": "0", "to": "2017-04-20", "include_lower": true, "include_upper": true, "boost": 1 } } } ], "disable_coord": false, "adjust_pure_negative": true, "boost": 1 } }, { "bool": { "must": [ { "prefix": { "sub_category": { "value": "sub", "boost": 1 } } }, { "term": { "flag_reviewed": { "value": 1, "boost": 1 } } }, { "term": { "flag_off": { "value": 0, "boost": 1 } } }, { "range": { "add_time": { "from": "0", "to": "2017-04-20", "include_lower": true, "include_upper": true, "boost": 1 } } } ], "disable_coord": false, "adjust_pure_negative": true, "boost": 1 } } ], "disable_coord": false, "adjust_pure_negative": true, "boost": 1 } }, "explain": true, "sort": [ { "add_time": { "order": "desc" } }, { "image_count": { "order": "desc" } } ] }
JSON错误写法:
{ "from": 0, "size": 50, "query": { "bool": { "must": [ { "term": { "flag_off": { "value": 0, "boost": 1 } } }, { "range": { "add_time": { "from": "0", "to": "2017-04-20", "include_lower": true, "include_upper": true, "boost": 1 } } } ], "should": [ { "bool": { "must": [ { "term": { "flag_best": { "value": 1, "boost": 1 } } }, { "term": { "flag_fornew": { "value": 1, "boost": 1 } } } ], "disable_coord": false, "adjust_pure_negative": true, "boost": 1 } }, { "bool": { "must": [ { "prefix": { "sub_category": { "value": "sub", "boost": 1 } } }, { "term": { "flag_reviewed": { "value": 1, "boost": 1 } } } ], "disable_coord": false, "adjust_pure_negative": true, "boost": 1 } } ], "disable_coord": false, "adjust_pure_negative": true, "boost": 1 } }, "explain": true, "sort": [ { "add_time": { "order": "desc" } }, { "image_count": { "order": "desc" } } ] }
JSON主体结构:
{"query": { "bool": { "should": [ {"bool": { "must": [ {} ] } }, {"bool": { "must": [ {} ] }} ] } } }