@Data
@Document(indexName = "test_index_name", type = "testindexname", indexStoreType = "fs", shards = 5, replicas = 1, refreshInterval = "-1")
public class testES{
@Id
private String unid;//主键
@DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")//注解可以以该格式注入格式
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")//输出格式
private Date passtime;
private String name;
}
创建实体类
ES通过ElasticsearchTemplate插入(插入语句输入相同withId也可当作修改使用)
public int saveES(testES params) {
params.setUnid(DateUtils.getUUID());
IndexQuery indexQuery = new IndexQueryBuilder()
.withId(DateUtils.getUUID())//ES表中主键
.withObject(params)//创建实体类入参
.build();
elasticsearchTemplate.index(indexQuery);//插入单条数据
return 0;
}
注意:使用ElasticsearchTemplate插入,若该表不存在则会创建该表并且插入数据。创建表实体字段为什么类型,表字段则为什么类型,后续更改实体不会修改表字段类型。
创建事件类型字段建议保存时间戳,比较容易比较。这里为了方便存储为Date类型时间。
批量插入创建List类IndexQuery
使用bulkIndexApi则为批量插入。
List<IndexQuery> queryList = new ArrayList<>();
for (testES entity : params) {
queryList.add(entity.transToIndexQuery());
}
//插入操作
elasticsearchTemplate.bulkIndex(queryList);
return 0;
删除一条数据
for (testES entity:params) {
DeleteQuery deleteQuery = new DeleteQuery();
deleteQuery.setQuery(QueryBuilders.matchQuery(COLUMN_UNID, entity.getUnid()));//通过ES表字段条件找到该数据
deleteQuery.setIndex("test_index_name");//ES表的Index字段
deleteQuery.setType("testindexname");//ES表的Type字段
elasticsearchTemplate.delete(deleteQuery);//删除语句
查询表达式
查询语句(Query clauses) 就像一些简单的组合块 ,这些组合块可以彼此之间合并组成更复杂的查询。这些语句可以是如下形式:
-
叶子查询语句 (Leaf query clauses) (就像 match 语句) 被用于将查询字符串和一个字段(或者多个字段)对比。
叶查询子句中寻找一个特定的值在某一特定领域,如 match,term或 range查询。这些查询可以单独使用 -
复合查询语句 (Compound query clauses) 语句 主要用于 合并其它查询语句。 比如,一个 bool 语句 允许在你需要的时候组合其它语句,无论是 must 匹配、 must_not 匹配还是 should 匹配,同时它可以包含不评分的过滤器(filters):
-
{ "bool": { "must": { "match": { "tweet": "elasticsearch" }}, "must_not": { "match": { "name": "mary" }}, "should": { "match": { "tweet": "full text" }}, "filter": { "range": { "age" : { "gt" : 30 }} } } }
match 分词匹配 match_all 匹配所有文档。在没有指定查询方式时,它是默认的查询 multi_match 在多个字段上执行相同的 match 查询 range 范围查询 (gt:大于、gte:大于等于、lt:小于、lte:小于等于) term 精确匹配 missing IS_NULL exists NOT IS_NULL 组合多查询编辑
bool 过滤器
constant_score 过滤器
must | 匹配 | |
must_not |
| |
should |
| |
filter |
|
聚合查询
聚合提供了用户进行分组和数理统计的能力,可以把聚合理解成SQL中的GROUP BY和分组函数
指标聚合/桶聚合
Metrics(度量/指标):简单的对过滤出来的数据集进行avg,max操作,是一个单一的数值
Bucket(桶):将过滤出来的数据集按条件分成多个小数据集,然后Metrics会分别作用在这些小数据集上
max/min/avg/sum/stats
{
'aggs':{c
'group_sum':{
'sum':{
'field':'money'
}
}
}
}
{
"aggs":{
"avg_fees":{
"avg":{
"field":"fees"
}
}
}
}
terms聚合
terms根据字段值项分组聚合.field按什么字段分组,size指定返回多少个分组,shard_size指定每个分片上返回多少个分组,order排序方式.可以指定include和exclude正则筛选表达式的值,指定missing设置缺省值
{
'aggs':{
'group_by_type':{
'terms':{
'field':'_type'
}
}
}
}
{
"size": 0,
"aggs": {
"terms":{
"terms": {
"field": "__type",
"size": 10
}
}
}
}
{
"size": 0,
"aggs": {
"terms":{
"terms": {
"field": "__type",
"size": 10,
"order": {
"_count": "asc"
}
}
}
}
}
{
"size": 0,
"aggs": {
"agg_terms": {
"terms": {
"field": "cost",
"order": {
"_count": "asc"
}
},
"aggs": {
"max_balance": {
"max": {
"field": "cost"
}
}
}
}
}
}
{
"size": 0,
"aggs": {
"agg_terms": {
"terms": {
"field": "cost",
"include": ".*",
"exclude": ".*"
}
}
}
}
cardinality去重
{
"size": 0,
"aggs": {
"count_type": {
"cardinality": {
"field": "__type"//去重字段
}
}
}
}