SELECT DISTINCT(userName) FROM table WHERE userId = 3;
{
"query": {
"term": {
"userId ": 3
}
},
"collapse": {
"field": "userName"
}
}
{
...
"hits": {
"hits": [
{
"_index": "test01",
"_type": "keywords",
"_source": {
"userId": "1",
"userName": "huahua"
},
"fields": {
"pk": [
"1"
]
}
}
]
}
}
总结:使用collapse字段后,查询结果中[hits]中会出现[fields]字段,其中包含了去重后的user_id
SELECT COUNT(DISTINCT(userName)) FROM table WHERE userId= 3;
{
"query": {
"term": {
"userId": 3
}
},
"aggs": {
"count": {
"cardinality": {
"field": "userName"
}
}
}
}
{
...
"hits": {
...
},
"aggregations": {
"count": {
"value": 121
}
}
}
总结:aggs中cardinality的字段代表需要distinct的字段
SELECT COUNT(userName) FROM table GROUP BY userId;
{
"aggs": {
"user_count": {
"terms": {
"field": "userId"
}
}
}
}
{
...
"hits": {
...
},
"aggregations": {
"user_type": {
...
"buckets": [
{
"key": 4,
"doc_count": 500
},
{
"key": 3,
"doc_count": 200
}
]
}
}
}
总结:aggs中terms的字段代表需要gruop by的字段
SELECT COUNT(DISTINCT(userName)) FROM table GROUP BY userId;
{
"aggs": {
"unique_count": {
"terms": {
"field": "userId"
},
"aggs": {
"count": {
"cardinality": {
"field": "userName"
}
}
}
}
}
}
{
...
"hits": {
...
},
"aggregations": {
"unique_count": {
...
"buckets": [
{
"key": 4,
"doc_count": 500, //去重前数据1220条
"count": {
"value": 26//去重后数据276条
}
},
{
"key": 3,
"doc_count": 200, //去重前数据488条
"count": {
"value": 20//去重后数据121条
}
}
]
}
}
}
collapse关键字