Hi everyone :wave: I'm currently using the `group_...
# community-help
a
Hi everyone 👋 I'm currently using the
group_by
functionality in Typesense to group documents by a specific field (e.g.
supplier_id
). I wanted to know if there’s currently a way to perform aggregation operations like
SUM
,
AVG
,
MIN
, or
COUNT
on grouped fields directly within Typesense
, similar to how SQL or Elasticsearch aggregations work? For example: • Group all documents by
supplier_id
• Then compute the
SUM
or
AVG
of the
price
field per group • And return that in the search result itself Is this kind of aggregation supported? If not, is it something that’s on the roadmap or are there any suggested workarounds apart from post-processing on the client/server side? Thanks in advance! cc. @Sahil Rally @Atishay Jain
🙌 3
ó
You can work around most of the use cases by using facets
a
Ohh yeahh i think ur saying right, I got some idea to solve the summation and related queries using that summation, which will be itself very fast. Do u also have idea, in case i want to put come conditional check on the grouped_hits and get the final summation over it, through typesense only. Lets say, in grp1 ---> h1( price1 = 100) ---> h2( price2 = 10) grp2 ---> h1( price1 = 2) ---> h2( price2 = 1) and i want something like below if i put this condition. (price1>=50 and price2 >=1) 1. grp1 ( 2 hits passed the above condition) 2. grp2 ( 1 hits passed the above condition) so can i put such conditions over hit and get the result, in any way ?
@Óscar Vicente
ó
I'm not sure if I'm following, but you can have ranges, filter, grouping and even perform multiple queries at once for each condition if needed using multi_search. https://typesense.org/28.0/api/search.html#mapping-facet-strings
a
yeahh i got that, but i basically want to do some aggregations in the response over some filter to get some metrics from typesense while they are grouping, just like SQL queries.
ó
You can use filters within the facet
s
@Kishore Nallan @Jason Bosco Pls let us know as we are stuck here. What simply we need is Grouping and doing aggregation on hits for some key inside the Group ,for instance sum of particular key in the Grouped Hits. ( Similar to what you can do in MongoDB grouping). Lets say, in Grp1 ---> h1( quantity = 2) ---> h2( quantity = 3) Grp2 ---> h1( quantity = 1) ---> h2( quantity = 8) Then we need 5 and 9 as calculated Sum aggregation from TS side
ó
Did you try grouping + facets?
k
> Grouping and doing aggregation on hits inside the Group Nested aggregation within a group is not possible. Faceting + group_by will treat each group as a document and can provide aggregation on that.
s
This I believe is a very frequent use case for most of the clients. Not very sure how it is not there in TS.
ó
But @Sahil Rally, did you try using group_by and facets? It will provide you those aggregations
s
Just an example from MongoDB, in case there is some confusion about what we need.
@Ashutosh @Atishay Jain Did you try out what @Óscar Vicente has suggested ?
@Kishore Nallan I dont think we are talking about Nested Aggregation here. What we need is to do some operation like SUM on the grouped results like in above snapshot.
a
@Óscar Vicente Are you suggesting to group by supplierId and facet by quantity ?
yeah @Sahil Rally I tried what @Óscar Vicente is suggesting, with group_by on supplierId and facet on quantity but It is returning aggregations on all documents not aggregations per group. Lets say, in S1 ---> h1( quantity = 1) ---> h2( quantity = 2) S2 ---> h1( quantity = 1) ---> h2( quantity = 2) so the response looked like this
Copy code
{
 "facet_counts": [
  {
   "counts": [
    {
     "count": 2,
     "highlighted": "1",
     "value": "1"
    },
    {
     "count": 2,
     "highlighted": "2",
     "value": "2"
    },
   ],
   "field_name": "quantity",
   "sampled": false,
   "stats": {
    "avg": 2,
    "max": 2,
    "min": 1,
    "sum": 8,
    "total_values": 4
   }
  }
 ],
 "found": 2,
 "found_docs": 4,
 "grouped_hits": [
  {
   "found": 2,
   "group_key": [
    "S1"
   ],
   "hits": [
    {
     "document": {
      "quantity": 1,
      "supplierId": "S1"
     },
     "highlight": {},
     "highlights": []
    },
    {
     "document": {
      "quantity": 2,
      "supplierId": "S1"
     },
     "highlight": {},
     "highlights": []
    },
   ]
  },
  {
   "found": 2,
   "group_key": [
    "S2"
   ],
   "hits": [
    {
     "document": {
      "quantity": 1,
      "supplierId": "S2"
     },
     "highlight": {},
     "highlights": []
    },
    {
     "document": {
      "quantity": 2,
      "supplierId": "S2"
     },
     "highlight": {},
     "highlights": []
    }
   ]
  }
  ]
 "out_of": 4,
 "page": 1,
 "request_params": {
  "collection_name": "supplierinventories",
  "first_q": "",
  "per_page": 10,
  "q": "*"
 },
 "search_cutoff": false,
 "search_time_ms": 63
}
In the above response as we can see the stats has sum as 8 which is the sum of quantity across all hits but we want sum per group which would be like 3 and 3.
a
@Kishore Nallan @Óscar Vicente I tried grouping over supplierId, but faceting on say price it doesnt show facet over groups instead over all of the hits. Say G1 has P1 and G2 also has P1 so facet only has one P1 with count as 2. and the sum is also coming over whole data not per group
a
@Kishore Nallan @Jason Bosco @Fanis Tharropoulos
k
As I mentioned earlier, we don't have a way to aggregate within groups.
There's an open feature request for this which I think you have also commented on https://github.com/typesense/typesense/issues/1520#issuecomment-2870028248
We haven't prioritized this yet. We plan features based on feedback and demand from the community.
s
Demand can be checked from the other parallel offerings as well 🙂 . I believe this is essential at least for us.
k
I know, but there's finite bandwidth unfortunately so we have to prioritize.
❤️ 1
s
It seems , TS is not fitting our use case well. @Kishore Nallan Do we have any paid dedicated support from TS as we need agility ?
@Atishay Jain Can you check https://github.com/typesense/typesense/issues/1520#issuecomment-2870028248 . I dont think, we are asking for same !
k
We have enterprise support options for Typesense Cloud that handles technical consulting and prioritized bug and stability fixes. But that doesn't guarantee feature prioritization.
s
Got it, thanks!