赞
踩
must:返回的文档必须满足子句的条件,并且参与计算分值
filter:返回的文档必须满足filter子句的条件,不会参与计算分值
should:返回的文档可能满足should子句的条件。
must_nout:返回的文档必须不满足must_not定义的条件。
注意:如果一个查询既有filter又有should,那么至少包含一个should子句。
"query": { "bool" : { "must" : { "term" : { "user" : "kimchy" } }, "filter": { "term" : { "tag" : "tech" } }, "must_not" : { "range" : { "age" : { "from" : 10, "to" : 20 } } }, "should" : [ {"term" : { "tag" : "wow" }}, {"term" : { "tag" : "elasticsearch" }} ] }
"query": {
"bool":{
"filter":{
"exists":{
"field":"subject" }
}
}
}
"size": 0,
"query": {
"bool":{
"filter":{
"exists":{
"field":"proofreadresult" }
}
}
}
"size": 0, "query": { "bool":{ "must": [{"match": {"ischeck": "false"}}], } } } "size": 0, "query": { "bool":{ "must": {"term" : { "ischeck" : "false" }} } } }
{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3476, "relation" : "eq" }, "max_score" : null, "hits" : [ ] } }
2、字段过滤
过滤字段不存在的数据,filter + exists
"size": 0,
"query": {
"bool":{
"filter":{"exists":{"field":"subject" }},
"must": [{"range": {"createtime": {
"gte": 1654012800000,
"lte": 1655308799000}}
}]
}
}
3、聚合查询
1)分组 https://blog.csdn.net/cainiao1412/article/details/120876972
聚合分组查询,使用terms,按照某个字段中的值来分类,通过size的值来确定返回文档的数量,如果不指定值,默认返回10个
Es query 中,使用 term 进行精准查询,不会对字段进行分词,
使用terms,进行多个条件查询,类似 in
使用 script 进行多个字段 分组的实现,通过需要自己进行split处理
"aggs": {
"result": {
"terms": {
"field": "uid",
"size": 2147483647
}
}
}
"aggs": {
"result": {
"terms": {
"script": { "inline": "doc['uid'].value +'-'+ doc['subject'].value"},
"size": 40
}
}
}
2)聚合查询先进行过滤,然后分组
"size": 0, "aggs": { "filer-subject" : { "filter": { "bool" : { "filter" : [ {"exists":{"field":"subject"}} ] } }, "aggs": { "result" : { "terms": { "script": { "inline": "doc['uid'].value +'-'+ doc['subject'].value"}, "size": 40 } } } } } `` # 返回的数据 ```bash "aggregations" : { "filer-subject" : { "doc_count" : 3557, "result" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 988, "buckets" : [ { "key" : "209372605317121-102", "doc_count" : 794 }, { "key" : "222580707950593-302", "doc_count" : 447 }, { "key" : "212246483697665-102", "doc_count" : 374 }, { "key" : "207843605348353-102", "doc_count" : 328 }] }}
Script script = new Script(ScriptType.INLINE,Script.DEFAULT_SCRIPT_LANG , "doc['uid'].value+'-'+doc['subject'].value", new HashMap<>()); TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("result").script(script).size(ESCommonConstant.ES_GROUP_QUERY_SIZE); // search SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.aggregation(aggregationBuilder); // query BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); List<QueryBuilder> must = boolQueryBuilder.must(); if(Objects.nonNull(requestDWDProofreadRecordDTO.getStartTime()) && Objects.nonNull(requestDWDProofreadRecordDTO.getEndTime())){ must.add(QueryBuilders.rangeQuery(ProofreadDetailConstant.CREATETIME).from(requestDWDProofreadRecordDTO.getStartTime()).to(requestDWDProofreadRecordDTO.getEndTime())); } must.add(QueryBuilders.boolQuery().must((QueryBuilders.existsQuery("subject")))); searchSourceBuilder.query(boolQueryBuilder); searchSourceBuilder.size(0);
3)聚合多条件查询,并按指定的文档返回想要的数据
"size": 0, "aggs": { # 该数据是 条件 ischeck 值为true,subject 存在, # 分组 combinequestiontype分组返回总数量 "ischeck-count" : { "filter": { "bool" : { "filter" : [ {"exists":{"field":"subject"}} ], "must" : {"term" : {"ischeck" : "true"} } } }, "aggs": { "result" : { "terms": { "field": "combinequestiontype", "size": 30 } } } }, # 该数据是 条件 proofreadresult存在, # 分组 combinequestiontype分组返回总数 "proofreadresult-count" : { "filter": { "bool" : { "filter" : [ {"exists":{"field":"proofreadresult"}} ] } }, "aggs": { "result" : { "terms": { "field": "combinequestiontype", "size": 30 } } } }, # 该数据是 返回 isCheck 的数量 "ischeck_total": { "terms": { "field": "ischeck" } }, # 该数据是 返回 proofreadresult 字段存在 的数量 # 即就是 proofreadresult 不为空的数量 "proofreadresult_total": { "filter": { "bool" : { "filter" : [ {"exists":{"field":"proofreadresult"}} ] } } } }
// ischeck-count
FilterAggregationBuilder ischeckCount = AggregationBuilders.filter("ischeck-count", QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("ischeck", true))
.must(QueryBuilders.termQuery("uid", uid))
.filter(QueryBuilders.existsQuery("xxx")))
.subAggregation(AggregationBuilders.terms("result").field("xxx").size(30));
// proofreadresult-count
FilterAggregationBuilder proofreadCount = AggregationBuilders.filter("ischeck-count", QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("ischeck", true))
.must(QueryBuilders.termQuery("uid", uid))
.filter(QueryBuilders.existsQuery("xxx")));
searchSourceBuilder.aggregation(ischeckCount);
// 结果处理
List list = new ArrayList<>();
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
Terms oneTerm = (Terms) response.getAggregations().asMap().get(“result”);
for (Terms.Bucket bucket : oneTerm.getBuckets()) {
ProofreadDetailDTO proofreadDetailDTO = new ProofreadDetailDTO();
// 此处自己split分隔处理
String[] strings = bucket.getKey().toString().split(“-”);
proofreadDetailDTO.setUid(Long.parseLong(strings[0]));
proofreadDetailDTO.setSubject(Integer.parseInt(strings[1]));
list.add(proofreadDetailDTO);
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。