Aggregate count. I need the count of how many members there are in a club

52 Views Asked by At

I have a 'users' collection and one of the fields is called "clubs" which is an object. This object is a 1 level json map represented by {'clubname1':23, 'clubname2: 12, 'clubname3: 49, etc} so it is a Map<String, int>. Everytime a new club is created the schema automatically updates with the new club field. So right now, I have added 4 clubs just to test and my schema looks like this:

{
  "created_at": 1691530894,
  "default_sorting_field": "",
  "enable_nested_fields": true,
  "fields": [
    {
      "facet": false,
      "index": false,
      "infix": false,
      "locale": "",
      "name": "imageUrl",
      "optional": true,
      "sort": false,
      "type": "string"
    },
    {
      "facet": false,
      "index": false,
      "infix": false,
      "locale": "",
      "name": "friends",
      "optional": true,
      "sort": false,
      "type": "string[]"
    },
    {
      "facet": false,
      "index": true,
      "infix": true,
      "locale": "",
      "name": "name",
      "optional": false,
      "sort": false,
      "type": "string"
    },
    {
      "facet": true,
      "index": true,
      "infix": true,
      "locale": "",
      "name": "uniqueName",
      "optional": false,
      "sort": false,
      "type": "string"
    },
    {
      "facet": true,
      "index": true,
      "infix": false,
      "locale": "",
      "name": "clubs",
      "optional": true,
      "sort": false,
      "type": "object"
    },
    {
      "facet": true,
      "index": true,
      "infix": false,
      "locale": "",
      "name": "clubs.*",
      "optional": true,
      "sort": false,
      "type": "string"
    },
    {
      "facet": true,
      "index": true,
      "infix": false,
      "locale": "",
      "name": "clubs.a",
      "optional": true,
      "sort": true,
      "type": "int64"
    },
    {
      "facet": true,
      "index": true,
      "infix": false,
      "locale": "",
      "name": "clubs.n",
      "optional": true,
      "sort": true,
      "type": "int64"
    },
    {
      "facet": true,
      "index": true,
      "infix": false,
      "locale": "",
      "name": "clubs.b",
      "optional": true,
      "sort": true,
      "type": "int64"
    },
    {
      "facet": true,
      "index": true,
      "infix": false,
      "locale": "",
      "name": "clubs.v",
      "optional": true,
      "sort": true,
      "type": "int64"
    }
  ],
  "name": "users",
  "num_documents": 3,
  "symbols_to_index": [],
  "token_separators": []
}`

So in order to get a count of how many users are in clubs.a per example I need to do something like:

  final searchParameters = {
    'q': '*',
    'facet_by': 'clubs.a',
    'max_facet_values': '1000000000',
  };`

but this doesnt work. I have found any documentation on how to do this count aggregations. I know there is a field called facet_counts and you also get something called status with max, average, min, etc but when I make this query I get face_counts: [].

I am testing the count for clubs.a right now and at the moment there is only 1 user in that club so I would like to get 1 as the count.

Learn how to make count aggregations in typesense

0

There are 0 best solutions below