如何在ES中实现类似的sql语句呢?
select count(1) as totals from 表名 where 条件;
已知ES结构
{
"from": 0,
"size": 10,
"query": {
"bool": {
"must": [
{
"match_phrase": {
"evaluate": "A"
}
}
],
"filter": [
{
"range": {
"publish_date": {
"gt": "2010-01-03 00:00:00",
"lt": "2023-09-30 00:00:00",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
]
}
},
"sort": [
{
"publish_date": {
"order": "desc"
}
}
]
}
应该在与query平级的基础上加上aggs条件实现count
{
"from": 0,
"size": 10,
"query": {
"bool": {
"must": [
{
"match_phrase": {
"evaluate": "A"
}
}
],
"filter": [
{
"range": {
"publish_date": {
"gt": "2010-01-03 00:00:00",
"lt": "2023-09-30 00:00:00",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
]
}
},
"sort": [
{
"publish_date": {
"order": "desc"
}
}
],
"aggs": {
"group_by_area": {
"cardinality": {
"field": "area"
}
}
}
}
由此便可得出count值