最近在使用es查詢(xún)某個(gè)字段在特定查詢(xún)條件下的某個(gè)字段的求和時(shí),忘記了query語(yǔ)句是怎么寫(xiě)的,簡(jiǎn)單記錄一下,方便自己和他人查閱。
一 什么是elasticsearch?
elasticsearch是一個(gè)分布式的使用 REST 接口的搜索引擎,簡(jiǎn)稱(chēng)為ES,它是面向文檔的,可以存儲(chǔ)整個(gè)對(duì)象或文檔。
二:elasticsearch的幾種操作
1?對(duì)某個(gè)字段求和,相當(dāng)于sql語(yǔ)句的:
select?sum(字段名)?from?table?where?條件1? and 條件2
#對(duì)某個(gè)字段求和操作
{
"query": {
"bool": {
"must": [
{
"range": {
"條件1": {
"gte": "2022-05-08",
"lte": "2022-05-14"
}
}
},
{
"match": {
"條件1": "***"
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 0,
"sort": [],
"aggs": {
"求和后叫的字段名": {
"sum": {
"field": "求和字段"
}
}
}
}
2?根據(jù)多個(gè)字段進(jìn)行分組,相當(dāng)于sql語(yǔ)句的:
select * from table where 條件1 and 條件2 group by XXX,YYY
#根據(jù)多個(gè)字段進(jìn)行聚合
{
"query": {
"bool": {
"must": [
{
"range": {
"條件1": {
"gte": "2022-05-08",
"lte": "2022-05-08"
}
}
},
{
"match": {
"條件2": "J00371"
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 0,
"sort": [],
"aggregations": {
"分組字段1": {
"terms": {
"field": "es索引中的字段1"
},
"aggregations": {
"分組字段2": {
"terms": {
"field": "es索引中的字段2"
}
}
}
}
}
}
3、向es中插入數(shù)據(jù),相當(dāng)于sql的:
insert into table (字段名1,字段名2,…………) VALUES (字段1的值,字段2的值,……);
PUT /索引名/type/8000
{
"filed1": "value1",
"filed2": "value2",
"filed3": "value3",
………………
}
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-524018.html
4、刪除es的某復(fù)合條件的記錄,相當(dāng)于sql的:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-524018.html
delete from table where condition1 and condition2
{
"query": {
"bool": {
"must": [
{
"match": {
"condition1": "condition1"
}
}
],
"must_not": [],
"should": []
}
}
}
到了這里,關(guān)于elasticsearch的group by分組和sum求和的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!