roni
roni

Reputation: 9

how to use ft.aggregate in node-redis

I've been trying to use ft.aggregate in node-redis, specifically APPLY and I cant find any good example. Plus, if anyone knows how to use parsetime function in redis I would appreciate it

For the parsetime problem, I have tried the next command:

ft.aggregate idx:example * APPLY parsetime(@date) as date.

And I keep getting: Error: Something went wrong

For the node-redis problem:

redis.ft.aggregate('idx:example, '*',{STEPS:[{type:AggregateSteps.APPLY,

and the rest I'm not sure- what do I need to write in the expression clause, same for the as

Upvotes: 0

Views: 988

Answers (2)

user25557969
user25557969

Reputation: 11

No decent documentation or sample code. The only way to fully understand redis.ft.aggregate() is to study its unit tests.

https://github.com/redis/node-redis/blob/d5355d43275fedf1b2afc4db8a926f72b05f79c5/packages/search/lib/commands/AGGREGATE.spec.ts#L6

It also helps to see the query being sent to Redis live in the Redis Stack Profiler.

Upvotes: 1

Guy Royse
Guy Royse

Reputation: 4332

I poked around with this a bit and got it to work from the command-line. I learned that there are a couple of important tidbits:

  1. You need to LOAD the field you want to call APPLY on in your query.
  2. You need specify the format of the string and, for whatever reason, %FT%TZ won't work.
  3. Since the format is in quotes, you need to wrap the entire APPLY argument in quotes.

See below:

redis.local> FT.AGGREGATE idx:example * LOAD 1 date APPLY "parsetime(@date, '%Y-%m-%dT%H:%M:%SZ')" as date

For the Node Redis code, I was able to get this to work:

const results = await redis.ft.aggregate('test:index', '*', {
  LOAD: [ 'date' ],
  STEPS: [{
    type: AggregateSteps.APPLY,
    expression: "parsetime(@date, '%Y-%m-%dT%H:%M:%SZ')",
    AS: 'date'
  }]
})

Upvotes: 2

Related Questions