ElasticSearch的"distinct","count"和"group by"

1 - distinct

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

2 - count + distinct

SELECT COUNT(DISTINCT(userName)) FROM table WHERE userId= 3;
{
  "query": {
    "term": {
      "userId": 3
    }
  },
  "aggs": {
    "count": {
      "cardinality": {
        "field": "userName"
      }
    }
  }
}
{
  ...
  "hits": {
  ...
  },
  "aggregations": {
    "count": {
      "value": 121
    }
  }
}

总结:aggscardinality的字段代表需要distinct的字段

3 - count + group by

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
        }
      ]
    }
  }
}

总结:aggsterms的字段代表需要gruop by的字段

4 - count + distinct + group 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条
          }
        }
      ]
    }
  }
}

 

5 - 注意事项

collapse关键字

  1. 折叠功能ES5.3版本之后才发布的。
  2. 聚合&折叠只能针对keyword类型有效;

你可能感兴趣的:(ElasticSearch)