Elasticsearch杂记

总结:

精确查询用term, 组合查询用bool, 范围用range,and查询用must ,or查询用should,not查询用must not 
##.keyword的意思是全匹配,相当于=
### 在es的查询是,相当于模糊查询,相当鱼like
一些聚合分组查询等等
构造查询条件
1.日期热力图(根据时间间隔查询,比如:1天为间隔)
DateHistogramAggregationBuilder db= AggregationBuilders.dateHistogram("A").field("B").dateHistogramInterval(DateHistogramInterval.DAY).format("yyyy-MM-dd");
含义:B:按照时间间隔差的字段名(表中字段的名字) A:你给这个字段起的别名,解析数据的时候需要使用
2.在时间热力图下再分组
DateHistogramAggregationBuilder db= AggregationBuilders.dateHistogram("date").field("@timestamp").dateHistogramInterval(DateHistogramInterval.DAY).format("yyyy-MM-dd");
                db.subAggregation(AggregationBuilders.terms("这个字段起的别名C").field("字段名.keyword"));
                db.subAggregation(AggregationBuilders.terms("这个字段起的别名D").field("字段名.keyword"));
这样的话,是先分时间端,然后时间段内再分组,“这个字段起的别名C”和“这个字段起的别名D”是并列的关系,不是父子关系,都跟时间段是父子关系
解析的时候,就类似json字符串解析就好了,网上有很多
3.获取max或者avg之类的聚合函数
MaxAggregationBuilder maxAggregationBuilder= AggregationBuilders.max("起的名字").field("字段名");
其他的类似


以上的查询,都是需要用一个方式查询的
Aggregations aggregationsList=elasticsearchTemplate.query(searchQueryList, new ResultsExtractor<Aggregations>() {
                    @Override
                    public Aggregations extract(SearchResponse response) {
                        return response.getAggregations();
                    }});
searchQueryList:这个是条件,就是,mus或者should等这些条件合起来的,还有表明,类型,排序等等的
例如:
SearchQuery searchQueryList = new NativeSearchQueryBuilder()
                        .withIndices(sqlName)   表名
                        .withTypes("doc")       表的类型
                        .withQuery(queryBuilder)    条件:mus或者should等这些条件
                        .withSort(SortBuilders.fieldSort("@timestamp").unmappedType("id").order(SortOrder.DESC))  排序
                        .addAggregation(aggregationBuilder)   聚合条件
                        .build();   


还有一种查询方式:也是很常用的:queryForList
具体的例子如下:
条件还是上面的条件
List list1=elasticsearchTemplate.queryForList(searchQueryList,List.class);
Kibana的增删改查:
PUT /索引名/类型名/文档名
{请求体}:
example:
	PUT /user/_doc/1      //在索引名为user,类型为_doc,文档名为1中,加入一条数据,name:张三  age:3
	{"name":"张三",
	"age":3}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值