I want to find total website visits by summing up the visits of individual pages.
In my Redis, I have JSONs like:
{'page_name': 'home', 'visit_count' : 10}
{'page_name': 'add_to_cart', 'visit_count' : 7}
{'page_name': 'checkout', 'visit_count' : 5}
I want to run an aggregation logic using FT.AGGREATE so that I can get, total_visits = 10 + 7 + 5 = 22
I can add necessary indexing, if required.
You can use
FT.AGGREGATE
for that. You will have to index at least one field for that.Here is an example using your data.
Add Data
Create an index
At least one field needs to be indexed. Here are few possible options:
Indexing
visit_count
:Indexing
page_name
Indexing
page_name
and referring tovisit_count
(multiple lines for readability):Notice that I mention
visit_count
withNOINDEX
for quick access but without indexing (so it cannot be referred to in the query part, but can be referred to as part of the aggregations).Querying the index
The trick is to use some dummy field that is equal in all the documents. You can use an existing one if you have one, but if not you can create one using
APPLY
(documentation here):will add to all the documents a field named
__dummy
with the value1
. This is only for the query context and it will not be added to the JSON object itself. Of course, any name and value are possible.For cases 1 and 3:
For case 2:
Output (for all cases):
EDIT
Apparently, you can
GROUPBY
0 field, so theAPPLY
trick is redundant.Use
for cases 1 and 3, or, for case 2:
to get the output:
Using redis-py:
ALTERNATIVE APPROACH
If you don't need to index the documents for other reasons, you can use a LUA script to scan all the keys and sum up this value. Scanning the entire key space might be slow if you have a lot of keys, but it will not require additional memory overhead and indexing time when modifying or adding new keys