ElastaticSearch--- es常用的java api
常见查询的DSL:
首先是es常见的DSL语句:
GET index名称/type名称/_search
{
"size" : 10000,
"query" : {
"bool" : {
"must" : [
{
"range" : {
"范围查询字段" : {
"from" : "2020-10-24",
"to" : "2020-10-30"
}
}
},
{
"term" : {
"查询字段" : {
"value" : "匹配值"
}
}
},
{
"exists" : {
"field" : "是否存在的字段名称"
}
}
]
}
}
}
对应的java代码如下:
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(10000);
BoolQueryBuilder bollQuery = QueryBuilders.boolQuery();
// bollQuery.filter(QueryBuilders.termQuery(过滤字段,匹配值));
bollQuery.must(QueryBuilders.termQuery(查询字段,匹配值));
bollQuery.must(QueryBuilders.existsQuery(是否存在的字段名称,匹配值));
bollQuery.must(QueryBuilders.rangeQuery(范围查询字段).from(开始).to(结束));
String dsl = searchSourceBuilder.query(bollQuery).toString();
聚合的DSL:
- 聚合统计:
聚合的语句,示例如下:
GET index名称/type名称/_search
{
"size" : 0,
"query" : {
"bool" : {
"filter" : [
{
"term" : {
"过滤字段1" : {
"value" : "匹配值"
}
}
},
{
"term" : {
"过滤字段2" : {
"value" : "匹配值"
}
}
}
]
}
},
"aggregations" : {
"自已命令的聚合名称1" : {
"terms" : {
"field" : "聚合分组字段",
"size" : 10000,
"order" : {
"排序字段" : "asc"
}
},
"aggregations" : {
"自已命令的聚合名称2" : {
"sum" : {
"field" : "聚合求和字段"
}
}
}
}
}
}
对应的java代码如下:
//只聚合求结果,不需要查询结果
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(0);
BoolQueryBuilder bollQuery = QueryBuilders.boolQuery();
bollQuery.filter(QueryBuilders.termQuery(过滤字段1,匹配值));
bollQuery.filter(QueryBuilders.termQuery(过滤字段2,匹配值));
TermsAggregationBuilder termsAggregation = AggregationBuilders.terms(自己命名的聚合名称).field(聚合分组字段)
.order(Terms.Order.aggregation(排序字段, false)).size(10000)
.subAggregation(AggregationBuilders.sum(自己命名的聚合求和名称).field(聚合求和字段))
.subAggregation(AggregationBuilders.max(自己命名的聚合求最大值名称).field(聚合求最大值字段));
searchSourceBuilder.aggregation(termsAggregation);
String aggregation = searchSourceBuilder.query(bollQuery).toString();
通过聚合查询后返回的结果 returnDataStr,使用fastjson解析,代码如下:
JSONObject returnDataJson = JSON.parseObject(returnDataStr);
JSONObject aggregations = returnDataJson.getJSONObject(AGGREGATIONS).getJSONObject(聚合分组字段);
JSONArray bucketsArray = aggregations.getJSONArray(BUCKETS);
for (Object bucket : bucketsArray) {
JSONObject bucketJson = (JSONObject) bucket;
String key = bucketJson.getString(KEY);
JSONObject numJson = bucketJson.getJSONObject(聚合字段);
int num =0;
if (numJson != null && numJson.getBigDecimal(VALUE)!=null) {
num = numJson.getBigDecimal(VALUE).intValue();
}
}
- script:
es多字段聚合,对性能要求不高的话,可以使用script。
查询语句如下:
{
"size" : 0,
"query" : {
"bool" : {
"filter" : [
{
"term" : {
"过滤字段1" : {
"value" : "匹配值"
}
}
},
{
"term" : {
"过滤字段2" : {
"value" : "匹配值"
}
}
}
]
}
}
,"aggregations" : {
"聚合名称" : {
"terms" : {
"script" : {
"inline" : "doc['字段名称1'].values +'-'+ doc['字段名称2'].values",
"lang" : "painless"
},
"size" : 10000,
"order" : {
"排序字段" : "desc"
}
},
"aggregations" : {
"自已命令的聚合名称" : {
"sum" : {
"field" : "聚合求和字段"
}
}
}
}
}
}
对应的java代码如下:
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(0);
BoolQueryBuilder bollQuery = QueryBuilders.boolQuery();
bollQuery.filter(QueryBuilders.termQuery(过滤字段,匹配值));
String idOrCode = "doc['"+字段名称+"'].values +'-'+ doc['"+字段名称+"'].values";
Script script = new Script(idOrCode);
TermsAggregationBuilder termsAggregation = AggregationBuilders.terms(自己命名的聚合名称).script(script)
.order(Terms.Order.aggregation(排序字段, false)).size(10000)
.subAggregation(AggregationBuilders.sum(自己命名的聚合求和名称).field(聚合求和字段))
.subAggregation(AggregationBuilders.max(自己命名的聚合求最大值名称).field(聚合求最大值字段));
searchSourceBuilder.aggregation(termsAggregation);
String aggregation = searchSourceBuilder.query(bollQuery).toString();